-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
313 lines (251 loc) · 10.1 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* global sessionStorage*/
////////////////////////////////////////////////////////////////////////////////
///////////////////////// VARIABLE DECLARATIONS ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// HTML jQuery Objects
let board = document.querySelector('#board');
let scoreElement = document.querySelector('#score');
let highScoreElement = document.querySelector('#highScore');
// game variables
let snake = {};
let apple;
let score;
// interval variable required for stopping the update function when the game ends
let updateInterval;
// Constant Variables
let SQUARE_SIZE = 4;
let ROWS = COLUMNS = 100/SQUARE_SIZE;
let KEY = {
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40
};
let xDown = null;
let yDown = null;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////// GAME SETUP //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// turn on keyboard inputs
document.querySelector('body').addEventListener('keydown', setNextDirection);
document.addEventListener('touchstart', handleTouchStart);
document.addEventListener('touchmove', handleTouchMove);
// start the game
init();
alert("Use your keyboard or swipe to control the snake.")
function init() {
// initialize the snake's body and head
snake.body = [];
snake.head = makeSnakeSquare(10, 10)
snake.head.setAttribute('id', 'snake-head');
// initialize the first apple
apple = makeApple();
// set score to 0
scoreElement.innerHTML = "Score: 0";
score = 0;
calculateAndDisplayHighScore();
// start update interval
updateInterval = setInterval(update, 100);
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////// PROGRAM FUNCTIONS ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
* On each update tick update each bubble's position and check for
* collisions with the walls.
*/
function update() {
moveSnake();
if (hasCollidedWithApple()) {
handleAppleCollision();
}
if (hasCollidedWithSnake() || hasHitWall()) {
endGame();
}
}
function moveSnake() {
// starting at the tail, each snakeSquare moves to the (row, column) position
// of the snakeSquare that comes before it. The head is moved separately
for (let i = snake.body.length - 1; i >= 1; i--) {
let snakeSquare = snake.body[i];
let nextSnakeSquare = snake.body[i - 1];
snakeSquare.direction = nextSnakeSquare.direction;
repositionSquare(snakeSquare, nextSnakeSquare.row, nextSnakeSquare.column);
}
/* snake.head.nextDirection is set using keyboard input and only changes if the
next direction is perpendicular to snake.head.direction. This prevents the
snake from turning back on itself if multiple commands are issued before the
next udpate.
snake.head.direction is then only set once at the moment the snake is prepared
to move forward
*/
snake.head.direction = snake.head.nextDirection;
if (snake.head.direction === "left") { snake.head.column--; }
else if (snake.head.direction === "right") { snake.head.column++; }
else if (snake.head.direction === "up") { snake.head.row--; }
else if (snake.head.direction === "down") { snake.head.row++; }
repositionSquare(snake.head, snake.head.row, snake.head.column);
}
function hasCollidedWithApple() {
return snake.head.row === apple.row && snake.head.column === apple.column;
}
function handleAppleCollision() {
// increase the score and update the score DOM element
score++;
scoreElement.innerHTML = "Score: " + score;
// Remove existing Apple and create a new one
apple.remove();
makeApple();
// calculate the location of the next snakeSquare based on the current
// position and direction of the tail, then create the next snakeSquare
let row = snake.tail.row;
let column = snake.tail.column;
if (snake.tail.direction === "left") { column++; }
else if (snake.tail.direction === "right") { column--; }
else if (snake.tail.direction === "up") { row++; }
else if (snake.tail.direction === "down") { row--; }
makeSnakeSquare(row, column);
}
function hasCollidedWithSnake() {
for (let i = 1; i < snake.body.length; i++) {
if (snake.head.row === snake.body[i].row && snake.head.column === snake.body[i].column) {
return true;
}
}
}
function hasHitWall() {
return snake.head.row > ROWS || snake.head.row < 1 || snake.head.column > COLUMNS || snake.head.column < 1;
}
function endGame() {
// stop update function from running
clearInterval(updateInterval);
// clear board of all elements
removeAllChildElements(board);
calculateAndDisplayHighScore();
// restart the game after 500 ms
setTimeout(function() { init(); }, 500);
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////// HELPER FUNCTIONS ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function removeAllChildElements(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
/* Create an HTML element for a snakeSquare using jQuery. Then, given a row and
* column on the board, position it on the screen. Finally, add the new
* snakeSquare to the snake.body Array and set a new tail.
*/
function makeSnakeSquare(row, column) {
// make the snakeSquare jQuery Object and append it to the board
let snakeSquare = document.createElement('div');
snakeSquare.setAttribute('class', 'snakeSquare');
board.appendChild(snakeSquare);
// set the position of the snake on the screen
repositionSquare(snakeSquare, row, column);
// add snakeSquare to the end of the body Array and set it as the new tail
snake.body.push(snakeSquare);
snake.tail = snakeSquare;
return snakeSquare;
}
/* Given a gameSquare (which may be a snakeSquare or the apple), update that
* game Square's row and column properties and then position the gameSquare on the
* screen.
*/
function repositionSquare(square, row, column) {
// update the row and column properties on the square Object
square.row = row;
square.column = column;
// position the square on the screen according to the row and column
const leftOffset = (column-1) * SQUARE_SIZE + "%";
const topOffset = (row-1) * SQUARE_SIZE + "%";
square.style.left = leftOffset;
square.style.top = topOffset;
}
/* Create an HTML element for the apple using jQuery. Then find a random
* position on the board that is not occupied and position the apple there.
*/
function makeApple() {
// make the apple jQuery Object and append it to the board
apple = document.createElement('div')
apple.setAttribute('id', 'apple')
board.appendChild(apple);
// get a random available position on the board and position the apple
let randomPosition = getRandomAvailablePosition();
repositionSquare(apple, randomPosition.row, randomPosition.column);
return apple;
}
/* Returns a (row,column) Object that is not occupied by another game component
*/
function getRandomAvailablePosition() {
let spaceIsAvailable;
let randomPosition = {};
/* Generate random positions until one is found that doesn't overlap with the snake */
while (!spaceIsAvailable) {
randomPosition.column = Math.ceil(Math.random() * COLUMNS);
randomPosition.row = Math.ceil(Math.random() * ROWS);
spaceIsAvailable = true;
for (let i = 0; i < snake.body.length; i++) {
let snakeSquare = snake.body[i];
if (snakeSquare.row === randomPosition.row && snakeSquare.column === randomPosition.column) {
spaceIsAvailable = false;
}
}
}
return randomPosition;
}
/* Triggered when keybord input is detected. Sets the snake head's nextDirection
* property when an arrow key is pressed. Only perpendicular movement is allowed
*/
function setNextDirection(event) {
let keyPressed = event.which;
/* only set the next direction if it is perpendicular to the current direction */
if (snake.head.direction !== "left" && snake.head.direction !== "right") {
if (keyPressed === KEY.LEFT) { snake.head.nextDirection = "left"; }
if (keyPressed === KEY.RIGHT) { snake.head.nextDirection = "right"; }
}
if (snake.head.direction !== "up" && snake.head.direction !== "down") {
if (keyPressed === KEY.UP) { snake.head.nextDirection = "up"; }
if (keyPressed === KEY.DOWN) { snake.head.nextDirection = "down"; }
}
}
function calculateAndDisplayHighScore() {
// retrieve the high score from session storage if it exists, or set it to 0
let highScore = sessionStorage.getItem("highScore") || 0;
if (score > highScore) {
sessionStorage.setItem("highScore", score);
highScore = score;
alert("New High Score!");
}
// update the highScoreElement to display the highScore
highScoreElement.innerHTML = "High Score: " + highScore;
}
/* Touch Controls */
function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
/* choose the most significant */
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {
if (snake.head.direction !== "left" && snake.head.direction !== "right") {
snake.head.nextDirection = xDiff > 0 ? "left" : "right";
}
}
else {
if (snake.head.direction !== "up" && snake.head.direction !== "down") {
snake.head.nextDirection = yDiff > 0 ? "up" : "down";
}
}
/* reset values */
xDown = null;
yDown = null;
};