-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.html
More file actions
211 lines (175 loc) · 4.38 KB
/
Copy pathSnake.html
File metadata and controls
211 lines (175 loc) · 4.38 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
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
<html>
<head>
</head>
<body>
<canvas style="margin-top:30px;display: block ;padding-left: 0; padding-right: 0; margin-left: auto; margin-right: auto;" id="myCanvas" width="520" height="520">
</canvas>
<br>
<marquee>Mission Helix</marquee>
<marquee><i>Snake game 🔥 SHUBHAM</i></marquee>
<p><b>#LearnInspireGrow #SnakeGame.</b></p>
<script>
// Made with love by Rishabh Jain !
var body = [];
var state = 0;// 0->right, 1->down, 2- left, 3 is up;
//step 1
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#A7D948";
ctx.fillRect(0, 0, 520, 520);
//step 2
function handleKey(e) {
e = e || window.event;
var play = false;
if (e.keyCode == '38'&&state!=1&&state!=3) {
// up arrow
state = 3;
play = true;
}
else if (e.keyCode == '40'&&state!=1&&state!=3) {
// down arrow
state = 1;
play = true;
}
else if (e.keyCode == '37'&&state!=0&&state!=2) {
// left arrow
state = 2;
play = true;
}
else if (e.keyCode == '39'&&state!=0&&state!=2) {
// right arrow
state = 0;
play = true;
}
if(play)
playAudio();
}
document.onkeydown = handleKey;
function playAudio(){
var audio = new Audio('https://www.soundjay.com/switch/switch-1.wav');
audio.play();
}
function playConsume(){
var audio = new Audio('https://www.soundjay.com/button/button-3.wav');
audio.play();
}
var N = 20;
var size = 520;
var cellSize = size/N;
var matrix = new Array(N);
for (var i = 0; i < matrix.length; i++) {
matrix[i] = new Array(N);
}
function drawCell(i,j){
if( (i+j)%2==0 ) {
ctx.fillStyle = ("#8ECC39");
}else{
ctx.fillStyle = "#A7D948";
}
ctx.fillRect(cellSize*i, cellSize*j, cellSize, cellSize);
}
for (var i = 0; i < matrix.length; i++){
for (var j = 0; j < matrix[i].length; j++){
matrix[i][j]=0;
drawCell(i,j);
}
}
body.push([1+ N/2,N/2]);
body.push([N/2,N/2]);
body.push([-1+N/2,N/2]);
var eyeImage = new Image();
eyeImage.src = "https://i.imgur.com/6jLbz7l.png";
var foodImage = new Image();
foodImage.src = "https://i.imgur.com/88saChB.png";
var counter = 0;
var foodX = 0;
var foodY = 0;
function generateFood(){
var success = false;
while(!success){
foodX = parseInt(Math.random()*N);
foodY = parseInt(Math.random()*N);
success = true;
for(var i=0;i<body.length;i++){
if(body[i][0]==foodX && body[i][1]==foodY){
success = false;
}
}
}
}
generateFood();
function update(){
counter++;
var increase = false;
if(body[0][0]==foodX&&body[0][1]==foodY){
generateFood();
playConsume();
increase = true;
}
for (var i = 0; i < matrix.length; i++){
for (var j = 0; j < matrix[i].length; j++){
drawCell(i,j);
}
}
ctx.drawImage(foodImage,
foodX*cellSize, foodY*cellSize,
cellSize, cellSize);
for(var i=0;i<body.length;i++){
ctx.fillStyle = ("#527DF9");
ctx.fillRect(cellSize*body[i][0], cellSize*body[i][1], cellSize, cellSize);
if(i==0){
var marginX = cellSize/3;
var marginY = cellSize/3;
if(state==0||state==2){
marginX=0;
}else if (state==1||state==3){
marginY=0;
}
ctx.drawImage(eyeImage,
0,28*(counter%9),
cellSize,cellSize,
cellSize*body[i][0]+marginX,
cellSize*body[i][1]+marginY,
cellSize, cellSize);
ctx.drawImage(eyeImage,
0,28*(counter%9),
cellSize,cellSize,
cellSize*body[i][0]-marginX,
cellSize*body[i][1]-marginY,
cellSize, cellSize);
/*
ctx.drawImage(tongueImage,
0,(504/21)*(counter%21),
48,
504/21,
cellSize*body[i][0]+cellSize,
cellSize*body[i][1]-marginY,
cellSize, cellSize);
*/
}
}
// 0->right, 1->down, 2- left, 3 is up;
var x = 0;
var y = 0;
if(state==0){
x++;
}
else if(state==1){
y++;
}
else if(state==2){
x--;
}
else if(state==3){
y--;
}
var first = body[0];
var arr = [ first[0]+x , first[1]+y ];
body.splice(0,0, arr);
if(!increase)
body.pop();
}
setInterval(update,300);
</script>
</body>
</html>