-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.php
More file actions
283 lines (243 loc) · 7.88 KB
/
Snake.php
File metadata and controls
283 lines (243 loc) · 7.88 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
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
<!-- FileName: Snake.php
* Author: Pratibha Natani
* Description: This file contains the Main game logic. Contains the php and javascript code.
* References:
* http://www.tizag.com/phpT/forms.php
* http://www.w3schools.com/js/js_objects.asp
* http://odhyan.com/blog/2010/10/snake-in-javascript/
-->
<html>
<head>
<link rel="stylesheet" type="text/css" href="Snake.css">
</head>
<?php
//form parameters captured
$BoardSize = $_POST['BoardSize'];
$BoardSizex =substr($BoardSize, 0, 3);
$BoardSizey =substr($BoardSize, 6, 3);
$Pace= $_POST['SnakePace'];
if($Pace=="Slow")
$SnakePace=300;
else if($Pace=="Medium")
$SnakePace=250;
else if($Pace=="Fast")
$SnakePace=200;
$Goals= $_POST['Goals'];
?>
<body>
<label type="text" id='scoreLabel'>Score: </label>
<input type='text' id='scoreText' value='0'></input>
<div id="Board">
<img src="snake.png" id="snake0" />
<img src="snake.png" id="snake1" />
<img src="snake.png" id="snake2" />
</div>
</body>
<script type="text/javascript">
//to set the different game levels
var level1Score =50;
var level2Score =90;
var level3Score =120;
var levelSpeed =200;
//Game Flags
var moveOne = true;
var snakeDead = false;
//for setting Game Score
var Score=0;
var snakePart = 2;
//retrieve form parameters
var BoardSizex = "<?php echo $BoardSizex ?>px";
var BoardSizey = "<?php echo $BoardSizey ?>px";
var SnakePace = "<?php echo $SnakePace ?>";
var Goals = "<?php echo $Goals ?>";
var BoardSize1x = "<?php echo $BoardSizex ?>";
var BoardSize1y = "<?php echo $BoardSizey ?>";
document.getElementById('Board').style.height = BoardSizey;
document.getElementById('Board').style.width = BoardSizex;
//flag set to not draw the food again and again on timer
firstDraw=false;
function drawFood() {
if(!firstDraw)
{
for(k=1;k<=Goals;k++)
{
callFood(k);
}
firstDraw=true;
}
}
function callFood(k) {
foodX= randomNumbergen(1,BoardSize1x-10);
foodX=foodX-(foodX%10);
foodY= randomNumbergen(1,BoardSize1y-10);
foodY=foodY-(foodY%10);
//generate img tags on fly
var incr = "<img src = \"food.png\" id=\"food"+k+"\" style = \"position:absolute;left:"+foodX+";top:"+foodY+";height:10px;\" />";
document.getElementById('Board').innerHTML += incr;
document.getElementById('food' + k).style.left = foodX;
document.getElementById('food' + k).style.top = foodY;
}
function redrawFood(k) {
callFood(k);
var changer = document.getElementById('scoreText');
changer.value = Score;
//change Game levels
if(Score == level3Score)
{
var answer = confirm("Voilaaaaa!You won the game!!!")
alert("Bye bye!")
window.location = "index.html";
}
else if(Score == level2Score)
{
var answer = confirm("Yippiee!You cleared 2nd level...do you want to continue?")
if (answer){
alert("Continue to next Level with faster pace...")
clearInterval(timer);
timer=setInterval('mrSnake.move()', 50);
}
else{
alert("Bye bye!")
window.location = "index.html";
}
}
else if (Score == level1Score)
{
var answer = confirm("Yippiee!You cleared 1st level...do you want to continue?")
if (answer){
alert("Continue to next Level with faster pace...")
clearInterval(timer);
timer=setInterval('mrSnake.move()', 100);
}
else{
alert("Bye bye!")
window.location = "index.html";
}
}
return;
}
//generates a random number between range x,y
function randomNumbergen(x, y){
return Math.floor(Math.random() * y) + x;
}
function Snake() {
this.x = 30;
this.y = 30;
this.dir = 'R';
}
Snake.prototype.changeDir = function(dir) {
this.dir = dir;
}
Snake.prototype.draw = function() {
if ( snakeDead )
{
return false;
}
else if( this.x < 0 || this.x +10> BoardSize1x )
{
return false;
}
else if( this.y < 0 || this.y +10> BoardSize1y )
{
return false;
}
//let snake tail take position of head
for(i=snakePart; i>0; --i)
{
document.getElementById("snake"+i).style.left = document.getElementById("snake"+ (i-1)).style.left;
document.getElementById("snake"+i).style.top = document.getElementById("snake"+ (i-1)).style.top;
document.getElementById("snake"+i).style.visibility = 'visible';
}
document.getElementById('snake0').style.left = this.x;
document.getElementById('snake0').style.top = this.y;
if( !moveOne )
{
//check if snake touches itself-dead
for(i=snakePart; !snakeDead && i>0; --i)
{
if(
((((this.x) + "px") == document.getElementById("snake"+i).style.left) &&
((this.y + "px") == document.getElementById("snake"+i).style.top))
)
snakeDead = true;
}
//for eating all the foods present on the screen
for(i=1;i<=Goals;i++)
{
if((this.x + "px") == document.getElementById("food"+i).style.left &&
(this.y + "px") == document.getElementById("food"+i).style.top)
{
//dynamically generate snake size
++snakePart;
var imageSnake = "<img src = \"snake.png\" id=\"snake"+(snakePart)+"\" style = \"position:absolute;left:0;top:0;height:10px;Visibility:hidden\" />";
document.getElementById('Board').innerHTML += imageSnake;
//to disappear food that is eaten up
var image_x = document.getElementById('Board');
image_x.removeChild(document.getElementById('food'+i));
Score=Score+10;
//to generate another goal
redrawFood(i);
}
}
}
moveOne = false;
return true;
}
Snake.prototype.move = function() {
switch (this.dir) {
case 'L': // Left
this.x -= 10;
break;
case 'U': // Up
this.y -= 10;
break;
case 'R': // Right
this.x += 10;
break;
case 'D': // Down
this.y += 10;
break;
}
retval= this.draw();
if(!retval){
var answer = confirm("Oops Your snake is dead! Click Ok to Quit.")
if(answer)
{
clearInterval(timer);
window.location = "index.html";
}
}
drawFood();
}
var mrSnake = new Snake();
window.addEventListener('keydown', function(event) {
switch (event.keyCode) {
case 37: // Left
if(mrSnake.dir!='R')
{
mrSnake.changeDir('L');
}
break;
case 38: // Up
if(mrSnake.dir!='D')
{
mrSnake.changeDir('U');
}
break;
case 39: // Right
if(mrSnake.dir!='L')
{
mrSnake.changeDir('R');
}
break;
case 40: // Down
if(mrSnake.dir!='U')
{
mrSnake.changeDir('D');
}
break;
}
}, false);
timer=setInterval('mrSnake.move()', SnakePace);
</script>
</html>