-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (123 loc) · 4.14 KB
/
Copy pathscript.js
File metadata and controls
158 lines (123 loc) · 4.14 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
const board = document.querySelector(".board");
const StartButton = document.querySelector(".btn-start");
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 blockHeight = 50;
const blockWidth = 50;
let IntervalId = null;
let TimerIntevalId = null;
let HighScore = localStorage.getItem("HighScore") || 0;
let Score = 0;
let Time = `00-00`;
HighScoreElement.innerText = HighScore;
const cols = Math.floor(board.clientWidth / blockWidth);
const rows = Math.floor(board.clientHeight / blockHeight);
let food = {x: (Math.floor(Math.random()*rows)),
y: (Math.floor(Math.random()*cols))
}
const blocks = [];
let snake = [{
x:1, y:3
}];
let direction = "down";
for(row = 0; row<rows; row++){
for(col = 0; col<cols; col++){
const block = document.createElement("div");
block.classList.add("block");
board.appendChild(block);
// block.textContent = `${row} x ${col}`;
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}
}
// wall colission
if(head.x < 0 || head.x >= rows || head.y < 0 || head.y >= cols){
clearInterval(IntervalId);
Modal.style.display = "flex";
StartGameModal.style.display = "none";
GameOverModal.style.display = "flex";
return;
}
// food
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.innerText = Score;
if(Score > HighScore){
HighScore = Score;
localStorage.setItem("HighScore", HighScore.toString()
);
}
}
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");
});
}
StartButton.addEventListener("click", () => {
Modal.style.display = "none";
IntervalId = setInterval(() => {render() }, 300);
TimerIntevalId = setInterval(() => {
let [min,sec] = Time.split("-").map(Number);
if(sec == 59){
min+=1;
sec = 0;
}
else{
sec+=1
}
Time = `${min}-${sec}`;
TimeElement.innerText = Time;
}, 1000);
});
restartButton.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.innerText = Score;
TimeElement.innerText = Time;
HighScoreElement.innerText = HighScore;
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() }, 300);
}
addEventListener("keydown", (event) => {
if(event.key === "w"){
direction = "up";
}else if(event.key === "s"){
direction = "down";
}else if(event.key === "d"){
direction = "right";
}else if(event.key === "a"){
direction = "left";
}
});