-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
216 lines (197 loc) · 5.53 KB
/
snake.js
File metadata and controls
216 lines (197 loc) · 5.53 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
use2D = true;
//const variables that don't change;
var tileSize = 10;
var w = 600;
var h = 600;
var go;
var Score;
var scoreText = new TextBox();
scoreText.x = 10;
scoreText.y = 10;
world.addChild(scoreText);
gInput.addBool(65, "left");
gInput.addBool(87, "up");
gInput.addBool(68, "right");
gInput.addBool(83, "down");
var snake = new List();
var sHead = new Sprite();
sHead.height = tileSize;
sHead.width = tileSize;
sHead.x = 300;
sHead.y = 200;
sHead.prevX = sHead.x;
sHead.prevY = sHead.y;
sHead.direction= "right";
sHead.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/sHead.png");
var food = new Sprite();
food.height = tileSize;
food.width = tileSize;
food.x;
food.y;
food.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/food.png");
world.addChild(food);
var mine = new Sprite();
mine.height = tileSize;
mine.width = tileSize;
mine.x;
mine.y;
mine.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/Mine.png");
function makeSnake(){
//makes the inital snake
sHead.x = 400;
sHead.y = 300;
sHead.PrevX = sHead.x;
sHead.PrevY = sHead.y;
sHead.direction= "right";
food.x = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
food.y = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
var length = 10;
snake.push(sHead);
score = 0;
for (var i = 1; i<length;i++){
var sBody = new Sprite();
sBody.height = tileSize;
sBody.width = tileSize;
sBody.x = (snake.getAt(i-1).x - (tileSize+2));
sBody.y = (snake.getAt(i-1).y);
sBody.prevX = sBody.x;
sBody.prevY = sBody.y;
sBody.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/sBody.png");
snake.push(sBody);
}
for (var j=0; j<snake.length; j++){
world.addChild(snake.getAt(j));
}
go = setInterval(updateGame, 60);
}
function updateS(){
//allows for the snake to move
for (var i = 1; i<snake.length; i++){
snake.getAt(i).prevX = snake.getAt(i).x;
snake.getAt(i).prevY = snake.getAt(i).y;
switch(sHead.direction){
case "left":
snake.getAt(i).x = (snake.getAt(i-1).prevX+2);
snake.getAt(i).y = (snake.getAt(i-1).prevY);
break;
case "up":
snake.getAt(i).x = (snake.getAt(i-1).prevX);
snake.getAt(i).y = (snake.getAt(i-1).prevY+2);
break;
case "right":
snake.getAt(i).x = (snake.getAt(i-1).prevX-2);
snake.getAt(i).y = (snake.getAt(i-1).prevY);
break;
case "down":
snake.getAt(i).x = (snake.getAt(i-1).prevX);
snake.getAt(i).y = (snake.getAt(i-1).prevY-2);
break;
}
}
}
function selfCannibal(List){
for (var i = 1; i < List.length; i++){
if (List.getAt(0).x == List.getAt(i).x && List.getAt(0).y == List.getAt(i).y)
return true;
}
return false;
}
function updateGame(){
//if you hit a wall Game Over
if (sHead.x == w || sHead.x == 0 || sHead.y >= h || sHead.y <= 0){
clearInterval(go);
var gameO = new Sprite();
gameO.width = 600;
gameO.height = 600;
gameO.x = 0;
gameO.y = 0;
gameO.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/GameOver.png");
world.addChild(gameO);
}
//check to see if the snake is a cannibal
if (selfCannibal(snake)){
clearInterval(go);
var gameO = new Sprite();
gameO.width = 600;
gameO.height = 600;
gameO.x = 0;
gameO.y = 0;
gameO.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/GameOver.png");
world.addChild(gameO);
}
//add to the snake when he "eats" food and add
//a new food to the world, as well as a mine
if (sHead.x == food.x && sHead.y == food.y){
var nsnake = new Sprite();
nsnake.height = tileSize;
nsnake.width = tileSize;
switch (sHead.direction){
case "left":
nsnake.x = (snake.getAt(snake.length-1).x + (tileSize+2));
nsnake.y = (snake.getAt(snake.length-1).y);
case "right":
nsnake.x = (snake.getAt(snake.length-1).x - (tileSize+2));
nsnake.y = (snake.getAt(snake.length-1).y);
case "up":
nsnake.x = (snake.getAt(snake.length-1).x);
nsnake.y = (snake.getAt(snake.length-1).y + (tileSize+2));
case "down":
nsnake.x = (snake.getAt(snake.length-1).x);
nsnake.y = (snake.getAt(snake.length-1).y +(tileSize-2));
}
nsnake.prevX = nsnake.x;
nsnake.prevY = nsnake.y;
nsnake.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/sBody.png");
snake.push_back(nsnake);
world.addChild(snake.getAt(snake.length-1));
food.x = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
food.y = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
world.addChild(mine);
mine.x = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
mine.y = (Math.floor(Math.random()*(w/tileSize)))*tileSize;
score++;
}
//end the game if the snake collides with a mine
if (sHead.x == mine.x && sHead.y == mine.y){
clearInterval(go);
var gameO = new Sprite();
gameO.width = 600;
gameO.height = 600;
gameO.x = 0;
gameO.y = 0;
gameO.image = Textures.load("http://people.ucsc.edu/~tmcqueen/Sprites/GameOver.png");
world.addChild(gameO);
}
//changes the direction of the snake based on input
if (gInput.left && (sHead.direction != "right")){
sHead.direction = "left";
}
if (gInput.up && (sHead.direction != "down")){
sHead.direction = "up";
}
if (gInput.right && (sHead.direction != "left")){
sHead.direction = "right";
}
if (gInput.down && (sHead.direction != "up")){
sHead.direction = "down";
}
sHead.prevX = sHead.x;
sHead.prevY = sHead.y;
switch(sHead.direction){
case "left":
sHead.x -= tileSize;
break;
case "up":
sHead.y -= tileSize;
break;
case "right":
sHead.x += tileSize;
break;
case "down":
sHead.y += tileSize;
break;
}
updateS();
scoreText.text = "Score: "+ score;
}
makeSnake();