Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d388e41
Add Hello World
JakeIV Oct 19, 2022
768b3ca
Improvement, helloWorld into its own class
Jonas-Oppong Oct 19, 2022
e60bca5
Change " to '
JakeIV Oct 19, 2022
a185da3
Update web/src/App.tsx
JakeIV Oct 19, 2022
9c7b319
Update web/src/App.tsx
JakeIV Oct 19, 2022
e4d02b9
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
Jonas-Oppong Oct 19, 2022
492bf8e
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
94e3f88
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
627d83f
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
47315b7
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
83404cc
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
725f79b
Merge branch 'main' into feature/JakeIV/snakeGameHW
Jonas-Oppong Oct 19, 2022
9189789
Created SnakeEngine.ts
IsaacAkin Oct 19, 2022
109a849
fixed linting
Jonas-Oppong Oct 19, 2022
79e644a
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
IsaacAkin Oct 19, 2022
a1c0b5e
change
IsaacAkin Oct 19, 2022
19591d0
formatted
JakeIV Oct 19, 2022
f3d6c9b
Update App.tsx
IsaacAkin Oct 19, 2022
91fa84a
Added board and snake 🐍
mark-chit Oct 19, 2022
7f86b88
Update web/src/App.tsx
Jonas-Oppong Oct 19, 2022
bfe0ce8
Update App.tsx
mark-chit Oct 19, 2022
0dcea24
Update App.tsx
Jonas-Oppong Oct 24, 2022
f208694
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
Jonas-Oppong Oct 26, 2022
4759ca5
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
IsaacAkin Oct 26, 2022
bf209ee
Added controller
Jonas-Oppong Oct 26, 2022
b96bf82
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
IsaacAkin Oct 26, 2022
59ce233
Snake Now moves
Jonas-Oppong Oct 26, 2022
de3ab61
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
Jonas-Oppong Oct 26, 2022
af7bc53
Merge branch 'feature/JakeIV/snakeGameHW' of https://github.com/gdscp…
JakeIV Oct 26, 2022
1f25614
View prototype
Jonas-Oppong Nov 9, 2022
88a5863
prototype board code
Jonas-Oppong Nov 9, 2022
c94305c
V4
Jonas-Oppong Nov 9, 2022
cd80df1
AddToBoard
JakeIV Nov 9, 2022
16d5ca0
UpdateBoard
JakeIV Nov 9, 2022
37af233
Updated View
JakeIV Nov 9, 2022
1d3b96d
Main SudoCode
JakeIV Nov 9, 2022
daef122
Main Loop v1
JakeIV Nov 16, 2022
0548532
generates board every time it is viewed
JakeIV Nov 16, 2022
c2370ff
generation v2
Jonas-Oppong Nov 16, 2022
1154a3a
Error fix, with coords printed to console.
JakeIV Nov 19, 2022
6cd8d7f
Board Wrapping Added and console cleared in view
JakeIV Nov 23, 2022
e2f5afb
added new lines
JakeIV Nov 23, 2022
6d273b0
food
Jonas-Oppong Nov 30, 2022
ac4e0c5
Added Food
JakeIV Dec 7, 2022
4974d87
Added arrow key control
JakeIV Dec 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import './App.css';

import reactLogo from './assets/react.svg';
import IncrementButton from './IncrementButton';
import { SnakeEngine } from './SnakeEngine/SnakeEngine';

SnakeEngine.helloWorld();

function App() {
return (
Expand All @@ -15,14 +17,16 @@ function App() {
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<div className='card'>
<IncrementButton incrementBy={1} />
<IncrementButton incrementBy={2} />
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className='read-the-docs'>Click on the Vite and React logos to learn more</p>
<p className='read-the-docs'>
Click on the Vite and React logos to learn more
</p>
</div>
);
}
Expand Down
186 changes: 186 additions & 0 deletions web/src/SnakeEngine/SnakeEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
class Constants {
static SnakeSpeed = 2;
}

export class SnakeEngine {
static boardSize = 9;
static gameOver = false
static food = { x: 0, y: 0 }
static snake = {
// length: 3,
body: [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 2, y: 0 },
],
direction: 'right',
};

static board = this.boardGenerator();

static randomCoord() {
return Math.floor(Math.random() * (SnakeEngine.boardSize + 1));
}

static boardGenerator() {
const board: string[] = [];
for (let i = 0; i < 10; i++) {
let row = '';
for (let j = 0; j < 10; j++) {
row += '0';
}
board.push(row);
}
SnakeEngine.snake.body.forEach(element => {
this.UpdateBoard(element.y, element.x, board, true);
console.log('BoardGen x: ', element.x, 'y: ', element.y);
});
this.UpdateBoard(SnakeEngine.food.y, SnakeEngine.food.x, board, false);
// this.UpdateBoard(0, 0, board, true);
// this.UpdateBoard(0, 1, board, true);
// this.UpdateBoard(0, 2, board, true);
return board;
}

static UpdateBoard(y: number, x: number, board: Array<string>, snake: boolean) {
const row = board[y];
// console.log('Update x: ', x, 'y: ', y);
const a = row.split('');
if (snake === true) {
a[x] = 'S';
} else {
a[x] = 'F';
}
// add ? a[x] = 'S' : a[x] = '0'; // add if true remove if false
board[y] = a.join('');
return board;
}

static helloWorld() {
const speed = new Constants();
console.log(speed);
}

static view(board: Array<string>) {
SnakeEngine.board = this.boardGenerator(); // board is generated
console.clear();
let boardString = '';
for (let i = 0; i < board.length - 1; i++) {
boardString += board[i] + '\n';
}
console.log(boardString);
}

static model() {
// const newSnakeHead = SnakeEngine.snake.body[SnakeEngine.snake.body.length - 1]; // change to slice
let x = SnakeEngine.snake.body[SnakeEngine.snake.body.length - 1].x;
let y = SnakeEngine.snake.body[SnakeEngine.snake.body.length - 1].y;
switch (SnakeEngine.snake.direction) {
case 'up':
y = y - 1;
if (y < 0) {
y = 9;
}
break;
case 'down':
y = y + 1;
if (y > 9) {
y = 0;
}
break;
case 'left':
x = x - 1;
if (x < 0) {
x = 9;
}
break;
case 'right':
x = x + 1;
if (x > 9) {
x = 0;
}
break;
default:
}
const newSnakeHead = { x: x, y: y };
// newSnakeHead.y = y;
// newSnakeHead.x = x;
// console.log('Model x; ', x, 'Model y: ', y);
SnakeEngine.snake.body.push(newSnakeHead);
if (SnakeEngine.foodEaten() === false) {
SnakeEngine.snake.body.shift();
}
// this.UpdateBoard(oldTail.y, oldTail.x, SnakeEngine.board, false);
// this.UpdateBoard(y, x, SnakeEngine.board, true);
}

static controller() {
document.addEventListener('keydown', parseInput);
function parseInput(e: { key: string}) {
switch (e.key) {
case 'w':
case 'ArrowUp':
SnakeEngine.snake.direction = 'up';
break;
case 'a':
case 'ArrowLeft':
SnakeEngine.snake.direction = 'left';
break;
case 's':
case 'ArrowRight':
SnakeEngine.snake.direction = 'down';
break;
case 'd':
case 'ArrowDown':
SnakeEngine.snake.direction = 'right';
break;
default:
}
}
}

static foodEaten() {
for (const cell of SnakeEngine.snake.body) {
if (cell.x === SnakeEngine.food.x && cell.y === SnakeEngine.food.y) {
SnakeEngine.randomiseFood();
return true;
}
}
return false;
}

static randomiseFood() {
let valid = false;
while (valid === false) {
valid = true;
SnakeEngine.food.x = SnakeEngine.randomCoord();
SnakeEngine.food.y = SnakeEngine.randomCoord();
for (const cell of SnakeEngine.snake.body) {
if (cell.x === SnakeEngine.food.x && cell.y === SnakeEngine.food.y) {
valid = false;
}
}
}
}

static main() {
SnakeEngine.randomiseFood();
// console.log('Get input');
this.controller();
// this.newFrame();
this.view(SnakeEngine.board);

const interval = setInterval(this.newFrame, 500);
if (SnakeEngine.gameOver === true) {
clearInterval(interval);
}

return null;
}

static newFrame = () => {
this.model();
this.view(SnakeEngine.board);
}
}
SnakeEngine.main();