-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
296 lines (235 loc) · 8.89 KB
/
script.js
File metadata and controls
296 lines (235 loc) · 8.89 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// PENDING BUG: sometimes snake disappears, need to solve - classList of undefined being accessed?
// often traced to eraseSnake, drawBlock, drawFood, etc.
const state = {
numCells: (600/40) * (600/40), // total number of grid cells: (#grid height / --cell-size) * (#grid width / --cell-size)
cells: [],
snakePos: [], // current cells occupied by snake
snakeDir: '', // current moving direction of snake
foodPos: 0, // current position of food
blockPos: [], // current cells occupied by procedurally generated obstacles/blocks
score: 0,
gameOver: false
}
const initGame = (element) => {
state.app = element; // element should be the top-level container (div)
drawStartButton();
drawHeader();
drawGrid();
drawScore();
const directions = ['left', 'right', 'up', 'down'];
state.snakePos[0] = getRandomPos(); // initial position of snake head, snakePos[0]
state.foodPos = getRandomPos();
state.snakeDir = directions[Math.floor(Math.random() * directions.length)];
drawSnake();
drawFood(state.foodPos);
}
const drawStartButton = () => {
const startButton = document.createElement('button');
startButton.setAttribute('type', 'button');
startButton.classList.add('button');
startButton.setAttribute('id', 'start');
const h1 = document.createElement('h1');
h1.innerText = "Start";
startButton.append(h1);
return startButton;
}
const drawRetryButton = () => {
const retryButton = document.createElement('button');
retryButton.setAttribute('type', 'button');
retryButton.classList.add('button');
retryButton.setAttribute('id', 'retry');
const h1 = document.createElement('h1');
h1.innerText = "Retry";
retryButton.append(h1);
return retryButton;
}
const drawHeader = () => {
const header = document.createElement('div');
header.setAttribute('id', 'header')
const h1 = document.createElement('h1');
h1.innerText = "Snake";
header.append(h1);
state.app.appendChild(header);
}
const drawGrid = () => {
const grid = document.createElement('div');
grid.setAttribute('id', 'grid');
for (let i = 0; i < state.numCells; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
grid.appendChild(cell);
state.cells.push(cell);
}
state.app.appendChild(grid);
startButton = drawStartButton();
grid.appendChild(startButton);
startButton.addEventListener('click', play)
}
const drawScore = () => {
const score = document.createElement('div');
score.setAttribute('id', 'score');
state.app.appendChild(score);
const h1 = document.createElement('h1');
h1.innerText = `Score: ${state.score}`;
score.append(h1);
}
const updateScore = () => {
score = document.getElementById('score');
score.innerHTML = ``;
const h1 = document.createElement('h1');
h1.innerText = `Score: ${++state.score}`;
score.append(h1);
}
const drawSnake = () => {
for (let i = 0; i < state.snakePos.length; i++) {
state.cells[state.snakePos[i]].classList.add('snake');
}
}
const eraseSnake = () => {
for (let i = 0; i < state.snakePos.length; i++) {
state.cells[state.snakePos[i]].classList.remove('snake');
}
}
const drawFood = (position) => {
state.foodPos = position;
state.cells[state.foodPos].classList.add('food');
}
const eraseFood = () => {
state.cells[state.foodPos].classList.remove('food');
}
const drawBlock = (position) => {
state.blockPos.push(position);
state.cells[position].classList.add('block');
}
const moveSnake = (direction) => {
switch (direction) {
case 'left':
if (state.snakePos[0] % Math.sqrt(state.numCells) === 0 // snake head hits wall in direction
|| state.snakePos.includes(state.snakePos[0] - 1) // snake collides with itself in direction
|| state.blockPos.includes(state.snakePos[0] - 1)) { // snake collides with block in direction
state.gameOver = true;
return;
} else state.snakePos.unshift(state.snakePos[0] - 1);
break;
case 'right':
if (state.snakePos[0] % Math.sqrt(state.numCells) === Math.sqrt(state.numCells) - 1 // "
|| state.snakePos.includes(state.snakePos[0] + 1) // "
|| state.blockPos.includes(state.snakePos[0] + 1)) { // "
state.gameOver = true;
return;
} else state.snakePos.unshift(state.snakePos[0] + 1);
break;
case 'up':
if (state.snakePos[0] <= Math.sqrt(state.numCells) - 1 // "
|| state.snakePos.includes(state.snakePos[0] - Math.sqrt(state.numCells)) // "
|| state.blockPos.includes(state.snakePos[0] - Math.sqrt(state.numCells))) { // "
state.gameOver = true;
return;
} else state.snakePos.unshift(state.snakePos[0] - Math.sqrt(state.numCells));
break;
case 'down':
if (state.snakePos[0] >= state.numCells - Math.sqrt(state.numCells) // "
|| state.snakePos.includes(state.snakePos[0] + Math.sqrt(state.numCells)) // "
|| state.blockPos.includes(state.snakePos[0] + Math.sqrt(state.numCells))) { // "
state.gameOver = true;
return;
} else state.snakePos.unshift(state.snakePos[0] + Math.sqrt(state.numCells));
break;
}
eraseSnake();
state.snakePos.pop();
drawSnake();
checkForFood();
}
const checkForFood = () => {
if (state.snakePos[0] === state.foodPos) {
eraseFood();
updateScore();
switch (state.snakeDir) { // add unit length to snake, depending on snake's current direction and tail position
case 'left':
state.snakePos.push(state.snakePos.at(-1) + 1);
break;
case 'right':
state.snakePos.push(state.snakePos.at(-1) - 1);
break;
case 'up':
state.snakePos.push(state.snakePos.at(-1) + Math.sqrt(state.numCells));
break;
case 'down':
state.snakePos.push(state.snakePos.at(-1) - Math.sqrt(state.numCells));
break;
}
let pos1;
do {pos1 = getRandomPos();} while (state.snakePos.includes(pos1) || state.blockPos.includes(pos1)); // no food appearing on snake or blocks
drawFood(pos1);
let pos2;
do {pos2 = getRandomPos();} while (state.snakePos.includes(pos2) || pos2 === pos1); // no blocks appearing on snake or food
drawBlock(pos2);
}
}
var getRandomPos = () => { // return random position in grid
return Math.floor(Math.random() * (state.numCells - 1));
}
const controlSnake = (event) => { // event handler for key press
if (state.gameOver) return; // do nothing
switch (event.code) {
case 'ArrowLeft':
if (state.snakeDir === 'right') return; // do nothing if snake is moving in the exact opposite direction; player must turn manually
else {
moveSnake('left');
state.snakeDir = 'left';
break;
}
case 'ArrowRight':
if (state.snakeDir === 'left') return; // "
else {
moveSnake('right');
state.snakeDir = 'right';
break;
}
case 'ArrowUp':
if (state.snakeDir === 'down') return; // "
else {
moveSnake('up');
state.snakeDir = 'up';
break;
}
case 'ArrowDown':
if (state.snakeDir === 'up') return; // "
else {
moveSnake('down');
state.snakeDir = 'down';
break;
}
}
}
const play = () => {
document.getElementById('start').style.visibility = 'hidden';
let interval;
interval = setInterval(() => {
if (state.gameOver) {
clearInterval(interval);
drawMessage('You have met your end.');
retryButton = drawRetryButton();
grid = document.getElementById('grid');
grid.removeChild(document.querySelector('#start'));
grid.appendChild(retryButton);
retryButton.addEventListener('click', reloadPage);
} else moveSnake(state.snakeDir);
}, 200); // adjusts speed of snake (hence difficulty of game)
window.addEventListener('keydown', controlSnake);
}
const reloadPage = () => {
window.location.reload();
}
const drawMessage = (text) => {
const message = document.createElement('div');
message.classList.add('message');
const h1 = document.createElement('h1');
h1.innerText = text;
message.append(h1);
state.app.append(message);
}
// --- main script ----
const appElement = document.getElementById('app');
initGame(appElement);