- Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathsnake.js
More file actions
Latest commit
170 lines (115 loc) · 3.26 KB
/
Copy pathsnake.js
File metadata and controls
170 lines (115 loc) · 3.26 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
159
160
161
162
163
164
165
166
167
168
169
170
/*
Create by Learn Web Developement
Youtube channel : https://www.youtube.com/channel/UC8n8ftV94ZU_DJLOLtrpORA
*/
constcvs=document.getElementById("snake");
constctx=cvs.getContext("2d");
// create the unit
constbox=32;
// load images
constground=newImage();
ground.src="img/ground.png";
constfoodImg=newImage();
foodImg.src="img/food.png";
// load audio files
letdead=newAudio();
leteat=newAudio();
letup=newAudio();
letright=newAudio();
letleft=newAudio();
letdown=newAudio();
dead.src="audio/dead.mp3";
eat.src="audio/eat.mp3";
up.src="audio/up.mp3";
right.src="audio/right.mp3";
left.src="audio/left.mp3";
down.src="audio/down.mp3";
// create the snake
letsnake=[];
snake[0]={
x : 9*box,
y : 10*box
};
// create the food
letfood={
x : Math.floor(Math.random()*17+1)*box,
y : Math.floor(Math.random()*15+3)*box
}
// create the score var
letscore=0;
//control the snake
letd;
document.addEventListener("keydown",direction);
functiondirection(event){
letkey=event.keyCode;
if(key==37&&d!="RIGHT"){
left.play();
d="LEFT";
}elseif(key==38&&d!="DOWN"){
d="UP";
up.play();
}elseif(key==39&&d!="LEFT"){
d="RIGHT";
right.play();
}elseif(key==40&&d!="UP"){
d="DOWN";
down.play();
}
}
// cheack collision function
functioncollision(head,array){
for(leti=0;i<array.length;i++){
if(head.x==array[i].x&&head.y==array[i].y){
returntrue;
}
}
returnfalse;
}
// draw everything to the canvas
functiondraw(){
ctx.drawImage(ground,0,0);
for(leti=0;i<snake.length;i++){
ctx.fillStyle=(i==0)? "green" : "white";
ctx.fillRect(snake[i].x,snake[i].y,box,box);
ctx.strokeStyle="red";
ctx.strokeRect(snake[i].x,snake[i].y,box,box);
}
ctx.drawImage(foodImg,food.x,food.y);
// old head position
letsnakeX=snake[0].x;
letsnakeY=snake[0].y;
// which direction
if(d=="LEFT")snakeX-=box;
if(d=="UP")snakeY-=box;
if(d=="RIGHT")snakeX+=box;
if(d=="DOWN")snakeY+=box;
// if the snake eats the food
if(snakeX==food.x&&snakeY==food.y){
score++;
eat.play();
food={
x : Math.floor(Math.random()*17+1)*box,
y : Math.floor(Math.random()*15+3)*box
}
// we don't remove the tail
}else{
// remove the tail
snake.pop();
}
// add new Head
letnewHead={
x : snakeX,
y : snakeY
}
// game over
if(snakeX<box||snakeX>17*box||snakeY<3*box||snakeY>17*box||collision(newHead,snake)){
clearInterval(game);
dead.play();
}
snake.unshift(newHead);
ctx.fillStyle="white";
ctx.font="45px Changa one";
ctx.fillText(score,2*box,1.6*box);
}
// call draw function every 100 ms
letgame=setInterval(draw,100);