-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
335 lines (293 loc) · 6.61 KB
/
snake.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// as variáveis de "saída"
var canvas;
var context;
// as variáveis do jogo
var state = 0; // estado do jogo
var TILESIZE; // tamanho dos "tiles", apenas para desenhar na tela
var pieces; // a cobra
var apple; // a maçã
var keyUp, keyRight, keyDown, keyLeft; // teclas pressionadas ou soltas
var UP, DOWN, LEFT, RIGHT; // direção da cobra
var velX, velY; // velocidade nos eixos
var collision; // indica colisão da cabeça com alguma parte da cobra
var mapWidth, mapHeight; // dimensão do "mapa"
// as imagens
var imgApple;
var imgPiece;
var imgHead;
function loadImage ( imgUrl )
{
var img = new Image();
img.src = imgUrl;
return img;
}
function Piece ( x, y, dir )
{
this.id = -1;
this.x = x || 0;
this.y = y || 0;
this.dir = dir || 0;
}
Piece.prototype =
{
setPos: function (x, y)
{
this.x = x;
this.y = y;
},
randomPos: function ( )
{
var repeat;
var newPos = {x:0, y:0};
do
{
repeat = false;
newPos.x = Math.floor(Math.random() * mapWidth);
newPos.y = Math.floor(Math.random() * mapHeight);
for (var i = 0; i < pieces.length; i++)
if (pieces[i].id == this.id || (pieces[i].x == newPos.x && pieces[i].y == newPos.y))
repeat = true;
} while (repeat);
this.x = newPos.x;
this.y = newPos.y;
},
draw: function ( img )
{
context.drawImage(img, this.x * TILESIZE, this.y * TILESIZE);
}
}
function keyboardDown ( event )
{
var ev = event || window.event;
switch (ev.keyCode)
{
case 37: // seta esquerda
keyLeft = true;
break;
case 38: // seta para cima
keyUp = true;
break;
case 39: // seta direita
keyRight = true;
break;
case 40: // seta para baixo
keyDown = true;
break;
case 80: // tecla p = pause game
velX = velY = 0;
break;
default:
break;
}
}
function keyboardUp ( event )
{
var ev = event || window.event;
switch (ev.keyCode)
{
case 37: // seta esquerda
keyLeft = false;
break;
case 38: // seta para cima
keyUp = false;
break;
case 39: // seta direita
keyRight = false;
break;
case 40: // seta para baixo
keyDown = false;
break;
default:
break;
}
}
function gameInit ( )
{
TILESIZE = 32;
mapWidth = 20;
mapHeight = 15;
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.width = mapWidth * TILESIZE;
canvas.height = mapHeight * TILESIZE;
imgPiece = loadImage("img/piece.png");
imgApple = loadImage("img/apple.png");
imgHead = loadImage("img/head.png");
pieces = new Array(new Piece(), new Piece(), new Piece());
apple = new Piece();
apple.id = -10;
velX = velY = 0;
collision = false;
// direções a seguir
UP = 0;
RIGHT = 1;
DOWN = 2;
LEFT = 3;
// inicializando a parte A - a cabeça
pieces[2].id = 2;
pieces[2].x = 3;
pieces[2].y = 3;
pieces[2].dir = RIGHT;
// inicializando a parte B
pieces[1].id = 1;
pieces[1].x = 2;
pieces[1].y = 3;
pieces[1].dir = RIGHT;
// inicializando a parte C - o rabo
pieces[0].id = 0;
pieces[0].x = 1;
pieces[0].y = 3;
pieces[0].dir = RIGHT;
// escolhe as coordenadas da maçã
apple.randomPos();
// por fim seta o estado para 1
state = 1;
// atualiza o tamnho atual na pagina html
document.getElementById("currSize").innerHTML = pieces.length;
// atualiza o tamanho anterior na pagina html
document.getElementById("lastSize").innerHTML = 0;
}
function gameReset ( )
{
// atualiza o tamanho anterior na pagina html
document.getElementById("lastSize").innerHTML = pieces.length;
// retira o excesso de elementos deixa só 3 pra reiniciar
while (pieces.length > 3)
pieces.pop();
velX = velY = 0;
collision = false;
// reinicializando as peças
// inicializando a parte A - a cabeça
pieces[2].id = 2;
pieces[2].x = 3;
pieces[2].y = 3;
pieces[2].dir = RIGHT;
// inicializando a parte B
pieces[1].id = 1;
pieces[1].x = 2;
pieces[1].y = 3;
pieces[1].dir = RIGHT;
// inicializando a parte C - o rabo
pieces[0].id = 0;
pieces[0].x = 1;
pieces[0].y = 3;
pieces[0].dir = RIGHT;
apple.randomPos();
// atualiza o tamnho atual na pagina html
document.getElementById("currSize").innerHTML = pieces.length;
}
function gameLoop ( )
{
if (keyLeft)
{
if (pieces[pieces.length - 1].dir != RIGHT)
{
velX = -1;
velY = 0;
pieces[pieces.length - 1].dir = LEFT;
}
}
else if (keyUp)
{
if (pieces[pieces.length - 1].dir != DOWN)
{
velX = 0;
velY = -1;
pieces[pieces.length - 1].dir = UP;
}
}
else if (keyRight)
{
if (pieces[pieces.length - 1].dir != LEFT)
{
velX = 1;
velY = 0;
pieces[pieces.length - 1].dir = RIGHT;
}
}
else if (keyDown)
{
if (pieces[pieces.length - 1].dir != UP)
{
velX = 0;
velY = 1;
pieces[pieces.length - 1].dir = DOWN;
}
}
// atualiza as posições das pieces se estiver movendo
if (velX != 0 || velY != 0)
{
for (var i=0; i < pieces.length - 1; i++)
{
pieces[i].x = pieces[i + 1].x;
pieces[i].y = pieces[i + 1].y;
pieces[i].dir = pieces[i + 1].dir;
}
}
if (pieces[pieces.length - 1].x == apple.x && pieces[pieces.length - 1].y == apple.y)
{
pieces.push(new Piece());
pieces[pieces.length - 1].x = apple.x;
pieces[pieces.length - 1].y = apple.y;
pieces[pieces.length - 1].dir = pieces[pieces.length - 2].dir;
pieces[pieces.length - 1].id = pieces.length - 1;
apple.randomPos();
// atualiza o tamanho atual na pagina html
document.getElementById("currSize").innerHTML = pieces.length;
}
// agora move a cabeça
pieces[pieces.length - 1].x += velX;
pieces[pieces.length - 1].y += velY;
// depois de mover a cabeça limita o movimento no canvas
// Para o eixo X
if (pieces[pieces.length - 1].x >= mapWidth)
{
pieces[pieces.length - 1].x = 0;
}
else if (pieces[pieces.length - 1].x < 0)
{
pieces[pieces.length - 1].x = mapWidth - 1;
}
// Para o eixo Y
if (pieces[pieces.length - 1].y >= mapHeight)
{
pieces[pieces.length - 1].y = 0;
}
else if (pieces[pieces.length - 1].y < 0)
{
pieces[pieces.length - 1].y = mapHeight - 1;
}
// se colidiu com alguma parte da cobra
if (collision)
{
gameReset();
}
// verifica se colidiu com alguma parte da cobra
for (var i=0; i < pieces.length - 2 && !collision; i++)
if (pieces[pieces.length - 1].x == pieces[i].x && pieces[pieces.length - 1].y == pieces[i].y)
collision = true;
context.fillStyle = "#FFFFFF";
context.fillRect(0,0,canvas.width,canvas.height);
// desenha o corpo da cobra (sem a cabeça)
for (var i=0; i < pieces.length - 1; i++)
pieces[i].draw(imgPiece);
// desenha a maçã
apple.draw(imgApple);
// desenha a cabeça
pieces[pieces.length - 1].draw(imgHead);
}
function gameMain ( )
{
switch (state)
{
case 0:
gameInit();
break;
case 1:
gameLoop();
break;
default:
state = 0;
break;
}
}