Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (132 loc) · 4.02 KB
/
Copy pathscript.js
File metadata and controls
154 lines (132 loc) · 4.02 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
const board = document.querySelector(".board");
const startBtn = document.querySelector(".start-btn");
const modal = document.querySelector(".modal");
const startGame = document.querySelector(".start-game");
const gameOver = document.querySelector(".game-over");
const restartBtn = document.querySelector(".btn-restart");
const highScoreElement = document.querySelector("#high-score");
const scoreElement = document.querySelector("#score");
const timeElement = document.querySelector("#time");
let highScore = localStorage.getItem("highscore") || 0;
let score = 0;
let time = `00-00`;
highScoreElement.textContent = highScore;
const blockHeight = 30;
const blockWeight = 30;
const blocks = [];
let intervalId = null;
let timeId = null;
let direction = "right";
const cols = Math.floor(board.clientWidth / blockWeight);
const rows = Math.floor(board.clientHeight / blockHeight);
let snake = [
{
x: 1,
y: 3,
},
];
let food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols),
};
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const block = document.createElement("div");
block.classList.add("blocks");
board.appendChild(block);
blocks[`${i} - ${j}`] = 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 == "up") {
head = { x: snake[0].x - 1, y: snake[0].y };
} else if (direction == "down") {
head = { x: snake[0].x + 1, y: snake[0].y };
}
if (head.x < 0 || head.y < 0 || head.x >= rows || head.y >= cols) {
clearInterval(intervalId);
modal.style.display = "flex";
startGame.style.display = "none";
gameOver.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.add("food");
snake.unshift(head);
score += 10;
scoreElement.textContent = score;
if (score > highScore) {
highScore = score;
localStorage.setItem("highscore", highScore);
}
}
snake.forEach((segment) => {
blocks[`${segment.x} - ${segment.y}`].classList.remove("fill");
});
snake.unshift(head);
snake.pop();
snake.forEach((segment) => {
blocks[`${segment.x} - ${segment.y}`].classList.add("fill");
});
}
startBtn.addEventListener("click", () => {
intervalId = setInterval(() => {
modal.style.display = "none";
render();
}, 200);
timeId = setInterval(() => {
let [minutes, second] = time.split("-").map(Number);
if (second == 59) {
minutes += 1;
second = 0;
} else {
second += 1;
}
time = `${minutes} - ${second}`;
timeElement.textContent = time;
}, 1000);
});
restartBtn.addEventListener("click", restartGame);
function restartGame() {
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.textContent = score;
highScoreElement.textContent = highScore;
timeElement.textContent = time;
modal.style.display = "none";
direction = "down";
snake = [{ x: 1, y: 3 }];
food = {
x: Math.floor(Math.random() * rows),
y: Math.floor(Math.random() * cols),
};
intervalId = setInterval(() => {
render();
}, 200);
}
addEventListener("keydown", (e) => {
if (e.key == "ArrowUp") {
direction = "up";
} else if (e.key == "ArrowDown") {
direction = "down";
} else if (e.key == "ArrowLeft") {
direction = "left";
} else if (e.key == "ArrowRight") {
direction = "right";
}
});