-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (168 loc) · 3.83 KB
/
index.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
let gamebox = document.querySelector(".gamebox");
let ctx = gamebox.getContext("2d");
let scoretext = document.querySelector(".scoretext");
let restartbtn = document.querySelector(".restart");
let gamewidth = gamebox.width;
let gameheight = gamebox.height;
let running = false;
let unitsize = 23;
let xvol = unitsize;
let yvol = 0;
let foodx;
let foody;
let score = 0;
let snake = [
{x:unitsize * 4 , y:0},
{x:unitsize * 3 , y:0},
{x:unitsize * 2 , y:0},
{x:unitsize , y:0},
{x:0 , y:0}
];
window.addEventListener("keydown", changethedirction);
restartbtn.addEventListener("click" , restartthegame);
startgame();
function startgame()
{
running = true;
gettick();
createfood();
};
function gettick()
{
if(running){
setTimeout(() => {
creategamebox();
drawfood();
movethesnake();
drawsnake();
checkgameover();
gettick();
}, 80);
}else
{
displaygameover();
}
};
function creategamebox()
{
ctx.fillStyle = "gray";
ctx.fillRect(0 , 0 , gamewidth , gameheight);
};
function createfood()
{
function randomfood(max , min)
{
let ranfood = Math.round((Math.random() * (max - min) + min) / unitsize) * unitsize;
return ranfood
}
foodx = randomfood(0 , gamewidth - unitsize);
foody = randomfood(0 , gamewidth - unitsize);
};
function drawfood()
{
ctx.fillStyle = "red";
ctx.fillRect(foodx,foody,unitsize,unitsize)
};
function movethesnake()
{
let head = {x:snake[0].x + xvol ,
y:snake[0].y + yvol};
snake.unshift(head);
if(snake[0].x == foodx && snake[0].y == foody)
{
score++;
scoretext.textContent = score;
createfood();
}
else
{
snake.pop();
}
};
function drawsnake()
{
ctx.fillStyle = "lightgreen"
ctx.strokeStyle = "black"
snake.forEach(snakepart =>{
ctx.fillRect(snakepart.x,snakepart.y,unitsize,unitsize)
ctx.strokeRect(snakepart.x,snakepart.y,unitsize,unitsize)
});
};
function changethedirction(e)
{
let keypressed = e.keyCode;
let left = 37;
let up = 38;
let right = 39;
let down = 40;
let goingup = (yvol == -unitsize);
let goingdown = (yvol == unitsize);
let goingright = (xvol == unitsize);
let goingleft = (xvol == -unitsize);
switch(true){
case(keypressed == left && !goingright):
xvol = -unitsize;
yvol = 0;
break;
case(keypressed == up && !goingdown):
xvol = 0;
yvol = -unitsize;
break;
case(keypressed == right && !goingleft):
xvol = unitsize;
yvol = 0;
break;
case(keypressed == down && !goingup):
xvol = 0;
yvol = unitsize;
break;
}
};
function checkgameover()
{
switch(true)
{
case(snake[0].x < 0):
running = false;
break;
case(snake[0].x >= gamewidth ):
running = false;
break;
case(snake[0].y < 0):
running = false
break;
case (snake[0].y >= gameheight):
running = false
break;
};
for(let i = 1; i < snake.length; i++)
{
if(snake[i].x == snake[0].x && snake[i].y == snake[0].y)
{
running = false;
}
}
};
function displaygameover()
{
ctx.font = "90px Arial";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText("Game Over!", 250, 250);
};
function restartthegame()
{
score = 0;
scoretext.textContent = score;
xvol = unitsize;
yvol = 0;
snake = [
{x:unitsize * 4 , y:0},
{x:unitsize * 3 , y:0},
{x:unitsize * 2 , y:0},
{x:unitsize , y:0},
{x:0 , y:0}
];
startgame();
};
// (;