-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.html
270 lines (221 loc) · 8.04 KB
/
snake_game.html
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
<!--
Author :
Himanshu Sharma
Github - himanshuthecoder
himanshusharma2972@gmail.com
www.blaregroup.com
*********************************************
Snake Game
*********************************************
About:
It is just a simple snake game which we used to play in keypad mobiles.
use keyboard arrow button to play this game
-->
<html>
<head>
<title>Snake Game</title>
<style>
body{background-color:black}
#gamearea{border:1px solid black;height:500;width:600;margin:80px auto;background-color:darkred;}
#score_container{position:relative;left:220px;top:25px;color:white;}
#playarea{height:400px;width:500px;border:2px solid red;border-radius:5px;margin:30px auto;margin-bottom:0px;background-color:lightgreen;}
#snake_food{background:red;height:7px;width:7px;border-radius:100%;position:relative;display:block;}
#snake{background:black;height:4px;width:20px;border-top-right-radius:10px;border-bottom-right-radius:10px;position:relative;padding:4px;
top:200px;left:250px;}
#leye,#reye{background:red;height:3px;width:3px;border-radius:100%;}
#leye{margin-left:15px;margin-top:-2px;}
#reye{margin-left:15px;margin-top:2px;}
</style>
<script>
/*Global declaring of variables*/
var score=0; //represent user score
var posl=250; //represent snake position from left side
var posu=200; //represent snake position from upper side
var i,j; //represents position of food
var t; //used to hold the program
var sleep=10; //represent for how much time program is being holded
/* creating food for snake */
function create_food()
{
var food=document.getElementById("snake_food");
i=Math.floor(Math.random()*(400-10));
j=Math.floor(Math.random()*(350-10));
food.style.left=i+"px";
food.style.top=j+"px";
}
//get user input by keyboard
function getkey(e)
{
/****************************************
left_arrow key=37
up_arrow key=38
right_arrow key=39
down_arrow key=40
*****************************************/
var key=e.which||e.keyCode; //getting key no.
switch(key)
{
case 37:move(4);break; //left
case 38:move(3);break; // up
case 39:move(1);break; //right
case 40:move(2);break; //down
}
}
/*moving of snake*/
function move(x)
{
/*used to speed up the snake */
if(score>10){sleep=6;}
if(score>20){sleep=4;}
if(x==1){clearInterval(t); t=setInterval(right,sleep);}
if(x==2){clearInterval(t); t=setInterval(down,sleep); }
if(x==3){clearInterval(t); t=setInterval(up,sleep);}
if(x==4){clearInterval(t); t=setInterval(left,sleep);}
if(x==5){clearInterval(t);}
}
/*moves snake to right side*/
function right()
{ var lefteye=document.getElementById("leye");
var righteye=document.getElementById("reye");
var snk=document.getElementById("snake");
posl+=1;
if(posl>=472)
{alert("Game Over"+"\nTotal Score="+score);clearInterval(t);restart();}
else{
snk.style.left=posl+"px";
snk.style.height="4px";
snk.style.width="20px";
snk.style.borderBottomLeftRadius="0px";
snk.style.borderBottomRightRadius="10px";
snk.style.borderTopLeftRadius="0px";
snk.style.borderTopRightRadius="10px";
lefteye.style.marginLeft="15px";
righteye.style.marginLeft="15px";
righteye.style.marginTop="2px";
lefteye.style.marginTop="-2px";
bite(posl+15,posu+4); //check eating of food
}
}
/*moves snake to left side*/
function left()
{ var lefteye=document.getElementById("leye");
var righteye=document.getElementById("reye");
var snk=document.getElementById("snake");
posl-=1;
if(posl<1){alert("Game Over"+"\nTotal Score="+score);clearInterval(t);restart();}
else{
snk.style.left=posl+"px";
snk.style.height="4px";
snk.style.width="20px";
snk.style.borderBottomLeftRadius="10px";
snk.style.borderBottomRightRadius="0px";
snk.style.borderTopLeftRadius="10px";
snk.style.borderTopRightRadius="0px";
lefteye.style.marginLeft="5px";
righteye.style.marginLeft="5px";
righteye.style.marginTop="2px";
lefteye.style.marginTop="-2px";
bite(posl,posu+4); //check eating of food
}
}
/*moves snake to up side*/
function up()
{ var lefteye=document.getElementById("leye");
var righteye=document.getElementById("reye");
var snk=document.getElementById("snake");
posu-=1;
if(posu<=-10){alert("Game Over"+"\nTotal Score="+score);clearInterval(t);restart();}
else{
snk.style.top=posu+"px";
snk.style.height="20px";
snk.style.width="4px";
snk.style.borderBottomLeftRadius="0px";
snk.style.borderBottomRightRadius="0px";
snk.style.borderTopRightRadius="10px";
snk.style.borderTopLeftRadius="10px";
lefteye.style.marginLeft="-1px";
righteye.style.marginLeft="3px";
righteye.style.marginTop="-3px";
lefteye.style.marginTop="5px";
bite(posl+2,posu); //check eating of food
}
}
/*moves snake to down side*/
function down()
{ var lefteye=document.getElementById("leye");
var righteye=document.getElementById("reye");
var snk=document.getElementById("snake");
posu+=1;
if(posu>=365){alert("Game Over"+"\nTotal Score="+score);clearInterval(t);restart();}
else{
snk.style.top=posu+"px";
snk.style.height="20px";
snk.style.width="4px";
snk.style.borderBottomLeftRadius="10px";
snk.style.borderBottomRightRadius="10px";
snk.style.borderTopLeftRadius="0px";
snk.style.borderTopRightRadius="0px";
lefteye.style.marginLeft="-1px";
righteye.style.marginLeft="3px";
righteye.style.marginTop="-3px";
lefteye.style.marginTop="15px";
bite(posl+2,posu+15); //check eating of food
}
}
/* this function checks the eating of food by snake
It takes the position of snake as argument
*/
function bite(left,top)
{
for(k=0;k<=7;k++)
{
if((i==left-k || i==left+k) && (j==top-k|| j==top+k))
{
score++; //incrementing score by 1
document.getElementById("score_Value").innerHTML=score;
create_food(); //create another food
}
}
}
/*this function restart the game*/
function restart()
{
posl=250;
posu=200;
score=0;
var lefteye=document.getElementById("leye");
var righteye=document.getElementById("reye");
document.getElementById("score_Value").innerHTML=score;
var snk=document.getElementById("snake");
snk.style.left=posl+"px";
snk.style.top=posu+"px";
snk.style.height="4px";
snk.style.width="20px";
snk.style.borderBottomLeftRadius="0px";
snk.style.borderBottomRightRadius="10px";
snk.style.borderTopLeftRadius="0px";
snk.style.borderTopRightRadius="10px";
lefteye.style.marginLeft="15px";
righteye.style.marginLeft="15px";
righteye.style.marginTop="2px";
lefteye.style.marginTop="-2px";
create_food();
}
</script>
</head>
<body onkeydown="getkey(event)" onload="create_food()">
<div id="gamearea">
<div id="score_container">
<div id="score_Label"><b>User Score = </b></div>
<div id="score_Value" style="height:20px;width:100px;margin-top:-18px;margin-left:130px">0</div>
</div>
<div id="playarea">
<div id="snake_food"></div>
<div id="snake">
<div id="leye"></div>
<div id="reye"></div>
</div>
</div>
</div>
</body>
</html>