-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
230 lines (207 loc) · 6.95 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
// Functions
const playBoard = document.querySelector(".snake_board");
const scoreElement = document.querySelector(".score");
const highScoreElement = document.querySelector(".high_score");
const timer = document.querySelector("#timer");
let gameover = false;
let foodx, foody;
let vx=0, vy=0;
let snakebody=[];
let setintervalid;
let score = 0;
let highscore = localStorage.getItem("high_score") || 0;
highScoreElement.innerText = `High Score: ${highscore}`;
let color = {
1:'blue',2: 'yellow', 3:'red',
4:'purple',5: 'orange', 6:'maroon',
7:'pink',8:'white',9:'aquamarine'
};
let cr =[];
const colorgeneration=()=>{
let color1 = 0;
let color2 = 0;
while (true) {
var randomcolor = Math.floor(Math.random() *9 ) +1;
if (randomcolor!=color1 && randomcolor!=color2 && cr.length!=3) {
cr.push(randomcolor);
console.log(randomcolor);
color2=color1;
color1=randomcolor;
}
else if (randomcolor!=color1 && randomcolor===color2 && cr.length!=3){
continue;
}
else if (randomcolor===color1 && cr.length!=3){
continue;
}
else{
break;
}
}
}
colorgeneration();
console.log(cr);
console.log(color[cr[0]]);
//color - 1
var random_color1 = color[cr[0]];
console.log(random_color1);
var x =document.getElementById('sq1');
x.setAttribute('style',`background-color:${random_color1}`);
console.log(document.getElementById('sq1').style.backgroundColor);
//color - 2
var random_color2 = color[cr[1]];
var x = document.getElementById('sq2');
x.setAttribute('style',`background-color:${random_color2}`);
//color - 3
var random_color3 = color[cr[2]];
var x = document.getElementById('sq3');
x.setAttribute('style',`background-color:${random_color3}`);
/*const changefoodpostion=()=>{
foodx = Math.floor(Math.random() * 30) +1;
foody = Math.floor(Math.random() * 30) +1;
}
*/
const changefoodpostion=()=>{
foodx1 = Math.floor(Math.random() * 30) +1;
foody1 = Math.floor(Math.random() * 30) +1;
foodx2 = Math.floor(Math.random() * 30) +1;
foody2 = Math.floor(Math.random() * 30) +1;
foodx3 = Math.floor(Math.random() * 30) +1;
foody3 = Math.floor(Math.random() * 30) +1;
}
const gameoverprompt=()=>{
clearInterval(setintervalid);
alert("Game Over - Press OK to play again");
location.reload();
}
const changedirection = (e) => {
console.log(e);
if(e.key === "ArrowUp"){
vx = 0;
vy = -1;
}
else if(e.key === "ArrowDown"){
vx = 0;
vy = 1;
}
else if(e.key === "ArrowLeft"){
vx = -1;
vy = 0;
}
else if(e.key === "ArrowRight"){
vx = 1;
vy = 0;
}
}
time=30;
let j=0;
let snakex = 5, snakey = 10;
let counter = 0;
changefoodpostion();
const ingame=() =>{
if(gameover){
return gameoverprompt();
}
if (j===0){
timer.innerHTML = 'Timer: 0:30';
}
j+=1;
if (j%10===0 && time!=0){
time-=1;
timer.innerHTML = `Timer: 0:${time}`;
}
else if (time===0){
return gameoverprompt();
}
var random_color1 = color[cr[0]];
let htmlmarkup = `<div id="sq11" style="grid-area: ${foody1} / ${foodx1} ;background-color:${random_color1}"></div>`;
var random_color2 = color[cr[1]];
htmlmarkup+=`<div id="sq22" style="grid-area: ${foody2} / ${foodx2} ;background-color:${random_color2}"></div>`;
var random_color3 = color[cr[2]];
htmlmarkup+=`<div id="sq33" style="grid-area: ${foody3} / ${foodx3} ;background-color:${random_color3}"></div>`;
/*
if (snakex === foodx && snakey === foody){
changefoodpostion();
snakebody.push([foodx,foody]);
score++;
highscore = score >= highscore ? score :highscore;
localStorage.setItem("high_score",highscore);
scoreElement.innerText = `Score: ${score}`;
highScoreElement.innerText = `High Score: ${highscore}`;
}
*/
if (counter === 0 && snakex === foodx1 && snakey === foody1){
snakebody.push([foodx1,foody1]);
counter++;
score++;
foodx1=-1
foody1=-1
highscore = score >= highscore ? score :highscore;
localStorage.setItem("high_score",highscore);
scoreElement.innerText = `Score: ${score}`;
highScoreElement.innerText = `High Score: ${highscore}`;
}
else if (counter === 1 && snakex === foodx2 && snakey === foody2){
snakebody.push([foodx2,foody2]);
counter++;
score++;
foodx2=-1
foody2=-1
highscore = score >= highscore ? score :highscore;
localStorage.setItem("high_score",highscore);
scoreElement.innerText = `Score: ${score}`;
highScoreElement.innerText = `High Score: ${highscore}`;
}
else if (counter === 2 && snakex === foodx3 && snakey === foody3){
snakebody.push([foodx3,foody3]);
counter++;
score++;
foodx3=-1
foody3=-1
highscore = score >= highscore ? score :highscore;
localStorage.setItem("high_score",highscore);
scoreElement.innerText = `Score: ${score}`;
highScoreElement.innerText = `High Score: ${highscore}`;
}
if (counter===3){
counter=0;
colorgeneration();
console.log(cr);
console.log(color[cr[0]]);
//color - 1
var random_color1 = color[cr[0]];
console.log(random_color1);
var x =document.getElementById('sq1');
x.setAttribute('style',`background-color:${random_color1}`);
console.log(document.getElementById('sq1').style.backgroundColor);
//color - 2
var random_color2 = color[cr[1]];
var x = document.getElementById('sq2');
x.setAttribute('style',`background-color:${random_color2}`);
//color - 3
var random_color3 = color[cr[2]];
var x = document.getElementById('sq3');
x.setAttribute('style',`background-color:${random_color3}`);
changefoodpostion();
time+=6;
}
for (let i = snakebody.length -1 ; i>0 ; i--) {
snakebody[i]=snakebody[i-1];
}
snakebody[0]=[snakex,snakey];
snakex += vx;
snakey += vy;
if (snakex < 0 || snakex > 30 || snakey < 0 || snakey > 30 ){
gameover=true;
}
for (let i = 0; i < snakebody.length; i++) {
htmlmarkup += `<div class="head" style="grid-area: ${snakebody[i][1]} / ${snakebody[i][0]}"></div>`;
if( i!==0 && snakebody[i][0]===snakebody[0][0] && snakebody[i][1]===snakebody[0][1]){
gameover=true;
}
}
playBoard.innerHTML = htmlmarkup;
}
changefoodpostion();
setintervalid = setInterval(ingame,100);
document.addEventListener("keydown",changedirection);