-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
76 lines (67 loc) · 1.66 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const {GRID_SIZE} = require ('./constants');
module.exports = {
createGameState,gameLoop,
}
function createGameState(){
return {
player: {
pos:{
x:3,
y:10,
},
vel:{
x:1,
y:0,
},
snake:[
{x:1,y:10},
{x:2,y:10},
{x:3,y:10},
],
},
food:{
x:7,
y:7,
},
gridsize: GRID_SIZE,
};
}
function gameLoop(state) {
if (!state){
return;
}
const playerOne = state.player;
playerOne.pos.x +=playerOne.vel.x;
playerOne.pos.y +=playerOne.vel.y;
if (playerOne.pos.x<0 || playerOne.pos.x > GRID_SIZE || playerOne.pos.y<0 || playerOne.pos.y>GRID_SIZE){
return 2;
}
if (state.food.x === playerOne.pos.x && state.food.y===playerOne.pos.y){
playerOne.snake.push({ ...playerOne.pos});
playerOne.pos.x +=playerOne.vel.x;
playerOne.pos.y +=playerOne.vel.y;
randomFood();
}
if (playerOne.vel.x || playerOne.vel.y){
for(let cell of playerOne.snake){
if (cell.x===playerOne.pos.x && cell.y ===playerOne.pos.y){
return 2;
}
}
playerOne.snake.push({...playerOne.pos});
playerOne.snake.shift();
}
return false; //no winner
}
function randomFood(state) {
food ={
x: Math.floor(Math.random() * GRID_SIZE),
y: Math.floor(Math.random() * GRID_SIZE),
}
for (let cell of state.player.snake){
if (cell.x===food.x && cell.y === food.y){
return ransomFood(state);
}
}
state.food = food;
}