-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
237 lines (179 loc) · 5.08 KB
/
snake.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//game variables
console.time("full script")
let body = document.getElementById("body");
let head = document.getElementById("head");
let food = document.getElementById("food");
let sfood= document.getElementById('sfood');
let board = document.querySelector(".container");
const score= document.getElementById('score');
const hiscore= document.getElementById('hiscore');
const eat = new Audio('audio/clap.wav');
const over= new Audio('audio/precursor.wav');
const dir= new Audio('audio/kick.wav');
const increaseSpeed = document.getElementById("increaseSpeed")
const decreaseSpeed = document.getElementById('decreaseSpeed')
const showSpeed = document.getElementById("showSpeed")
let snakeDir = { x: 0, y: 0 };
let snake = [
{ x: 10, y: 10 },
{ x: 10, y: 9 },
{ x: 10, y: 8 }
];
let lastTime=0;
let speed = localStorage.getItem("speed")|| 5;
let foodLoaction = { x: 5, y: 15 };
let sfoodLocation= { x: 5, y: 15 };
let pause = true;
let point=0;
let highestPoint=0;
let supfood=0;
showSpeed.innerHTML= ` <b>speed : ${speed}</b> `
increaseSpeed.addEventListener("click",()=>{
speed++;
showSpeed.innerHTML= ` <b>speed : ${speed}</b> `
localStorage.setItem("speed",speed)
})
decreaseSpeed.addEventListener("click",()=>{
speed= speed-1
showSpeed.innerHTML= ` <b>speed : ${speed}</b> `
localStorage.setItem("speed",speed)
})
window.requestAnimationFrame(main);
// functions
// dont write code on main fucntion
function main(cTime) {
window.requestAnimationFrame(main);
// just to control the speed
if ((cTime - lastTime) / 1000 < 1 / speed) {
return 0;
}
lastTime = cTime;
board.innerHTML = "";
snakeMove();
foodUpdate();
gameover();
printScore();
// superfood(); super food is still not ready
// dont forget to call the function
}
window.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowUp":
if(snakeDir.y!=1){
snakeDir = { x: 0, y: -1 };
pause = false;
dir.play()
}
break;
case "ArrowDown":
if(snakeDir.y!=-1){
snakeDir = { x: 0, y: 1 };
pause = false;
dir.play()
}
break;
case "ArrowLeft":
if(snakeDir.x!=1){
snakeDir = { x: -1, y: 0 };
pause = false;
dir.play()
}
break;
case "ArrowRight":
dir.play()
if(snakeDir.x!=-1){
snakeDir = { x: 1, y: 0 };
pause = false;
dir.play()
}
break;
case "Enter":
snakeDir = { x: 0, y: 0 };
pause = true;
break;
default:
break;
}
});
// moving the snake
function snakeMove() {
snake.forEach((e, index) => {
if (e.x >= 20) e.x = e.x - 20;
if (e.y >= 20) e.y = e.y - 20;
if (e.x <= 0) e.x += 20;
if (e.y <= 0) e.y += 20;
if (index != snake.length - 1) {
if (!pause) {
snake[snake.length - index - 1].x = snake[snake.length - index - 2].x;
snake[snake.length - index - 1].y = snake[snake.length - index - 2].y;
}
body.style.gridRowStart =snake[snake.length - index - 1].y;
body.style.gridColumnStart =snake[snake.length - index - 1].x;
let sbody = body.cloneNode(true);
board.appendChild(sbody);
}
});
snake[0].x += snakeDir.x;
snake[0].y += snakeDir.y;
head.style.gridRowStart = snake[0].y;
head.style.gridColumnStart = snake[0].x;
board.appendChild(head);
}
// gameover function
function foodUpdate() {
if (foodLoaction.x == snake[0].x && foodLoaction.y == snake[0].y) {
snake.unshift({ x: foodLoaction.x, y: foodLoaction.y });
console.log(foodLoaction,snake)
point++;
supfood++;
// set timeout to change the after a litte delay to make it real
setTimeout(() => {
foodLoaction = {
x: Math.round(Math.random() * 20),
y: Math.round(Math.random() * 20),
};
}, 50);
eat.play();
}
food.style.gridRowStart = foodLoaction.y;
food.style.gridColumnStart = foodLoaction.x;
board.appendChild(food);
}
function gameover() {
// there is a bug i it should work from zero but it did not
for (let i = 3; i < snake.length; i++) {
if (snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
snakeDir = { x: 0, y: 0 };
pause = true;
over.play();
window.location.reload();
window.alert('game over dude');
}
}
}
function printScore(){
score.innerHTML=`<b> Score: ${point}</b>`;
hiscore.innerHTML=`<b> High Score: ${highestPoint}</b>`;
highestPoint=localStorage.getItem('hp');
highestPoint=localStorage.getItem('hp');
if (highestPoint<point) {
highestPoint=point;
localStorage.setItem('hp',highestPoint)
}
}
function superfood(){
setInterval(() => {
sfood.style.gridRowStart = sfoodLocation.y;
sfood.style.gridColumnStart = sfoodLocation.x;
board.appendChild(sfood);
if (sfoodLocation.x == snake[0].x && sfoodLocation.y == snake[0].y)
{
snake.unshift({ x: sfoodLocation.x, y: sfoodLocation.y });
sfoodLocation = {
x: Math.round(Math.random() * 20),
y: Math.round(Math.random() * 20),
};
}
}, 5000);
}
console.timeEnd("full script")