-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (108 loc) · 3.99 KB
/
script.js
File metadata and controls
129 lines (108 loc) · 3.99 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
const board = document.querySelector('.board');
const startButton = document.querySelector('.btn-start');
const restartButton = document.querySelector('.btn-restart');
const model = document.querySelector('.model');
const startGameModel = document.querySelector('.start-game');
const gameOverModel = document.querySelector('.game-over');
const highScoreElement = document.querySelector('#high-score');
const scoreElement = document.querySelector('#score');
const timeElement = document.querySelector('#time');
const blockHeight = 30;
const blockWidth = 30;
const cols = Math.floor(board.clientWidth / blockWidth);
const rows = Math.floor(board.clientHeight / blockHeight);
let highScore = localStorage.getItem('highScore') || 0;
let score = 0;
let time = `00:00`;
let intervalId = null;
let timerIntervalId = null;
let direction = 'down';
const blocks = [];
let snake = [{ x: 1, y: 3 }];
let food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols)
};
highScoreElement.innerText = highScore;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const block = document.createElement('div');
block.classList.add("blocks");
board.appendChild(block);
blocks[`${row}-${col}`] = block;
}
}
function render() {
let head = null;
blocks[`${food.x}-${food.y}`].classList.add('food');
if (direction === 'left') head = { x: snake[0].x, y: snake[0].y - 1 };
else if (direction === 'right') head = { x: snake[0].x, y: snake[0].y + 1 };
else if (direction === 'down') head = { x: snake[0].x + 1, y: snake[0].y };
else if (direction === 'up') head = { x: snake[0].x - 1, y: snake[0].y };
if (head.x < 0 || head.x >= rows || head.y < 0 || head.y >= cols) {
clearInterval(intervalId);
model.style.display = 'flex';
startGameModel.style.display = 'none';
gameOverModel.style.display = 'flex';
return;
}
if (head.x === food.x && head.y === food.y) {
blocks[`${food.x}-${food.y}`].classList.remove("food");
food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols)
};
blocks[`${food.x}-${food.y}`].classList.remove('food');
snake.unshift(head);
score += 10;
scoreElement.innerText = score;
if (score > highScore) {
highScore = score;
localStorage.setItem("highScore", highScore.toString());
}
}
snake.forEach(dets => blocks[`${dets.x}-${dets.y}`].classList.remove('fill'));
snake.unshift(head);
snake.pop();
snake.forEach(dets => blocks[`${dets.x}-${dets.y}`].classList.add('fill'));
}
startButton.addEventListener("click", () => {
model.style.display = 'none';
intervalId = setInterval(render, 200);
timerIntervalId = setInterval(updateTimer, 1000);
});
function updateTimer() {
let [min, sec] = time.split(':').map(Number);
if (sec === 59) {
min += 1;
sec = 0;
} else {
sec += 1;
}
time = `${String(min).padStart(2, '0')}:${String(sec).padStart(2, '0')}`;
timeElement.innerText = time;
}
function restartGame() {
blocks[`${food.x}-${food.y}`].classList.remove("food");
snake.forEach(dets => blocks[`${dets.x}-${dets.y}`].classList.remove("fill"));
score = 0;
time = `00:00`;
direction = 'down';
snake = [{ x: 1, y: 3 }];
food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols)
};
scoreElement.innerText = score;
timeElement.innerText = time;
highScoreElement.innerText = highScore;
model.style.display = 'none';
intervalId = setInterval(render, 200);
}
restartButton.addEventListener("click", restartGame);
addEventListener("keydown", (event) => {
if (event.key === 'ArrowUp') direction = 'up';
else if (event.key === 'ArrowRight') direction = 'right';
else if (event.key === 'ArrowDown') direction = 'down';
else if (event.key === 'ArrowLeft') direction = 'left';
});