-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.js
319 lines (288 loc) · 8.32 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//iife to make variables safe
;(function(){
"use strict";
var canvas = document.getElementById('canvas'),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight * 0.9,
ctx = canvas.getContext('2d'),
snakeWidth=25,
snakeHeight=25,
snakeHeadColor="#3f51b5",
snakeBodyColor = snakeHeadColor,
snakeBoundary = "#8c9eff",
//location of snakke
x=100,
y=100,
//speed of snake
dx=snakeWidth,
dy=snakeWidth,
length=1,
//button pressed state
leftPressed=false,
rightPressed=false,
downPressed=false,
upPressed=false,
previousReleased = false,
foodWidth = 25,
foodHeight = 25,
foodX = getRandom(width-foodWidth),
foodY = getRandom(height-foodHeight),
foodColor = "#8bc34a",
//blocks after the head
tails = [],
lives = 3,
heart = new Image(),
mouse = new Image(),
score = 0,
running = false,
previousPress = "default",
collided = false,
motionSpeedTimer = 150,
isGameOver = false;
heart.src="clippedHeart.png";
mouse.src="mouse.png";
function getRandom(upto){
//return random no between from 0 to upto
return Math.floor(Math.random()*upto);
}
//each block after the head (function constructor)
function SnakeBlocks(x,y){
this.x = x;
this.y = y;
this.direction = this.setDirection();
}
SnakeBlocks.prototype.move = function(){
//movement of tail blocks are independent of the button pressed
//depends on the direction each block have
if(this.direction==="left"){
this.x-=dx;
}else if(this.direction==="right"){
this.x+=dx;
}else if(this.direction==="up"){
this.y-=dy;
}else if(this.direction==="down"){
this.y+=dy;
}
};
//paints our tails
SnakeBlocks.prototype.paint = function(){
ctx.fillStyle=snakeBodyColor;
ctx.fillRect(this.x+1,this.y+1,snakeWidth-1,snakeHeight-1);
ctx.strokeStyle=snakeBoundary;
ctx.strokeRect(this.x,this.y,snakeWidth+2,snakeHeight+2);
};
//sets the direction of tail block according to where the next block is.
SnakeBlocks.prototype.setDirection = function(snake){
//by default give head parameters
//snake => SnakeBlocks object that is next to it
snake = snake || {x:x,y:y};
if(this.x === snake.x){
if(this.y > snake.y){
this.direction="up";
}else{
this.direction="down";
}
}else if(snake.y === this.y){
if(this.x < snake.x){
this.direction="right";
}else{
this.direction="left";
}
}
};
//event listeners
document.addEventListener('keydown',handleKeyDown,false);
function handleKeyDown(e){
//restrict left <=> right or up <=> down movements.
if(e.keyCode === 37 && previousPress !=="right"){
//reset all press states
disableAll();
//sets what's active
leftPressed = true;
previousPress = "left";
}else if(e.keyCode === 39 && previousPress !=="left"){
disableAll();
rightPressed = true;
previousPress = "right";
}else if(e.keyCode === 38 && previousPress!=="down"){
disableAll();
upPressed = true;
previousPress ="up";
}else if(e.keyCode===40 && previousPress!=="up"){
disableAll();
downPressed = true;
previousPress= "down";
}
}
function disableAll(){
leftPressed= false;
rightPressed= false;
upPressed= false;
downPressed = false;
//make it run whenever the valid key's are pressed
running = true;
}
function createSnake(){
//creates SnakeBlocks Object at correct pos acc to where the next one is
var previousSnake = tails[tails.length-1] || {x:x,y:y,direction:'right'};
if(previousSnake.direction=="left"){
tails.push(new SnakeBlocks(previousSnake.x + snakeWidth,previousSnake.y));
}else if(previousSnake.direction=="right"){
tails.push(new SnakeBlocks(previousSnake.x - snakeWidth,previousSnake.y));
}else if(previousSnake.direction=="up"){
tails.push(new SnakeBlocks(previousSnake.x ,previousSnake.y + snakeHeight));
}else if(previousSnake.direction=="down"){
tails.push(new SnakeBlocks(previousSnake.x,previousSnake.y-snakeHeight));
}
}
//UPDATES & PAINT EVERYTHIN ( MAIN GAME LOOP )
function draw(){
ctx.clearRect(0,0,width,height);
if(lives===0 && !isGameOver){
gameOver();
}
//reset directions on each block acc to next one
for(var i=0;i<tails.length;i++){
tails[i].setDirection(tails[i-1]);
}
//paint everything
drawFood();
for(var i=0;i<tails.length;i++){
tails[i].paint();
}
drawHead();
drawLives();
drawScore();
updateHead();
if(running){
for(var i=0;i<tails.length;i++){
tails[i].move();
}
}
checkFoodCollision();
//requestAnimationFrame(draw);
setTimeout(draw, motionSpeedTimer);
}
draw();
//by default create one tail
createSnake();
function drawLives(){
for(var i=0;i<lives;i++){
ctx.drawImage(heart,25 +35*i,25);
}
}
function drawScore(){
ctx.fillStyle="#8EB73D";
ctx.font = "20px Roboto,Arial";
ctx.fillText("Yummy, Mouses : " + score, width-250,45);
}
//checks food collision with head
function checkFoodCollision(){
if(x+snakeWidth + dx/8>=foodX && x <=foodX + foodWidth){
if(y+snakeHeight+dy/8>=foodY && y<=foodY + foodHeight){
//generate new snake
createSnake();
foodX = getRandom(width-foodWidth),
foodY = getRandom(height-foodHeight);
score+=1;
// increment speed after every 5 increment score
if(score >= 5 && score % 5 === 0){
motionSpeedTimer -=5;
console.log('decrementing',motionSpeedTimer);
}
collided = true;
}
}
}
function drawFood(){
ctx.fillStyle=foodColor;
ctx.drawImage(mouse,foodX,foodY);
}
function drawHead(){
//head background
ctx.fillStyle=snakeHeadColor;
ctx.fillRect(x+1,y+1,snakeWidth-1,snakeHeight-1);
ctx.strokeStyle=snakeBoundary;
ctx.strokeRect(x,y,snakeWidth+2,snakeHeight+2);
//eye1
ctx.fillStyle = "#e8eaf6";
drawArc("fill",x+snakeWidth/4,y+snakeHeight/4,2,0,2*Math.PI);
//eye2
drawArc("fill",x+snakeWidth*3/4,y+snakeHeight/4,2,0,2*Math.PI);
ctx.strokeStyle = "#e8eaf6";
if(collided){
//eating face
drawArc("stroke",x+snakeWidth/2,y+snakeHeight/1.5, 5 ,0,Math.PI*2);
collided = false;
}else{
//cute smile
drawArc("stroke",x+snakeWidth/2,y+snakeHeight/2, 5 ,45*Math.PI/180,Math.PI-4*Math.PI/180);
}
}
//just dry code to draw arc
function drawArc(style,x,y,radius,startAngle,endAngle){
ctx.beginPath();
ctx.arc(x,y,radius,startAngle,endAngle);
if(style=="stroke"){
ctx.stroke();
}else if(style=="fill"){
ctx.fill();
}
ctx.closePath();
}
function updateHead(){
//move head acc to what is pressed
if(leftPressed){
x-=dx;
}else if(rightPressed){
x+=dx;
}else if(upPressed){
y-=dy;
}else if(downPressed){
y+=dy;
}
//boundary detection
if(x+snakeWidth>width){
lives-=1;
stopMotion({horizontal:true,length: -dx*4});
x -=dx*5;
}else if(x+dy<0){
lives-=1;
stopMotion({horizontal:true,length: dx*4});
x+=dx*5;
}
if(y<0){
lives-=1;
stopMotion({horizontal:false,length: dy*4});
y+=dy*5;
}else if(y+snakeHeight>height){
lives-=1;
stopMotion({horizontal:false,length: -dy*4});
y-=dy*5;
}
}
//resets the positions of snake and stops it
function stopMotion(pos){
running = false;
leftPressed=0;
rightPressed=0;
downPressed=0;
upPressed=0;
if(pos.horizontal){
//position the x direction
for(var i=0;i<tails.length;i++){
tails[i].x +=pos.length;
}
}else{
//position the y direction
for(var i=0;i<tails.length;i++){
tails[i].y += pos.length;
}
}
}
function gameOver(){
alert("Game Over !! :( ");
isGameOver = true;
window.location.reload();
}
}());