-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
391 lines (317 loc) · 8.66 KB
/
script.js
File metadata and controls
391 lines (317 loc) · 8.66 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
const LEFT = "LEFT";
const RIGHT = "RIGHT";
const UP = "UP";
const DOWN = "DOWN";
const speedInMs = 250;
const playground = document.getElementById("playground");
const container = document.getElementById("container");
const snake = document.getElementById("snake");
const score = document.getElementById("score");
const highscore = document.getElementById("highscore");
const dimensionOfSquare = playground.clientWidth / 17;
let timeSinceLastMove;
let apple_interval;
let timeouts = [];
let apple;
let snakeTail;
let snakeTails = [];
let secondSnakeTail;
let movingDirection = RIGHT;
let prevPositions = [
{
left: snake.getBoundingClientRect().left,
top: 10 * dimensionOfSquare,
},
];
let highscoreNumber = 0;
let foundApples = 0;
let snakeSize = 3;
document.addEventListener("DOMContentLoaded", () => {
console.log("DOM fully loaded and parsed");
score.innerText = "Apples found: " + foundApples;
highscore.innerText = "Highscore: " + highscoreNumber;
changeSnakeSize();
createSquares();
spawnApple();
snakeRight();
});
document.addEventListener("keydown", (e) => {
const key = e.key;
switch (key) {
case "d":
snakeRight();
break;
case "a":
snakeLeft();
break;
case "w":
snakeUp();
break;
case "s":
snakeDown();
break;
}
});
function inBound(a, b, bound) {
return (a - bound < b && a + bound > b) || (b - bound < a && b + bound > a);
}
function isMoveDownSlowEnoguh() {
return Date.now() - timeSinceLastMove >= speedInMs;
}
function checkIfAppleFound() {
if (!apple) return;
if (isAppleFound()) {
snakeSize++;
foundApples++;
if (foundApples > highscoreNumber) {
highscoreNumber = foundApples;
highscore.innerText = "Highscore: " + highscoreNumber;
}
score.innerText = "Apples found: " + foundApples;
spawnApple();
}
}
function isAppleFound() {
return (
inBound(
snake.getBoundingClientRect().left,
apple.getBoundingClientRect().left,
dimensionOfSquare / 3
) &&
inBound(
snake.getBoundingClientRect().top,
apple.getBoundingClientRect().top,
dimensionOfSquare / 3
)
);
}
function clearTimeouts() {
timeouts.forEach(clearTimeout);
timeouts = [];
}
function renderTail() {
for (let i = 1; i < prevPositions.length - 1; i++) {
renderSingleTail(i);
}
}
function removeSnakeTails() {
for (let i = 1; i < prevPositions.length; i++) {
if (snakeTails[i]) snakeTails[i].remove();
}
snakeTails = [];
prevPositions = [];
}
function renderSingleTail(index) {
if (snakeTails[index]) snakeTails[index].remove();
snakeTails[index] = document.createElement("div");
console.log((index % 2) * 0.1 + 0.452);
snakeTails[index].style.backgroundColor =
"hsla(260, 75%, 50%, " + ((index % 2) * 0.05 + 0.452) + ")";
snakeTails[index].style.top = snake.getBoundingClientRect().top + "px";
snakeTails[index].style.left = snake.getBoundingClientRect().left + "px";
snakeTails[index].style.width = dimensionOfSquare - 0.4 + "px";
snakeTails[index].style.height = dimensionOfSquare - 0.4 + "px";
snakeTails[index].classList.add("tempSnake");
container.appendChild(snakeTails[index]);
snakeTails[index].style.left = prevPositions[index].left + "px";
snakeTails[index].style.top = prevPositions[index].top + "px";
}
function snakeRight() {
clearTimeouts();
let interval = speedInMs - (Date.now() - timeSinceLastMove);
if (isMoveDownSlowEnoguh()) {
interval = 0;
}
const timeout_id = setTimeout(() => {
onceRight();
snakeRight();
if (
snake.getBoundingClientRect().right >=
playground.getBoundingClientRect().right
) {
fail();
return;
}
}, interval);
timeouts.push(timeout_id);
}
function onceRight() {
const snakePos = snake.getBoundingClientRect().left;
snake.style.left = snakePos + dimensionOfSquare + "px";
newMove();
}
function snakeLeft() {
clearTimeouts();
let interval = speedInMs - (Date.now() - timeSinceLastMove);
if (isMoveDownSlowEnoguh()) {
interval = 0;
}
const timeout_id = setTimeout(() => {
onceLeft();
snakeLeft();
if (
snake.getBoundingClientRect().left + dimensionOfSquare <=
playground.getBoundingClientRect().left
) {
fail();
return;
}
}, interval);
timeouts.push(timeout_id);
}
function onceLeft() {
const snakePos = snake.getBoundingClientRect().left;
snake.style.left = snakePos - dimensionOfSquare + "px";
newMove();
}
function snakeUp() {
clearTimeouts();
let interval = speedInMs - (Date.now() - timeSinceLastMove);
if (isMoveDownSlowEnoguh()) {
interval = 0;
}
const timeout_id = setTimeout(() => {
onceUp();
snakeUp();
if (
snake.getBoundingClientRect().top + dimensionOfSquare <=
playground.getBoundingClientRect().top
) {
fail();
return;
}
}, interval);
timeouts.push(timeout_id);
}
function onceUp() {
const snakePos = snake.getBoundingClientRect().top;
snake.style.top = snakePos - dimensionOfSquare + "px";
newMove();
}
function newMove() {
timeSinceLastMove = Date.now();
checkIfAppleFound();
isSnakeColidingWithBody();
addSnakePrev();
}
function snakeDown() {
clearTimeouts();
let interval = speedInMs - (Date.now() - timeSinceLastMove);
if (isMoveDownSlowEnoguh()) {
interval = 0;
}
const timeout_id = setTimeout(() => {
onceDown();
snakeDown();
if (
snake.getBoundingClientRect().bottom >=
playground.getBoundingClientRect().bottom
) {
fail();
return;
}
}, interval);
timeouts.push(timeout_id);
}
function onceDown() {
const snakePos = snake.getBoundingClientRect().top;
snake.style.top = snakePos + dimensionOfSquare + "px";
newMove();
}
function getRandomInt(max) {
return Math.floor(Math.random() * max) + 1;
}
function spawnApple() {
if (apple) apple.remove();
if (apple_interval) clearInterval(apple_interval);
const leftBound = playground.getBoundingClientRect().left;
const topBound = playground.getBoundingClientRect().top;
//
apple = document.createElement("img");
// apple = document.createElement("div");
apple.src = "./apple.png";
apple.style.height = dimensionOfSquare + 5 + "px";
apple.style.width = dimensionOfSquare + 5 + "px";
apple.classList.add("apple");
container.append(apple);
apple_interval = setInterval(() => {
apple.style.transform = "scale(1.5)"; // Enlarge for 500ms
setTimeout(() => {
apple.style.transform = "scale(1)"; // Reset after 500ms
}, 500);
}, 2000); // Repeat every 2 seconds
let index = 0;
let left;
let top;
do {
if (index > 1000) {
console.log("Reached 1000 loops in do-while");
break;
}
left = leftBound + getRandomInt(16) * dimensionOfSquare + "px";
top = topBound + getRandomInt(16) * dimensionOfSquare + "px";
console.log(left);
index++;
} while (
prevPositions.some(
(pos) =>
Math.round(left) === Math.round(pos.left) ||
Math.round(top) === Math.round(pos.top)
)
);
apple.style.left = left;
apple.style.top = top;
}
function addSnakePrev() {
if (prevPositions.length > snakeSize) {
prevPositions.shift();
}
prevPositions.push({
left: snake.getBoundingClientRect().left,
top: snake.getBoundingClientRect().top,
});
renderTail();
}
function isSnakeColidingWithBody() {
const snakeLeft = snake.getBoundingClientRect().left;
const snakeTop = snake.getBoundingClientRect().top;
for (let i = 1; i < prevPositions.length; i++) {
if (
inBound(prevPositions[i].left, snakeLeft, dimensionOfSquare / 5) &&
inBound(prevPositions[i].top, snakeTop, dimensionOfSquare / 5)
) {
fail();
console.log("Touching");
}
}
}
function fail() {
snakeSize = 3;
foundApples = 0;
removeSnakeTails();
score.innerText = "Apples found: " + foundApples;
clearTimeouts();
snake.style.top =
playground.getBoundingClientRect().top + 7 * dimensionOfSquare + "px";
snake.style.left = playground.getBoundingClientRect().left + "px";
snakeRight();
if (apple) apple.remove();
spawnApple();
}
function changeSnakeSize() {
snake.style.width = dimensionOfSquare + "px";
snake.style.height = dimensionOfSquare + "px";
snake.style.top =
playground.getBoundingClientRect().top + 7 * dimensionOfSquare + "px";
}
function createSquares() {
for (let i = 0; i < 17 * 17; i++) {
const sqaure = document.createElement("div");
sqaure.classList.add("square");
sqaure.style.width = dimensionOfSquare + "px";
sqaure.style.height = dimensionOfSquare + "px";
if (i % 2 === 0) sqaure.style.backgroundColor = "#aad751";
else sqaure.style.backgroundColor = "#a2d149";
playground.appendChild(sqaure);
}
}