-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
296 lines (271 loc) · 8.18 KB
/
script.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
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
const mainGrid = document.getElementById("main-box");
let foodSquare = document.querySelector("food");
let headSquare = document.querySelector("head");
// state trackers
const playerData = {
head: [],
length: 1,
orientation: null,
tempOrientation: null,
body: [],
currScore: 0,
highScore: 0,
};
let prevX = 0;
let prevY = 0;
let foodInterval;
let snakeInterval;
// currently not in use
let playerDataCopy;
// wipes board clean
const initBoard = () => {
// coordinates for squares
let rowIndex = 0;
let columnIndex = 0;
// create rows...
mainGrid.replaceChildren(
...[...Array(40)].map(() => {
// reset column# for each row
columnIndex = 0;
const row = document.createElement("div");
row.setAttribute("class", "row");
row.setAttribute("id", `row-${rowIndex++}`);
// create columns
row.replaceChildren(
...[...Array(40)].map(() => {
const column = document.createElement("div");
column.setAttribute("class", "column");
column.setAttribute("id", `column-${columnIndex++}`);
return column;
})
);
return row;
})
);
};
const generateApple = (
// randomizes the apple coordinates for every call
x = Math.floor(Math.random() * 40),
y = Math.floor(Math.random() * 40)
) => {
// makes sure that new coord != most recent/last coord
// DONE: logic has to account for snake body
while (x === prevX && y === prevY &&
!playerData.head[0] === x && !playerData[1] === y &&
!playerData.body.find(item => item[0] === x && item[1] === y)
) {
x = Math.floor(Math.random() * 40);
y = Math.floor(Math.random() * 40);
}
// to avoid prev and new tile conflict
prevX = x;
prevY = y;
// if food square still exists, wipe that square
if (foodSquare !== null) {
foodSquare.classList.remove("food");
}
currentRow = document.querySelector(`#row-${y}`);
foodSquare = currentRow.querySelector(`#column-${x}`);
foodSquare.setAttribute("class", "column food");
};
const generateSnake = (x, y) => {
// "moving" head coord
if (headSquare !== null) {
headSquare.classList.remove("head");
currentRow = document.querySelector(`#row-${y}`);
headSquare = currentRow.querySelector(`#column-${x}`);
// if head is moving into food
if (headSquare.classList[1] === "food") {
updateCurrScore();
// append to body
playerData.body.unshift([x, y]);
// resets the food
// if this isn't done, interval gets messed up
clearInterval(foodInterval);
generateApple();
foodInterval = setInterval(generateApple, 5000);
} else if (headSquare.classList[1] === "body") {
// game over!
terminateGame();
return;
}
headSquare.setAttribute("class", "column head");
playerData.head[0] = x;
playerData.head[1] = y;
} else {
currentRow = document.querySelector(`#row-${y}`);
headSquare = currentRow.querySelector(`#column-${x}`);
// holy shit how do i handle collision
headSquare.setAttribute("class", "column head");
playerData.head[0] = x;
playerData.head[1] = y;
}
};
// just making body tiles
const generateBody = (coords) => {
for (const val of coords) {
currentRow = document.querySelector(`#row-${val[1]}`);
let body = currentRow.querySelector(`#column-${val[0]}`);
body.setAttribute("class", "column body");
}
};
// for wiping tail tile
const clearTile = (coord) => {
currentRow = document.querySelector(`#row-${coord[1]}`);
let body = currentRow.querySelector(`#column-${coord[0]}`);
body.setAttribute("class", "column");
};
const updateCurrScore = () => {
document.getElementById(
"current-score-box"
).textContent = `current score: ${++playerData.currScore}`;
};
const updateHighScore = () => {
document.getElementById(
"current-score-box"
).textContent = `current score: ${playerData.currScore}`;
document.getElementById(
"high-score-box"
).textContent = `high score: ${playerData.highScore}`;
};
const moveSnake = () => {
// finalizes movement input
// has to be done to make game properly "run" on set interval
playerData.orientation = playerData.tempOrientation;
// to help wipe tail if it exists
let rearHolder = null;
// moving head according to input
if (playerData.orientation === "Left") {
prevHead = playerData.head;
if (playerData.currScore > 0) {
playerData.body.unshift([playerData.head[0], playerData.head[1]]);
rearHolder = playerData.body.pop()
}
playerData.head[0]--;
} else if (playerData.orientation === "Right") {
prevHead = playerData.head;
if (playerData.currScore > 0) {
playerData.body.unshift([playerData.head[0], playerData.head[1]]);
rearHolder = playerData.body.pop()
}
playerData.head[0]++;
} else if (playerData.orientation === "Down") {
prevHead = playerData.head;
if (playerData.currScore > 0) {
playerData.body.unshift([playerData.head[0], playerData.head[1]]);
rearHolder = playerData.body.pop()
}
playerData.head[1]++;
} else if (playerData.orientation === "Up") {
prevHead = playerData.head;
if (playerData.currScore > 0) {
playerData.body.unshift([playerData.head[0], playerData.head[1]]);
rearHolder = playerData.body.pop()
}
playerData.head[1]--;
} else {
// idk just blank for now
}
// if out of bounds
if (playerData.head[0] < 0 || playerData.head[0] > 39 ||
playerData.head[1] < 0 || playerData.head[1] > 39
) {
terminateGame();
}
generateSnake(playerData.head[0], playerData.head[1]);
if (playerData.body.length > 0) {
console.log("generating body");
console.log(playerData.body);
generateBody(playerData.body);
}
// to wipe tail
if (rearHolder) {
clearTile(rearHolder);
}
// in case i want to do anything with previous gamestate
playerDataCopy = JSON.parse(JSON.stringify(playerData));
};
// flag to check if game has already started
let gameStarted = false;
// starts game
onkeydown = (e) => {
// makes sure generate doesn't happen more than once naturally
if (!gameStarted) {
gameStarted = true;
foodInterval = setInterval(generateApple, 5000);
}
// removes the 'Arrow'- prefix onkeydown event
const keypress = e.key.slice(5);
// inits snake movement
if (playerData.orientation === null) {
snakeInterval = setInterval(moveSnake, 50);
playerData.orientation = keypress;
playerData.tempOrientation = keypress;
return;
}
// validating keypresses
// if the same as last. and if polar opposite
switch (keypress) {
case "Left":
if (
keypress !== playerData.orientation &&
playerData.orientation !== "Right"
) {
playerData.tempOrientation = keypress;
}
break;
case "Right":
if (
keypress !== playerData.orientation &&
playerData.orientation !== "Left"
) {
playerData.tempOrientation = keypress;
}
break;
case "Down":
if (
keypress !== playerData.orientation &&
playerData.orientation !== "Up"
) {
playerData.tempOrientation = keypress;
}
break;
case "Up":
if (
keypress !== playerData.orientation &&
playerData.orientation !== "Down"
) {
playerData.tempOrientation = keypress;
}
break;
}
};
const terminateGame = () => {
if (playerData.currScore > playerData.highScore) {
alert(`Game over! Click 'Okay' or Press <Enter> to restart.\nNew Best: ${playerData.currScore}`);
} else {
alert(`Game over! Click 'Okay' or Press <Enter> to restart.`);
}
clearInterval(snakeInterval);
clearInterval(foodInterval);
// GOOD OL' MANUAL STATE RESETS
prevX = 0;
prevY = 0;
playerData.orientation = null;
playerData.tempOrientation = null;
playerData.head = [],
playerData.highScore = Math.max(playerData.currScore, playerData.highScore);
playerData.currScore = 0;
playerData.body = [],
gameStarted = false;
updateHighScore();
initBoard();
generateApple(10, 30);
generateSnake(30, 10);
};
// ------------------ GAME START ------------------ //
// - apateu-a-pateu apateu-a-pateu apateu-a-pateu - //
initBoard();
generateApple(10, 30);
generateSnake(30, 10);
alert(`Use <arrow> keys to move the snake!`)