-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
299 lines (253 loc) · 8.14 KB
/
index.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
297
298
299
// array as board, fixed count
let board = [
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
]
// elements
let snakeIndex = [67,75];
let appleIndex = 20;
let listOfAppleFree = []
let moveLeft = false;
let moveRight = false;
let moveUp = true;
let moveDown = false;
let speed = 600;
let defPass = true;
const boardDiv = document.getElementById('board-div');
const gameButton = document.getElementById('game-button');
const controlToggleButton = document.getElementById('control-toggle');
const controlsButton = document.querySelectorAll('.controls-button');
const controlDiv = document.getElementById('controls-div');
// initial draw call
draw(board);
// early DOM configuration
gameButton.onclick = gameOn;
controlToggleButton.onclick = controlsToggle;
controlsButton.forEach(btn => btn.onclick = function(){inputKey(btn.id)});
// control essentials
document.addEventListener('keydown', (e) => {
inputKey(e.key.toLowerCase());
})
function controlsToggle() {
if (controlToggleButton.innerHTML === "show control buttons?") {
controlDiv.style.display = "flex";
controlToggleButton.innerHTML = "hide control buttons?"
} else {
controlDiv.style.display = "none";
controlToggleButton.innerHTML = "show control buttons?"
}
}
function inputKey(key) {
if (gameButton.innerHTML !== 'click me to play!' && gameButton.innerHTML !== 'aw! wanna try again?') {
switch(key) {
case 'arrowup':
case 'w':
if (snakeIndex[0]-8 === snakeIndex[1]) {
break;
} else {
moveUp = true;
moveRight = false;
moveLeft = false;
moveDown = false;
}
break;
case 'arrowleft':
case 'a':
if (snakeIndex[0]-1 === snakeIndex[1]) {
break;
} else {
moveLeft = true;
moveUp = false;
moveRight = false;
moveDown = false;
}
break;
case 'arrowright':
case 'd':
if (snakeIndex[0]+1 === snakeIndex[1]) {
break;
} else {
moveRight = true;
moveUp = false;
moveLeft = false;
moveDown = false;
}
break;
case 'arrowdown':
case 's':
if (snakeIndex[0]+8 === snakeIndex[1]) {
break;
} else {
moveDown = true;
moveUp = false;
moveRight = false;
moveLeft = false;
}
break;
};
}
};
// sets new speed
function newSpeed() {
speed = 600 -(( 1/( board.length/snakeIndex.length) )*200);
};
// sets new list of snake-free index for next apple
function refreshAppleFree() {
let freeArr = [];
for (let i = 0; i < board.length; i++) {
if (!snakeIndex.some(x => x === i)) {
freeArr.push(i);
};
};
listOfAppleFree = freeArr;
}
// generate random index for next apple
function newAppleIndex() {
let rndm = Math.floor(Math.random() * (( listOfAppleFree.length -1 )- 0 + 1) + 0);
return rndm;
}
// resets the #board-div with a new fresh updated divs ;)
function draw(board) {
if (boardDiv.innerHTML === '') {
board.forEach(( e, i) => {
const cell = document.createElement('div')
boardDiv.appendChild(cell);
cell.id = i
cell.classList.add('cells');
cell.style.backgroundColor = '#212121';
detectSnakeOrApple(cell, i);
});
} else {
board.forEach(( e, i) => {
const cell = document.getElementById(i);
cell.style.backgroundColor = '#212121';
if(cell.innerHTML === ':' && i !== snakeIndex[0]) {
cell.innerHTML = '';
cell.classList.remove('cell-head');
cell.style.transform = '';
}
detectSnakeOrApple(cell, i);
});
}
}
function detectSnakeOrApple(cell, i) {
if(snakeIndex.some(x => x === i)) {
if(i === snakeIndex[0]) {
cell.classList.add('cell-head');
cell.innerHTML = ':';
let numDeg = moveUp ? 90 : moveRight ? 180 : moveDown ? 270 : 0
cell.style.transform = `rotate(${numDeg}deg)`
};
cell.style.backgroundColor = '#4df163';
} else if(i === appleIndex) {
cell.style.backgroundColor = '#ff5959';
};
}
// resets necessary game elements
function reset() {
snakeIndex = [75,83];
appleIndex = 20;
listOfAppleFree = []
moveLeft = false;
moveRight = false;
moveUp = true;
moveDown = false;
speed = 600;
boardDiv.onclick = () => {defPass = true; gameOn()};
gameButton.onclick = () => {defPass = true; gameOn()};
gameButton.innerHTML = 'click me to play!';
boardDiv.style.filter = '';
};
// starts the game with setInterval() and renderBoard()
function gameOn() {
boardDiv.style.filter = ''
// recalls renderBoard() for every speed in milliseconds
let interval = setInterval(() => renderBoard(), speed);
// instruction before first apple gets eaten
gameButton.innerHTML = '(w, a, s, d) are the controls!';
// disable the unnecessary button for gameplay
boardDiv.onclick = '';
gameButton.onclick = '';
// use of localStorage to clean interval later on other function
localStorage.clear();
localStorage.setItem('game-interval', interval);
}
// render the game by the game rules
function renderBoard() {
// if snake eats apple
if (snakeMove(snakeIndex[0]) === appleIndex) {
// adds apple index to snake
snakeIndex.unshift(appleIndex)
// new apple function
refreshAppleFree();
appleIndex = listOfAppleFree[newAppleIndex()];
newSpeed();
// refreshes the render calls for new speed
gameOff();
gameOn();
// gameboard update
gameButton.innerHTML =
`<div class="apple-count">${94-(board.length - snakeIndex.length)}/94</div> apples, <div class="speed-count">${(600/speed).toFixed(2)}x</div> speed`;
} else {
// else if snake dont eats apple
snakeIndex = snakeIndex.map((pos, i) => i === 0 ? snakeMove(pos): snakeIndex[i-1] );
}
// win or defeat checker
winTest();
defTest();
if (defPass) {
draw(board);
}
}
// stops the game with clearInterval()
function gameOff() {
clearInterval( localStorage.getItem('game-interval') );
}
function snakeMove(pos) {
// basic math to move snake in array w/ restrictions
if (moveUp) {
return pos-8;
} else if (moveDown) {
return pos+8;
} else if (moveLeft) {
return pos-1;
} else if (moveRight) {
return pos+1;
}
}
function winTest() {
// basic logic to detect win win
if (snakeIndex.length >= board.length) {
gameOff();
gameButton.innerHTML = 'nc! wanna play again?';
gameButton.onclick = gameOn;
}
}
function defTest() {
// to call if proven lose
function provenLose() {
reset();
gameOff();
defPass = false;
gameButton.innerHTML = 'aw! wanna try again?';
boardDiv.style.filter = 'brightness(80%)';
};
// some impromptu maths to detect snake move violations
if (snakeIndex.indexOf(snakeIndex[0]) !== snakeIndex.lastIndexOf(snakeIndex[0])) {
provenLose();
} else if (snakeIndex[0] < 0 || snakeIndex[0] > board.length-1) {
provenLose();
} else if ((moveLeft && (snakeIndex[0]+1)%8 === 0) || (moveRight && (snakeIndex[0]+8)%8 === 0)) {
provenLose();
}
}