-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
177 lines (154 loc) · 4.37 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const { GRID_SIZE } = require('./constants');
module.exports = {
initGame,
gameLoop,
getUpdateVelocity,
}
// launching new game and generating random food
function initGame() {
const state = createGameState();
randomFood(state);
return state;
}
// positions and vehicle
function createGameState() {
return {
players: [{
pos: {
x: 3,
y: 10
},
vel: {
x: 1,
y: 0
},
snake: [
{x: 1, y: 10},
{x: 2, y: 10},
{x: 3, y: 10},
],
},{
pos: {
x: 18,
y: 10
},
vel: {
x: 0,
y: 0
},
snake: [
{x: 20, y: 10},
{x: 19, y: 10},
{x: 18, y: 10},
],
}],
food: {},
gridsize: GRID_SIZE,
active: true,
};
}
function gameLoop(state) {
if (!state) {
return;
}
const playerOne = state.players[0];
const playerTwo = state.players[1];
playerOne.pos.x += playerOne.vel.x;
playerOne.pos.y += playerOne.vel.y;
playerTwo.pos.x += playerTwo.vel.x;
playerTwo.pos.y += playerTwo.vel.y;
// snake died from hitting the wall
if (playerOne.pos.x < 0 || playerOne.pos.x > GRID_SIZE || playerOne.pos.y < 0 || playerOne.pos.y > GRID_SIZE) {
return 2;
}
if (playerTwo.pos.x < 0 || playerTwo.pos.x > GRID_SIZE || playerTwo.pos.y < 0 || playerTwo.pos.y > GRID_SIZE) {
return 1;
}
// snake eats food
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(state);
}
if (state.food.x === playerTwo.pos.x && state.food.y === playerTwo.pos.y){
playerTwo.snake.push({...playerTwo.pos});
playerTwo.pos.x += playerTwo.vel.x;
playerTwo.pos.y += playerTwo.vel.y;
randomFood(state);
}
// !
// playerOne
if (playerOne.vel.x || playerOne.vel.y) {
// snake bite itself
for (let cell of playerOne.snake) {
if (cell.x === playerOne.pos.x && cell.y === playerOne.pos.y) {
return 2;
}
}
//the first snake bite the second one
for (let cell of playerOne.snake) {
if (cell.x === playerTwo.pos.x && cell.y === playerTwo.pos.y) {
return 2;
}
}
// snake is moving
playerOne.snake.push({ ...playerOne.pos});
playerOne.snake.shift();
}
// playerTwo
if (playerTwo.vel.x || playerTwo.vel.y) {
// snake bite itself
for (let cell of playerTwo.snake) {
if (cell.x === playerTwo.pos.x && cell.y === playerTwo.pos.y) {
return 1;
}
}
//the second snake bite the first one
for (let cell of playerTwo.snake) {
if (cell.x === playerOne.pos.x && cell.y === playerOne.pos.y) {
return 1;
}
}
// snake is moving
playerTwo.snake.push({ ...playerTwo.pos});
playerTwo.snake.shift();
}
return false;
}
// food's appearing randomly
function randomFood(state) {
food = {
x: Math.floor(Math.random() * GRID_SIZE),
y: Math.floor(Math.random() * GRID_SIZE),
}
// food is not a part of a snake
for (let cell of state.players[0].snake) {
if (cell.x === food.x && cell.y === food.y) {
return randomFood(state);
}
}
for (let cell of state.players[1].snake) {
if (cell.x === food.x && cell.y === food.y) {
return randomFood(state);
}
}
state.food = food;
}
// snake is moving
function getUpdateVelocity(keyCode) {
switch (keyCode) {
case 37: { //left
return {x: -1, y: 0};
}
case 38: { //down
return {x: 0, y: -1};
}
case 39: { //right
return {x: 1, y: 0};
}
case 40: { //top
return {x: 0, y: 1};
}
}
}