-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (107 loc) · 3.56 KB
/
Copy pathapp.js
File metadata and controls
137 lines (107 loc) · 3.56 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
const board = document.querySelector('.board');
const startButton = document.querySelector('.btn-start');
const modal = document.querySelector('.modal');
const scoreEl = document.getElementById("SCORE");
const highScoreEl = document.getElementById("HIGH-SCORE");
const timeEl = document.getElementById("TIME");
const blockSize = 50;
const cols = Math.floor(board.clientWidth / blockSize);
const rows = Math.floor(board.clientHeight / blockSize);
let intervalId = null;
let timeInterval = null;
let direction = "right";
let score = 0;
let time = 0;
let highScore = localStorage.getItem("snakeHighScore") || 0;
highScoreEl.innerText = highScore;
const blocks = [];
let snake = [{ x: 1, y: 3 }];
let food = { x: 0, y: 0 };
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[`${row}-${col}`] = block;
}
}
function generateFood() {
do {
food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols)
};
} while (snake.some(seg => seg.x === food.x && seg.y === food.y));
}
function resetGame() {
snake.forEach(seg => {
blocks[`${seg.x}-${seg.y}`].classList.remove("fill");
});
blocks[`${food.x}-${food.y}`]?.classList.remove("food");
snake = [{ x: 1, y: 3 }];
direction = "right";
score = 0;
time = 0;
scoreEl.innerText = score;
timeEl.innerText = "00-00";
generateFood();
}
function gameOver() {
clearInterval(intervalId);
clearInterval(timeInterval);
intervalId = null;
if (score > highScore) {
highScore = score;
localStorage.setItem("snakeHighScore", highScore);
highScoreEl.innerText = highScore;
}
startButton.innerText = "RESTART GAME";
modal.style.display = "flex";
}
function render() {
let head = { ...snake[0] };
if (direction === "left") head.y--;
else if (direction === "right") head.y++;
else if (direction === "up") head.x--;
else if (direction === "down") head.x++;
if (head.x < 0 || head.x >= rows || head.y < 0 || head.y >= cols) {
gameOver();
return;
}
for (let seg of snake) {
if (seg.x === head.x && seg.y === head.y) {
gameOver();
return;
}
}
if (head.x === food.x && head.y === food.y) {
score++;
scoreEl.innerText = score;
blocks[`${food.x}-${food.y}`].classList.remove("food");
generateFood();
} else {
const tail = snake.pop();
blocks[`${tail.x}-${tail.y}`].classList.remove("fill");
}
snake.unshift(head);
blocks[`${food.x}-${food.y}`].classList.add("food");
blocks[`${head.x}-${head.y}`].classList.add("fill");
}
startButton.addEventListener("click", () => {
modal.style.display = "none";
startButton.innerText = "START GAME";
resetGame();
intervalId = setInterval(render, 300);
timeInterval = setInterval(() => {
time++;
const min = String(Math.floor(time / 60)).padStart(2, "0");
const sec = String(time % 60).padStart(2, "0");
timeEl.innerText = `${min}-${sec}`;
}, 3000);
});
addEventListener("keydown", (e) => {
if (e.key === "ArrowUp" && direction !== "down") direction = "up";
else if (e.key === "ArrowDown" && direction !== "up") direction = "down";
else if (e.key === "ArrowLeft" && direction !== "right") direction = "left";
else if (e.key === "ArrowRight" && direction !== "left") direction = "right";
});