-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
231 lines (198 loc) · 5.69 KB
/
snake.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
const gameBoard = document.querySelector("#gameBoard");
const ctx = gameBoard.getContext("2d");
const scoreText = document.querySelector("#scoreText");
const resetBtn = document.querySelector("#resetBtn");
const gameWidth = gameBoard.width;
const gameHeight = gameBoard.height;
const boardBackground = "white";
const snakeColor = "lightgreen";
const snakeBorder = "black";
const foodColor = "red";
const unitSize = 25;
//queueInput is used to synchronize the tick with user input.
//this needed to be done in order there is no multiple input before
//the snake is moved
let queueInput = false;
//timeout is used to make sure every reset, the speed is the same
let timeout;
let running = false;
let xVelocity = unitSize;
let yVelocity = 0;
let foodX;
let foodY;
let score = 0;
let snake = [
{x:unitSize * 4, y:0},
{x:unitSize * 3, y:0},
{x:unitSize * 2, y:0},
{x:unitSize, y:0},
{x:0, y:0}
];
window.addEventListener("keydown", changeDirection);
resetBtn.addEventListener("click", resetGame);
gameStart();
function gameStart(){
running = true;
scoreText.textContent = score;
createFood();
drawFood();
nextTick();
};
function nextTick(){
if(running){
queueInput = false;
timeout = setTimeout(()=>{
clearBoard();
drawFood();
moveSnake();
drawSnake();
checkGameOver();
nextTick();
}, 100);
}
else{
displayGameOver();
}
};
function clearBoard(){
ctx.fillStyle = boardBackground;
ctx.fillRect(0, 0, gameWidth, gameHeight);
};
//function to check if the food is going to spawn within the snakes body
//if it is, we call the create food function again. There should be better
//implementation than for looping the entire body of the snake. There is some obvious
//limitation that can happen once the snake body becames so huge.
function checkOverlap(x, y){
for(let i = 0; i < snake.length; i+=1)
{
if(x == snake[i].x && y == snake[i].y)
{
createFood();
break;
}
}
};
function createFood(){
function randomFood(min, max){
const randNum = Math.round((Math.random() * (max-min) + min) / unitSize) * 25;
return randNum;
}
foodX = randomFood(0, gameWidth-unitSize);
foodY = randomFood(0, gameWidth-unitSize);
checkOverlap(foodX, foodY);
};
function drawFood(){
ctx.fillStyle = foodColor;
ctx.fillRect(foodX, foodY, unitSize, unitSize);
};
function moveSnake(){
const head = {x: snake[0].x + xVelocity,
y: snake[0].y + yVelocity};
snake.unshift(head);
//The border is a portal (snake game with no border)
switch(true){
case(snake[0].x >= gameWidth):
snake[0].x = 0;
break;
case(snake[0].x < 0):
snake[0].x = 475;
break;
case(snake[0].y >= gameHeight):
snake[0].y = 0;
break;
case(snake[0].y < 0):
snake[0].y = 475;
break;
}
//if food is eaten
if(snake[0].x == foodX && snake[0].y == foodY){
score+=1;
scoreText.textContent = score;
createFood();
}
else{
snake.pop();
}
};
function drawSnake(){
ctx.fillStyle = snakeColor;
ctx.strokeStyle = snakeBorder;
snake.forEach(snakePart => {
ctx.fillRect(snakePart.x, snakePart.y, unitSize, unitSize);
ctx.strokeRect(snakePart.x, snakePart.y, unitSize, unitSize);
})
};
function changeDirection(event){
if(queueInput)return
queueInput = true;
const keyPressed = event.keyCode;
const LEFT = 37;
const UP = 38;
const RIGHT = 39;
const DOWN = 40;
const goingUp = (yVelocity == -unitSize);
const goingDown = (yVelocity == unitSize);
const goingRight = (xVelocity == unitSize);
const goingLeft = (xVelocity == -unitSize);
switch(true){
case(keyPressed == LEFT && !goingRight):
xVelocity = -unitSize;
yVelocity = 0;
break;
case(keyPressed == UP && !goingDown):
xVelocity = 0;
yVelocity = -unitSize;
break;
case(keyPressed == RIGHT && !goingLeft):
xVelocity = unitSize;
yVelocity = 0;
break;
case(keyPressed == DOWN && !goingUp):
xVelocity = 0;
yVelocity = unitSize;
break;
}
};
function checkGameOver(){
//This is for snake game with border
// switch(true){
// case (snake[0].x < 0):
// running = false;
// break;
// case (snake[0].x >= gameWidth):
// running = false;
// break;
// case (snake[0].y < 0):
// running = false;
// break;
// case (snake[0].y >= gameHeight):
// running = false;
// break;
// }
for(let i = 1; i < snake.length; i+=1){
if(snake[i].x == snake[0].x && snake[i].y == snake[0].y){
running = false;
}
}
};
function displayGameOver(){
ctx.font = "50px MV Boli";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText("GAME OVER!", gameWidth/2, gameHeight/2);
running = false;
};
function resetGame(){
score = 0;
xVelocity = unitSize;
yVelocity = 0;
snake = [
{x:unitSize * 4, y:0},
{x:unitSize * 3, y:0},
{x:unitSize * 2, y:0},
{x:unitSize, y:0},
{x:0, y:0}
];
clearTimeout(timeout);
gameStart();
};