-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.js
154 lines (132 loc) · 3.73 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
let scoreField = document.getElementById("score-field");
let coinField = document.getElementById("coin-field");
let score = 0;
let coin = 0;
class SnakeBody {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// Board
let blockSize = 20;
let cols = 21;
let rows = 21;
let board;
let context;
// Snake
let snakeX = 10 * blockSize;
let snakeY = 10 * blockSize;
let velocityX = 0;
let velocityY = 0;
let snakeBody = [];
// Food
let foodX;
let foodY;
// Game State
let gameOver = false;
window.onload = function () {
board = document.getElementById("board");
board.height = rows * blockSize;
board.width = cols * blockSize;
context = board.getContext("2d");
if (window.innerWidth <= 600) {
document.querySelector(".error").style.display = "flex";
document.querySelector("canvas").style.display = "none";
return;
}
placeFood();
document.addEventListener("keydown", changeDirection);
setInterval(update, 1000 / 10);
};
function update() {
if (gameOver) {
showGameOverPopup();
return;
}
// Clear the board
context.fillStyle = "#ffd0a4";
context.fillRect(0, 0, board.width, board.height);
// Draw food
context.fillStyle = "red";
context.fillRect(foodX, foodY, blockSize, blockSize);
// Check if snake eats food
if (snakeX === foodX && snakeY === foodY) {
snakeBody.push(new SnakeBody(foodX, foodY));
placeFood();
coin += 100;
coinField.innerText = coin;
score++;
scoreField.innerText = score;
scoreField.style.color = "yellow"; // Highlight score temporarily
setTimeout(() => (scoreField.style.color = "black"), 500);
}
// Move snake body
for (let i = snakeBody.length - 1; i > 0; i--) {
snakeBody[i] = snakeBody[i - 1];
}
if (snakeBody.length) {
snakeBody[0] = new SnakeBody(snakeX, snakeY);
}
// Update head position
snakeX += velocityX * blockSize;
snakeY += velocityY * blockSize;
// Draw snake
context.fillStyle = "green";
context.fillRect(snakeX, snakeY, blockSize, blockSize);
context.fillStyle = "#4d1700";
for (let part of snakeBody) {
context.fillRect(part.x, part.y, blockSize, blockSize);
}
// Game Over conditions
if (
snakeX < 0 ||
snakeX >= cols * blockSize ||
snakeY < 0 ||
snakeY >= rows * blockSize ||
snakeBody.some(part => part.x === snakeX && part.y === snakeY)
) {
gameOver = true;
}
}
function changeDirection(e) {
if (e.code === "ArrowUp" && velocityY !== 1) {
velocityX = 0;
velocityY = -1;
} else if (e.code === "ArrowDown" && velocityY !== -1) {
velocityX = 0;
velocityY = 1;
} else if (e.code === "ArrowLeft" && velocityX !== 1) {
velocityX = -1;
velocityY = 0;
} else if (e.code === "ArrowRight" && velocityX !== -1) {
velocityX = 1;
velocityY = 0;
}
}
function placeFood() {
do {
foodX = Math.floor(Math.random() * cols) * blockSize;
foodY = Math.floor(Math.random() * rows) * blockSize;
} while (snakeBody.some(part => part.x === foodX && part.y === foodY));
}
function showGameOverPopup() {
const popup = document.getElementById("game-over-popup");
const finalScore = document.getElementById("final-score");
finalScore.textContent = score; // Display final score
popup.style.display = "block"; // Show popup
}
document.getElementById("restart-button").addEventListener("click", () => {
score = 0;
coin = 0;
scoreField.innerText = score;
coinField.innerText = coin;
snakeX = 10 * blockSize;
snakeY = 10 * blockSize;
velocityX = 0;
velocityY = 0;
snakeBody = [];
gameOver = false;
placeFood();
document.getElementById("game-over-popup").style.display = "none";
});