-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
212 lines (178 loc) · 4.6 KB
/
snake.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const DEBUG = true;
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Width/height of grid in block units.
const gridWidth = 50;
const gridHeight = 40;
// Width/height of block in pixels.
const blockDim = 10;
// Controls the speed of the game.
const speed = 100;
const keyCodes = {
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40
};
const initialState = {
isGameOver: false,
isPaused: false,
score: 0,
// List of blocks comprising the snake body.
snake: [3, 2, 1, 0].map(x => ({ x, y: gridHeight / 2, dir: keyCodes.RIGHT })),
pivots: {}
};
let state;
const resetState = () => {
state = JSON.parse(JSON.stringify(initialState));
document.getElementById('gameover').style.display = 'none';
};
const coordToIdx = (x, y) => y * gridWidth + x;
const idxToCoord = idx => ({
x: idx % gridWidth,
y: Math.floor(idx / gridWidth)
});
const drawBlock = (x, y, color) => {
ctx.beginPath();
ctx.rect(x * blockDim, y * blockDim, blockDim, blockDim);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
};
const drawSnake = () => {
const { snake } = state;
snake.forEach(({ x, y }) => drawBlock(x, y, 'blue'));
};
const updateSnake = () => {
const { interval, food, snake, pivots } = state;
const translate = {
[keyCodes.LEFT]: ({ x, y, dir }) => ({ x: x - 1, y, dir }),
[keyCodes.RIGHT]: ({ x, y, dir }) => ({ x: x + 1, y, dir }),
[keyCodes.DOWN]: ({ x, y, dir }) => ({ x, y: y + 1, dir }),
[keyCodes.UP]: ({ x, y, dir }) => ({ x, y: y - 1, dir })
};
const grow = {
[keyCodes.LEFT]: translate[keyCodes.RIGHT],
[keyCodes.RIGHT]: translate[keyCodes.LEFT],
[keyCodes.DOWN]: translate[keyCodes.UP],
[keyCodes.UP]: translate[keyCodes.DOWN]
};
const newSnake = snake.map(block => {
const { x, y } = block;
const dir = pivots[coordToIdx(x, y)] || block.dir;
return translate[dir]({ x, y, dir });
});
// Boundary detection.
const { x, y } = newSnake[0];
if (x < 0
|| y < 0
|| x >= gridWidth
|| y >= gridHeight
// Self intersection
|| newSnake.find(({ x: ox, y: oy }, idx) => idx !== 0 && x === ox && y === oy)) {
endGame();
}
if (x === food.x && y === food.y) {
const tail = newSnake[newSnake.length - 1];
newSnake.push(grow[tail.dir](tail));
state.score++;
updateScore();
spawnFood();
}
updatePivots();
state.snake = newSnake;
};
const updateScore = () => {
document.getElementById('score').textContent = state.score;
};
const spawnFood = () => {
let x, y;
const { snake } = state;
do {
x = Math.floor(Math.random() * gridWidth);
y = Math.floor(Math.random() * gridHeight);
} while (!!snake.find(({ x: ox, y: oy }) => x === ox && y === oy));
state.food = { x, y };
};
const updatePivots = () => {
const { pivots, snake } = state;
// Remove the pivot after the last block of the snake has passed thru it.
const { x, y } = snake[snake.length - 1];
const idx = coordToIdx(x, y);
if (pivots[idx]) {
delete pivots[idx];
}
};
const drawPivots = () => {
const { pivots } = state;
Object.keys(pivots).forEach(idx => {
const { x, y } = idxToCoord(idx);
drawBlock(x, y, 'red');
});
};
const drawFood = () => {
const { food: { x, y } } = state;
drawBlock(x, y, 'blue');
};
const draw = () => {
// console.log('BEFORE DRAW:', snake);
drawSnake();
drawFood();
DEBUG && drawPivots();
};
const update = () => {
updateSnake();
};
const loop = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
draw();
update();
};
const resumeGame = () => {
state.isPaused = false;
state.interval = setInterval(loop, speed);
document.getElementById('paused').style.display = 'none';
};
const pauseGame = () => {
state.isPaused = true;
clearInterval(state.interval);
document.getElementById('paused').style.display = 'block';
};
const endGame = () => {
state.isGameOver = true;
clearInterval(state.interval);
document.getElementById('gameover').style.display = 'block';
};
const init = () => {
resetState();
updateScore();
spawnFood();
resumeGame();
};
canvas.style.backgroundColor = 'rgba(211, 211, 211, 1)';
init();
document.addEventListener('keydown', ({ keyCode }) => {
switch (keyCode) {
case keyCodes.SPACE:
if (state.isGameOver) {
init();
break;
}
if (!state.isPaused) {
pauseGame();
} else {
resumeGame();
}
break;
case keyCodes.DOWN:
case keyCodes.LEFT:
case keyCodes.RIGHT:
case keyCodes.UP: {
const { pivots, snake } = state;
const { x, y } = snake[0];
pivots[coordToIdx(x, y)] = keyCode;
break;
}
}
});