-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (135 loc) · 4.47 KB
/
Copy pathscript.js
File metadata and controls
177 lines (135 loc) · 4.47 KB
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
const board = document.querySelector('.board');
const blockHeight = 50;
const blockWidth = 50;
const startButton = document.querySelector(".btn-start");
const cols = Math.floor(board.clientWidth / blockWidth);
const rows = Math.floor(board.clientHeight / blockHeight);
const modal = document.querySelector(".modal");
const startGameModal = document.querySelector(".start-game");
const gameOverModal = document.querySelector(".game-over");
const restartButton = document.querySelector(".btn-restart");
const highScoreElement = document.querySelector("#high-score")
const scoreElement = document.querySelector("#score")
const timeElement = document.querySelector("#time")
const blocks = [];
let score = 0
let highScore = localStorage.getItem("highScore") || 0
let time = `00-00`
highScoreElement.innerText = highScore
let snake = [{x: 1,y: 3}];
let direction = "down";
let intervalId = null;
let timerintervalId = null;
let food = {x: Math.floor(Math.random() * cols), y: Math.floor(Math.random() * rows)};
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const block = document.createElement('div');
block.classList.add("block");
board.appendChild(block);
blocks[`${col}-${row}`] = block;
}
}
function render() {
let head = null;
if (direction === "left") {head = {x: snake[0].x - 1,y: snake[0].y};
} else if (direction === "right") {head = {x: snake[0].x + 1,y: snake[0].y};
} else if (direction === "up") {head = {x: snake[0].x,y: snake[0].y - 1};
} else if (direction === "down") {
head = {
x: snake[0].x,
y: snake[0].y + 1
};
}
// Correct boundary check
if (
head.x < 0 ||
head.x >= cols ||
head.y < 0 ||
head.y >= rows
) {
clearInterval(intervalId);
modal.style.display = "flex";
startGameModal.style.display = "none";
gameOverModal.style.display = "flex";
return;
}
// Remove old snake drawing
snake.forEach(segment => {
blocks[`${segment.x}-${segment.y}`]?.classList.remove("fill");
});
let ateFood = false;
// Food consume logic
if (head.x === food.x && head.y === food.y) {
ateFood = true;
score += 10
scoreElement.innerText = score;
if (score > highScore) {
highScore = score;
localStorage.setItem("highScore", highScore.toString())
}
blocks[`${food.x}-${food.y}`]?.classList.remove("food");
food = {
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows)
};
}
// Move snake
snake.unshift(head);
// Only remove tail if food NOT eaten
if (!ateFood) {
snake.pop();
}
// Draw snake
snake.forEach(segment => {
blocks[`${segment.x}-${segment.y}`]?.classList.add("fill");
});
// Draw food
blocks[`${food.x}-${food.y}`]?.classList.add("food");
}
startButton.addEventListener("click", () => {
modal.style.display = "none";
intervalId = setInterval(() => { render(); }, 300);
timerintervalId = setInterval(() => {
let [min, sec] = time.split("-").map(Number);
if (sec === 60) {
min += 1
sec = 0
} else {
sec += 1
}
time = `${min}-${sec}`
timeElement.innerText = time
}, 1000);
});
restartButton.addEventListener("click", restartGame);
function restartGame() {
clearInterval(intervalId);
clearInterval(timerintervalId);
blocks[`${food.x}-${food.y}`]?.classList.remove("food");
snake.forEach(segment => {
blocks[`${segment.x}-${segment.y}`]?.classList.remove("fill");
});
score = 0
time =`00-00`
scoreElement.innerText = score
timeElement.innerText = time
highScoreElement.innerText = highScore
modal.style.display = "none";
snake = [{x: 1,y: 3}];
food = {x: Math.floor(Math.random() * cols), y: Math.floor(Math.random() * rows)};
intervalId = setInterval(render, 300);
timerintervalId = setInterval(() => {
timeElement.innerText = time;
}, 1000);
}
addEventListener("keydown", (event) => {
if (event.key === "ArrowUp" && direction !== "down") {
direction = "up";
} else if (event.key === "ArrowDown" && direction !== "up") {
direction = "down";
} else if (event.key === "ArrowRight" && direction !== "left") {
direction = "right";
} else if (event.key === "ArrowLeft" && direction !== "right") {
direction = "left";
}
});