-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
360 lines (278 loc) · 10.8 KB
/
Copy pathindex.js
File metadata and controls
360 lines (278 loc) · 10.8 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
var score = 0;
var intervalSpeed = 600; // 600 is default, for future increased difficulty
var snakePreviousTailPosition;
var snakeNextTailPosition;
var snakeNextHeadPosition;
var randomFruitPosition;
var sensHead = "right";
var sensTail = "right";
var previousSens;
var turnPositions = [];
var snakeArray = [];
const $divdiv = document.querySelectorAll("div div");
const $score = document.querySelector("h2");
function random300() {
var n = Math.floor(Math.random() * 301);
return n;
}
function createAndRenderFruit() {
/* TODO */
}
function renderSnakeNextTurn() {
/* TODO */
}
document.addEventListener("DOMContentLoaded", () => {
/* SNAKE START POSITIONS */
const snakeTail = document.querySelector("#grid div:nth-child(142)");
const snakeBody1 = document.querySelector("#grid div:nth-child(143)");
const snakeBody2 = document.querySelector("#grid div:nth-child(144)");
const snakeBody3 = document.querySelector("#grid div:nth-child(145)");
const snakeHead = document.querySelector("#grid div:nth-child(146)");
$divdiv.forEach(element => {
element.style.backgroundColor = "white";
});
snakeTail.style.backgroundColor = "yellow";
snakeTail.style.opacity = "1";
snakeBody1.style.backgroundColor = "green";
snakeBody1.style.opacity = "1";
snakeBody2.style.backgroundColor = "green";
snakeBody2.style.opacity = "1";
snakeBody3.style.backgroundColor = "green";
snakeBody3.style.opacity = "1";
snakeHead.style.backgroundColor = "red";
snakeHead.style.opacity = "1";
randomFruitPosition = $divdiv[random300()];
while (randomFruitPosition.style.backgroundColor !== "white") {
randomFruitPosition = $divdiv[random300()];
}
randomFruitPosition.style.backgroundColor = "orange";
randomFruitPosition.style.opacity = "1";
/* KEYBOARD CONTROLS */
document.addEventListener("keydown", keyboard => {
if (keyboard.key === "ArrowUp" && previousSens !== "down") {
sensHead = "up";
} else if (keyboard.key === "ArrowDown" && previousSens !== "up") {
sensHead = "down";
} else if (keyboard.key === "ArrowLeft" && previousSens !== "right") {
sensHead = "left";
} else if (keyboard.key === "ArrowRight" && previousSens !== "left") {
sensHead = "right";
}
});
/* FRUIT POP UP */
setInterval(() => {
randomFruitPosition = $divdiv[random300()];
while (randomFruitPosition.style.backgroundColor !== "white") {
randomFruitPosition = $divdiv[random300()];
}
randomFruitPosition.style.backgroundColor = "orange";
randomFruitPosition.style.opacity = "1";
}, intervalSpeed * 10);
/* SNAKE MOVING */
setInterval(() => {
/* CHECKING CURRENT SNAKE POSITION AND RULES ABOUT NEXT HEAD / BODY / TAIL POSITION + MOVEMENT */
var i = 0;
$divdiv.forEach(element => {
i++;
const $BoxDown = $divdiv[i + 19];
const $BoxUp = $divdiv[i - 21];
const $BoxLeft = $divdiv[i - 2];
const $BoxRight = $divdiv[i];
const $BoxLeftScreen = $divdiv[i - 20];
const $BoxRightScreen = $divdiv[i + 18];
const $BoxBottomScreen = $divdiv[i + 279];
const $BoxTopScreen = $divdiv[i - 281];
/* WHAT TO DO IN THE LOOP IF THE ELEMENT IS BODY / GREEN */
if (element.style.backgroundColor === "green") {
snakeArray.push(element);
}
/* WHAT TO DO IN THE LOOP IF THE ELEMENT IS TAIL / YELLOW */
if (element.style.backgroundColor === "yellow") {
snakeArray.push(element);
snakePreviousTailPosition = element;
if (turnPositions[0] === $BoxUp && $BoxUp !== undefined) {
snakeNextTailPosition = $BoxUp;
sensTail = "up";
} else if (turnPositions[0] === $BoxDown && $BoxDown !== undefined) {
snakeNextTailPosition = $BoxDown;
sensTail = "down";
} else if (turnPositions[0] === $BoxLeft && $BoxLeft !== undefined) {
snakeNextTailPosition = $BoxLeft;
sensTail = "left";
} else if (turnPositions[0] === $BoxRight && $BoxRight !== undefined) {
snakeNextTailPosition = $BoxRight;
sensTail = "right";
} else if (turnPositions[0] === $BoxBottomScreen && $BoxBottomScreen !== undefined) {
snakeNextTailPosition = $BoxBottomScreen;
} else if (turnPositions[0] === $BoxTopScreen && $BoxTopScreen !== undefined) {
snakeNextTailPosition = $BoxTopScreen;
} else if (turnPositions[0] === $BoxLeftScreen && $BoxLeftScreen !== undefined) {
snakeNextTailPosition = $BoxLeftScreen;
} else if (turnPositions[0] === $BoxRightScreen && $BoxRightScreen !== undefined) {
snakeNextTailPosition = $BoxRightScreen;
} else {
if (sensTail === "up") {
if ($BoxUp === undefined) {
snakeNextTailPosition = $BoxBottomScreen;
} else {
snakeNextTailPosition = $BoxUp;
}
} else if (sensTail === "down") {
if ($BoxDown === undefined) {
snakeNextTailPosition = $BoxTopScreen;
} else {
snakeNextTailPosition = $BoxDown;
}
} else if (sensTail === "right") {
if (element === $divdiv[19] ||
element === $divdiv[39] ||
element === $divdiv[59] ||
element === $divdiv[79] ||
element === $divdiv[99] ||
element === $divdiv[119] ||
element === $divdiv[139] ||
element === $divdiv[159] ||
element === $divdiv[179] ||
element === $divdiv[199] ||
element === $divdiv[219] ||
element === $divdiv[239] ||
element === $divdiv[259] ||
element === $divdiv[279] ||
element === $divdiv[299]) {
snakeNextTailPosition = $BoxLeftScreen;
} else {
snakeNextTailPosition = $BoxRight;
}
} else if (sensTail === "left") {
if (element === $divdiv[0] ||
element === $divdiv[20] ||
element === $divdiv[40] ||
element === $divdiv[60] ||
element === $divdiv[80] ||
element === $divdiv[100] ||
element === $divdiv[120] ||
element === $divdiv[140] ||
element === $divdiv[160] ||
element === $divdiv[180] ||
element === $divdiv[200] ||
element === $divdiv[220] ||
element === $divdiv[240] ||
element === $divdiv[260] ||
element === $divdiv[280]) {
snakeNextTailPosition = $BoxRightScreen;
} else {
snakeNextTailPosition = $BoxLeft;
}
}
}
}
/* WHAT TO DO IN THE LOOP IF THE ELEMENT IS HEAD / RED */
if (element.style.backgroundColor === "red") {
snakeArray.push(element);
snakeHeadPosition = element;
if (sensHead === "right") {
if (element === $divdiv[19] ||
element === $divdiv[39] ||
element === $divdiv[59] ||
element === $divdiv[79] ||
element === $divdiv[99] ||
element === $divdiv[119] ||
element === $divdiv[139] ||
element === $divdiv[159] ||
element === $divdiv[179] ||
element === $divdiv[199] ||
element === $divdiv[219] ||
element === $divdiv[239] ||
element === $divdiv[259] ||
element === $divdiv[279] ||
element === $divdiv[299]) {
snakeNextHeadPosition = $BoxLeftScreen;
} else {
snakeNextHeadPosition = $BoxRight;
}
} else if (sensHead === "left") {
if (element === $divdiv[0] ||
element === $divdiv[20] ||
element === $divdiv[40] ||
element === $divdiv[60] ||
element === $divdiv[80] ||
element === $divdiv[100] ||
element === $divdiv[120] ||
element === $divdiv[140] ||
element === $divdiv[160] ||
element === $divdiv[180] ||
element === $divdiv[200] ||
element === $divdiv[220] ||
element === $divdiv[240] ||
element === $divdiv[260] ||
element === $divdiv[280]) {
snakeNextHeadPosition = $BoxRightScreen;
} else {
snakeNextHeadPosition = $BoxLeft;
}
} else if (sensHead === "up") {
if ($BoxUp === undefined) {
snakeNextHeadPosition = $BoxBottomScreen;
} else {
snakeNextHeadPosition = $BoxUp;
}
} else if (sensHead === "down") {
if ($BoxDown === undefined) {
snakeNextHeadPosition = $BoxTopScreen;
} else {
snakeNextHeadPosition = $BoxDown;
}
}
turnPositions.push(snakeNextHeadPosition);
}
});
/* MOVING RENDER : TODO : FUNCTION RENDER */
if (snakeNextHeadPosition.style.backgroundColor !== "orange") {
snakePreviousTailPosition.style.backgroundColor = "white";
snakePreviousTailPosition.style.opacity = "0.7";
snakeNextTailPosition.style.backgroundColor = "yellow";
snakeNextTailPosition.style.opacity = "1";
if (snakeNextTailPosition === turnPositions[0]) {
turnPositions.splice(0, 1);
}
} else if (snakeNextHeadPosition.style.backgroundColor === "orange") {
score += 100;
$score.textContent = "Score : " + score;
}
if (snakeNextHeadPosition.style.backgroundColor !== "green" &&
snakeNextHeadPosition.style.backgroundColor !== "yellow") {
snakeHeadPosition.style.backgroundColor = "green";
snakeHeadPosition.style.opacity = "1";
snakeNextHeadPosition.style.backgroundColor = "red";
snakeNextHeadPosition.style.opacity = "1";
} else {
alert("Game Over !")
/* RESET SNAKE POSITION : TODO : FUNCTION, CODE X2 */
$divdiv.forEach(element => {
element.style.backgroundColor = "white";
element.style.opacity = "0.7";
});
snakeTail.style.backgroundColor = "yellow";
snakeTail.style.opacity = "1";
snakeBody1.style.backgroundColor = "green";
snakeBody1.style.opacity = "1";
snakeBody2.style.backgroundColor = "green";
snakeBody2.style.opacity = "1";
snakeBody3.style.backgroundColor = "green";
snakeBody3.style.opacity = "1";
snakeHead.style.backgroundColor = "red";
snakeHead.style.opacity = "1";
score = 0;
$score.textContent = "Score : " + score;
/* + RESET VARIABLES */
turnPositions = [];
sensHead = "right";
sensTail = "right";
snakePreviousTailPosition = undefined;
snakeNextTailPosition = undefined;
snakeNextHeadPosition = undefined;
}
previousSens = sensHead;
snakeArray = [];
}, intervalSpeed);
});