-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
283 lines (225 loc) · 7.55 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
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
//board
let blockSize = 25
let rows = 30;
let cols = 30;
let board;
let context;
//snake head
let snakeX = blockSize * 5;
let snakeY = blockSize * 5;
//snake speed
let velocityX = 0;
let velocityY = 0;
//snake body
let snakeBody = [];
//food
let foodX;
let foodY;
//gameover
let gameOver = false;
let score = 0;
let highscore = 0;
let scoreText;
let highScoreText;
let gameOverText;
let lastKeyPressed;
// animation
let growing = false; // Indicates if the snake is currently growing
let growthDuration = 100; // Duration of the growth animation in milliseconds
let growthStartTime = 0; // Time when the growth animation started
//options
let hasWalls = false;
window.onload = function () {
board = document.getElementById('board')
board.height = rows * blockSize;
board.width = cols * blockSize;
context = board.getContext('2d') // used for drawing on the board
placeFood();
document.addEventListener('keyup', changeDirection);
scoreText = document.getElementById('score')
scoreText.textContent = scoreText.textContent = `Score : ${score}`;
highScoreText = document.getElementById('highscore')
highScoreText.textContent = `Highscore : 0`
gameOverText = document.getElementById('gameOver')
const optionsButton = document.getElementById('options');
const optionsContent = document.getElementById('options-content');
optionsButton.addEventListener('click', function () {
if (optionsContent.style.display === 'block') {
optionsContent.style.display = 'none';
} else {
optionsContent.style.display = 'block';
}
});
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', function () {
if (this.name === 'hasWalls') {
hasWalls = this.checked;
console.log('Walls turned ' + (hasWalls ? 'on' : 'off'));
}
})
})
//reset
const resetButton = document.getElementById('reset-button');
resetButton.addEventListener('click', resetGame)
// Starting game loop
setInterval(update, 1000 / 10); // every one hundred milliseconds
}
function update() {
if (gameOver) {
return;
}
context.fillStyle = 'black';
context.fillRect(0, 0, board.width, board.height)
context.fillStyle = 'red';
context.fillRect(foodX, foodY, blockSize, blockSize)
//eating food
if (snakeX == foodX && snakeY == foodY) {
growing = true;
growthStartTime = Date.now();
snakeBody.push([foodX, foodY]);
score += 1;
scoreText.textContent = `Score : ${score}`; // Update the displayed score
placeFood();
}
for (let i = snakeBody.length - 1; i > 0; i--) {
snakeBody[i] = snakeBody[i - 1];
}
if (snakeBody.length) {
snakeBody[0] = [snakeX, snakeY];
}
context.fillStyle = 'lime';
if (hasWalls) {
snakeX += velocityX * blockSize; snakeY += velocityY * blockSize;
} else {
snakeX = (snakeX + velocityX * blockSize + cols * blockSize) % (cols * blockSize);
snakeY = (snakeY + velocityY * blockSize + rows * blockSize) % (rows * blockSize);
}
context.fillRect(snakeX, snakeY, blockSize, blockSize);
for (let i = 0; i < snakeBody.length; i++) {
drawSnakeSegment(snakeBody[i][0], snakeBody[i][1]);
}
console.log(hasWalls)
if (hasWalls) {
console.log('checking walls')
if (snakeX < 0 || snakeX > cols * blockSize - 1 || snakeY < 0 || snakeY > rows * blockSize - 1) {
gameOver = true;
gameOverText.textContent = 'Game Over!'
}
}
if (growing) {
// Check if the growth animation has finished
const currentTime = Date.now();
if (currentTime - growthStartTime >= growthDuration) {
growing = false;
} else {
// Double the snake head size during the growth animation
let direction = lastKeyPressed === 'W' ? [snakeX - blockSize / 2, snakeY] :
lastKeyPressed === 'S' ? [snakeX - blockSize / 2, snakeY] :
lastKeyPressed === 'A' ? [snakeX, snakeY - blockSize / 2] :
lastKeyPressed === 'D' ? [snakeX, snakeY - blockSize / 2] : null
context.fillRect(direction[0], direction[1], blockSize * 2, blockSize * 2);
}
}
for (let i = 0; i < snakeBody.length; i++) {
if (snakeX == snakeBody[i][0] && snakeY == snakeBody[i][1]) {
gameOver = true;
gameOverText.textContent = 'Game Over!'
}
}
}
function drawSnakeSegment(x, y) {
context.fillStyle = 'lime';
context.fillRect(x, y, blockSize, blockSize);
context.strokeStyle = 'black'; // Outline color
context.lineWidth = 2; // Outline width
context.strokeRect(x, y, blockSize, blockSize);
}
function placeFood() {
const emptyCells = [];
// Create a list of all empty cells on the board
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
const cellX = x * blockSize;
const cellY = y * blockSize;
let isOccupied = false;
// Check if the cell is occupied by the snake's head or body
if (cellX === snakeX && cellY === snakeY) {
isOccupied = true;
}
for (let i = 0; i < snakeBody.length; i++) {
if (cellX === snakeBody[i][0] && cellY === snakeBody[i][1]) {
isOccupied = true;
break;
}
}
if (!isOccupied) {
emptyCells.push({ x: cellX, y: cellY });
}
}
}
// Randomly select an empty cell for the food
if (emptyCells.length > 0) {
const randomIndex = Math.floor(Math.random() * emptyCells.length);
foodX = emptyCells[randomIndex].x;
foodY = emptyCells[randomIndex].y;
}
}
function changeDirection(e) {
switch (e.code) { // Can not use conditional in case statement
case 'ArrowUp':
case 'KeyW':
if (velocityY !== 1) {
velocityX = 0;
velocityY = -1;
lastKeyPressed = 'W'
// console.log(lastKeyPressed)
}
break;
case 'ArrowDown':
case 'KeyS':
if (velocityY !== -1) {
velocityX = 0;
velocityY = 1;
lastKeyPressed = 'S'
// console.log(lastKeyPressed)
}
break;
case 'ArrowLeft':
case 'KeyA':
if (velocityX !== 1) {
velocityX = -1;
velocityY = 0;
lastKeyPressed = 'A'
// console.log(lastKeyPressed)
}
break;
case 'ArrowRight':
case 'KeyD':
if (velocityX !== -1) {
velocityX = 1;
velocityY = 0;
lastKeyPressed = 'D'
// console.log(lastKeyPressed)
}
break;
}
}
function resetGame() {
snakeX = blockSize * 5;
snakeY = blockSize * 5;
velocityX = 0;
velocityY = 0;
snakeBody = [];
gameOver = false;
// Update the highscore if the current score is higher
if (score > highscore) {
highscore = score;
highScoreText.textContent = `Highscore : ${highscore}`;
}
score = 0;
scoreText.textContent = `Score : ${score}`;
gameOverText.textContent = null
context.clearRect(0, 0, board.width, board.height);
placeFood();
}