-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
442 lines (374 loc) · 13 KB
/
snake.js
File metadata and controls
442 lines (374 loc) · 13 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
let STARTX = 9;
let STARTY = 9;
let STARTLINGER = 1;
let STARTDIR = 3;
let headX = STARTX;
let headY = STARTY;
let boardXSize = 20;
let boardYSize = 20;
let grid = [];
let framesPerTick = 25;
let linger = STARTLINGER; // The amount of time (ticks) that a tail block will linger on the screen
let dir = STARTDIR;
let dirMap = [moveRight, moveLeft, moveDown, moveUp];
let newFoodScore = 0;
let gameStartTime = 0;
// common size for blocks
let blockWidth = 20;
let blockHeight= 20;
let gridXStart = blockWidth * 1;
let gridYStart = blockHeight * 1;
let running = false;
// The time that it takes for the next frame to be created
let frametime = 1;
// Used for making sure the user can't kill themselves by accident using controls that shouldn't be allowed
let controlTimeout = 20;
let framesDone = 0;
// This could use some adjustment for gameplay experience but generally fine
let CHANGEDIRTIME = framesPerTick;
let dirCountdown = CHANGEDIRTIME;
// Canvases
let gridC = document.getElementById("grid").getContext("2d");
let thisBlockC = document.getElementById("currentblock").getContext("2d");
let backgroundC = document.getElementById("background").getContext("2d");
// Size of the black background
let blackBackX = blockWidth * boardXSize;
let blackBackY = blockHeight * boardYSize;
// Size of the gray background
let grayBackX = blockWidth * (boardXSize + 2);
let grayBackY = blockHeight * (boardYSize + 2);
let slowified = 0;
let fastified = 0;
// All the different types of powers
let powers = [0, 1, 2];
let powersToColors = ["red", "blue", "yellow"];
let powersToFunctions = [makeFast, makeSlow, noOp];
function noOp() {}
function makeSlow() {
fastified = 0;
framesPerTick = 50;
slowified = 20;
}
function makeFast() {
slowified = 0;
framesPerTick = 12;
fastified = 20;
}
function BodyCell(inX, inY, timeout, inPower) {
this.x = inX;
this.y = inY;
this.timeLeft = timeout;
this.power = inPower;
}
let bodyCells = [];
let foodCells = [];
// Initialize the grid to be of the right size, filled with zeros
function constructEmptyGrid() {
grid = [];
for (let i = 0; i < boardXSize; i++) {
let col = [];
for(let j = 0; j < boardYSize; j++) {
col.push(0);
}
grid.push(col);
}
}
// Move character right, including looping to the left side if necessary
function moveRight() {
if (headX + 1 === boardXSize) {
headX = 0;
} else {
headX = headX + 1;
}
}
// Move character left, including looping to the right side if necessary
function moveLeft() {
if (headX - 1 < 0) {
headX = boardXSize - 1;
} else {
headX = headX - 1;
}
}
// Move character down, including looping to the top side if necessary
function moveDown() {
if (headY + 1 === boardYSize) {
headY = 0;
} else {
headY = headY + 1;
}
}
// Move character up, including looping to the bottom side if necessary
function moveUp() {
if (headY - 1 < 0) {
headY = boardYSize - 1;
} else {
headY = headY - 1;
}
}
function doMovement() {
dirMap[dir]();
}
function setDir(newDir) {
dir = newDir;
}
// Puts self onto board, then sets something to remove it from the board in a set amount of time
function materializeSelf(x, y) {
let block = new BodyCell(x, y, linger);
grid[block.x][block.y] = 1;
bodyCells.push(block);
}
function drawBackground() {
backgroundC.fillStyle = "gray";
backgroundC.fillRect(0, 0, grayBackX, grayBackY);
backgroundC.fillStyle = "black";
backgroundC.fillRect(blockWidth, blockHeight, blackBackX, blackBackY);
}
// TEST FUNCTION?
function drawHead() {
drawBlock(headX, headY, "green");
}
function drawBody() {
for (let i = 0; i < bodyCells.length; i++) {
drawBlock(bodyCells[i].x, bodyCells[i].y, "green");
}
}
function drawFood() {
for (let i = 0; i < foodCells.length; i++) {
drawBlock(foodCells[i].x, foodCells[i].y, powersToColors[foodCells[i].power]);
}
}
// Draws a single block on the grid, given coordinates and a main color
function drawBlock(x, y, mainColor) {
thisBlockC.fillStyle = "white";
thisBlockC.fillRect(gridXStart + (x * blockWidth), gridYStart + (y * blockHeight), blockWidth, blockHeight);
thisBlockC.fillStyle = mainColor;
thisBlockC.fillRect(gridXStart + (x * blockWidth) + 1, gridYStart + (y * blockHeight) + 1, blockWidth - 2, blockHeight - 2);
}
function makeBody() {
materializeSelf(headX, headY);
}
function isBodyCollide() {
for (let i = 0; i < bodyCells.length; i++) {
if (bodyCells[i].x === headX && bodyCells[i].y === headY) {
return true;
}
}
return false;
}
function isFoodCollide() {
for (let i = 0; i < foodCells.length; i++) {
if (foodCells[i].x === headX && foodCells[i].y === headY) {
return foodCells[i];
}
}
return false;
}
function setOriginalCSS() {
let up = document.getElementById("up");
let down = document.getElementById("down");
let left = document.getElementById("left");
let right = document.getElementById("right");
let start = document.getElementById("start");
start.style.display = "";
up.classList.remove("invisiblebutton")
down.classList.remove("invisiblebutton")
left.classList.remove("invisiblebutton")
right.classList.remove("invisiblebutton")
up.classList.add("gamebuton");
down.classList.add("gamebuton");
left.classList.add("gamebuton");
right.classList.add("gamebuton");
up.classList.add("upcolor");
down.classList.add("downcolor");
left.classList.add("leftcolor");
right.classList.add("rightcolor");
}
function setGameplayCSS() {
let up = document.getElementById("up");
let down = document.getElementById("down");
let left = document.getElementById("left");
let right = document.getElementById("right");
let start = document.getElementById("start");
up.classList.remove("gamebuton");
down.classList.remove("gamebuton");
left.classList.remove("gamebuton");
right.classList.remove("gamebuton");
up.classList.remove("upcolor");
down.classList.remove("downcolor");
left.classList.remove("leftcolor");
right.classList.remove("rightcolor");
up.classList.add("invisiblebutton")
down.classList.add("invisiblebutton")
left.classList.add("invisiblebutton")
right.classList.add("invisiblebutton")
start.style.display = "none";
}
function gameOver() {
running = false;
document.getElementById("score").innerText = 0;
headX = STARTX;
headY = STARTY;
linger = STARTLINGER;
dir = STARTDIR;
slowified = 0;
fastified = 0;
framesPerTick = 25;
foodCells = [];
bodyCells = [];
thisBlockC.clearRect(0, 0, thisBlockC.canvas.width, thisBlockC.canvas.height);
setOriginalCSS();
constructEmptyGrid();
setLocalHighScore();
newFoodScore = 0;
}
function setLocalHighScore() {
let localFoodScore = localStorage.getItem("newFoodScore");
if (localFoodScore !== null) {
if (localFoodScore < newFoodScore) {
localStorage.setItem("newFoodScore", newFoodScore);
document.getElementById("highscore").innerText = newFoodScore;
}
} else {
localStorage.setItem("newFoodScore", newFoodScore);
document.getElementById("highscore").innerText = newFoodScore;
}
let newTimeScore = ((new Date).getTime() - gameStartTime) / 1000;
let localTimeScore = localStorage.getItem("timeScore");
if (localTimeScore !== null) {
if (localTimeScore < newTimeScore) {
localStorage.setItem("timeScore", newTimeScore);
document.getElementById("besttime").innerText = newTimeScore;
}
} else {
localStorage.setItem("timeScore", newTimeScore);
document.getElementById("besttime").innerText = newTimeScore;
}
}
function spawnNewFood() {
// Get every coordinate that doesn't already have an element in it
let allCoords = [];
// Choose the correct power to give this food
let chosenPower = powers[Math.floor(Math.random() * powers.length)];
let cellsToAvoid = [];
for (let i = 0; i < bodyCells.length; i++) {
cellsToAvoid.push(new BodyCell(bodyCells[i].x, bodyCells[i].y, 0));
}
cellsToAvoid.push(new BodyCell(headX, headY, 0));
for (let i = 0; i < boardXSize; i++) {
for (let j = 0; j < boardYSize; j++) {
allCoords.push(new BodyCell(i, j, 0, chosenPower));
}
}
allCoords = allCoords.filter((item) => {
for (let i = 0; i < cellsToAvoid.length; i++) {
if (item.x === cellsToAvoid[i].x && item.y === cellsToAvoid[i].y) {
return false;
}
}
return true;
}
);
// Randomly choose one of them for adding the food to
foodCells.push(allCoords[Math.floor(Math.random() * allCoords.length)]);
}
let SLEEPMAGIC = 1;
window.onkeydown = function(e) {
if (e.code === "ArrowUp" || e.code === "KeyW") { // up
if (dir !== 2) {
setDir(3);
}
} else if (e.code === "ArrowDown" || e.code === "KeyS") { // down
if (dir !== 3) {
setDir(2);
}
} else if (e.code === "ArrowRight" || e.code === "KeyD") { // right
if (dir !== 1) {
setDir(0);
}
} else if (e.code === "ArrowLeft" || e.code === "KeyA") { // left
if (dir !== 0) {
setDir(1);
}
}
};
// change the direction of the player, keeping in mind that
// the change shouldn't happen immediately after another one
function setDir(newDir) {
if (dirCountdown <= 0) {
dir = newDir;
dirCountdown = CHANGEDIRTIME;
}
}
window.onload = function() {
constructEmptyGrid();
drawBackground();
document.getElementById("right").onclick = function() {if (dir !== 1) {setDir(0)}};
document.getElementById("left").onclick = function() {if (dir !== 0) {setDir(1)}};
document.getElementById("down").onclick = function() {if (dir !== 3) {setDir(2)}};
document.getElementById("up").onclick = function() {if (dir !== 2) {setDir(3)}};
document.getElementById("start").onclick = function() {running = true; setGameplayCSS(); gameStartTime = (new Date).getTime()};
let localFoodScore = localStorage.getItem("newFoodScore");
if (localFoodScore !== null) {
document.getElementById("highscore").innerText = localFoodScore;
} else {
document.getElementById("highscore").innerText = 0;
}
let highTimeScore = localStorage.getItem("timeScore");
if (localFoodScore !== null) {
document.getElementById("besttime").innerText = highTimeScore;
} else {
document.getElementById("besttime").innerText = "0.0";
}
setInterval(function() {
if (running) {
dirCountdown -= 1;
framesDone += 1;
if (framesDone === framesPerTick) {
if (foodCells.length === 0) {
spawnNewFood();
}
if (fastified > 0) {
fastified--;
if (fastified === 0) {
framesPerTick = 25;
}
}
if (slowified > 0) {
slowified--;
if (slowified === 0) {
framesPerTick = 25;
}
}
framesDone = 0;
makeBody();
doMovement();
let cellsToKill = [];
for (let i = 0; i < bodyCells.length; i++) {
bodyCells[i].timeLeft = bodyCells[i].timeLeft - 1;
if (bodyCells[i].timeLeft === 0) {
cellsToKill.push(bodyCells[i]);
grid[bodyCells[i].x][bodyCells[i].y] = 0;
}
}
bodyCells = bodyCells.filter((item) => !cellsToKill.includes(item));
if (isBodyCollide()) {
gameOver();
return;
}
let colFood = isFoodCollide();
if (colFood !== false) {
powersToFunctions[colFood.power]();
newFoodScore++;
document.getElementById("score").innerText = newFoodScore;
linger++;
foodCells = foodCells.filter((item) => item !== colFood);
spawnNewFood();
}
thisBlockC.clearRect(0, 0, thisBlockC.canvas.width, thisBlockC.canvas.height);
drawHead();
drawBody();
drawFood();
}
}
}, frametime);
};