-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
381 lines (338 loc) · 10.9 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var game = (function () {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var nextFrame = null;
var scoreToIncreaseLevel = 10;
var framerate = 1000 / 40;
var framecount = 0;
var snake_def;
var keys = {
up: 0,
down: 0,
left: 0,
right: 0
};
var offset = {
x: 0,
y: 40
};
var grid = {
x: 32,
y: 32
};
var unit = {
x: (canvas.width - offset.x) / grid.x,
y: (canvas.height - offset.y) / grid.y
};
var treasure = {
location: {
x: 5,
y: 5
},
size: 8,
min_size: 6,
max_size: 8,
step: 0.1
};
var fontsize = 30;
context.font = fontsize + "px Verdana";
/* setup keyhandlers */
document.addEventListener("keyup", keyUpHandler, false);
document.addEventListener("keydown", keyDownHandler, false);
/* reset the snake, and start animating */
function start() {
snake_def = {
level: 1,
length: 2,
score: 0,
initial_length: 2,
speed: 3,
initial_speed: 3,
increase_speed: 0.05,
head: {x: 1, y: 1},
body: [],
size: unit.x / 2,
color: 'orange',
lookat: {x: 1, y: 0 },
direction: {x: 1, y: 0}
};
nextFrame = tick;
prevTick = Date.now();
tick();
}
/* called (once) on gameover */
function idle() {
}
/* called when game is running */
function tick() {
if(framecount > (framerate / snake_def.speed)) {
moveSnake(snake_def);
framecount = 0;
}
tickSnake(snake_def);
drawGame (context, snake_def);
framecount++;
requestAnimationFrame(nextFrame);
}
/* draw game elements */
function drawGame(ctx, snake) {
clearCanvas(ctx, 0, 0, canvas.width, canvas.height, '#eeffee'); // clear screen
var w = unit.x * grid.x;
var h = unit.y * grid.y;
drawRect(ctx,1,offset.y+1,w-2, h-2, 'black'); // draw borders
snake.body.forEach(function(e) { // draw snake body parts
drawSnakeBody(ctx, snake, e);
});
drawTreasure(ctx, treasure); // draw treasure
drawSnakeHead(ctx, snake); // draw snake head
drawSnakeEyes(ctx, snake, 'white', 'black'); // draw snake eyes
drawScore(ctx, snake); // draw score
}
/* check keys and adjust lookat accordingly */
function tickSnake(snake) {
var l = snake.lookat;
var d = snake.direction;
if(keys.left > 0) {
if(d.x === 0)
{
l.x = -1;
l.y = 0;
keys.left = -1;
}
}
if(keys.up > 0) {
if(d.y === 0)
{
l.x = 0;
l.y = -1;
keys.up = -1;
}
}
if(keys.right > 0) {
if(d.x == 0)
{
l.x = 1;
l.y = 0;
keys.right = -1;
}
}
if(keys.down > 0) {
if(d.y === 0)
{
l.y = 1;
l.x = 0;
keys.down = -1;
}
}
}
/* generate a random position on the map (for treasure) */
function randomGridPosition() {
return {
x: 2 + (Math.floor(Math.random() * (grid.x-4))),
y: 2 + (Math.floor(Math.random() * (grid.y-4)))
};
}
/* see if snake head is on same position as treasure */
function checkTreasure(snake) {
if (snake.head.x === treasure.location.x && snake.head.y === treasure.location.y) {
treasure.location = randomGridPosition();
return true;
}
return false;
}
/* called when snake is at treasure */
function ate(snake) {
snake.score++;
if(handlers.started && handlers.ate) {
handlers.ate(snake);
}
}
/* called when snake is leveled */
function leveled(snake) {
snake.speed += snake.increase_speed;
if(handlers.started && handlers.leveled) {
handlers.leveled(snake);
}
}
/* called on gameover */
function gameOver(snake) {
nextFrame = idle;
if(handlers.started) {
handlers.started = false;
if(handlers.gameOver) {
handlers.gameOver(snake);
}
}
}
/* called when eating treasure */
function growSnake(snake) {
snake.body.push({
x: snake.head.x,
y: snake.head.y
});
trimSnake(snake);
}
/* trim old snake body parts - after moving*/
function trimSnake(snake) {
while(snake.body.length > snake.length) {
snake.body.shift();
}
}
function moveSnake(snake) {
/* see if snake head is on treasure */
if (checkTreasure(snake)) {
snake.length++;
ate(snake);
if( (1+snake.length) % scoreToIncreaseLevel === 0) {
snake.level++;
leveled(snake);
}
}
/* move snake */
growSnake(snake);
snake.direction.x = snake.lookat.x;
snake.direction.y = snake.lookat.y;
snake.head.x += snake.direction.x;
snake.head.y += snake.direction.y;
wrapSnake(snake);
/* check snake head / body collisions */
snake.body.forEach(function(e) {
if(e.x === snake.head.x && e.y === snake.head.y) {
gameOver(snake);
}
});
}
/* check snake head / border collisions */
function wrapSnake(snake) {
if (snake.head.x > grid.x) {
snake.head.x = 0;
gameOver(snake);
}
if (snake.head.y > grid.y) {
snake.head.y = 0;
gameOver(snake);
}
if (snake.head.y < 0) {
snake.head.y = grid.y;
gameOver(snake);
}
if (snake.head.x < 0) {
snake.head.x = grid.x;
gameOver(snake);
}
}
/* draw (pulsating) treasure */
function drawTreasure(ctx, treasure) {
drawCircle(ctx, offset.x + treasure.location.x * unit.x, offset.y + treasure.location.y * unit.y, treasure.size, 'blue');
drawCircle(ctx, offset.x + treasure.location.x * unit.x, offset.y + treasure.location.y * unit.y, treasure.size-1, 'yellow');
treasure.size += treasure.step;
if(treasure.size > treasure.max_size || treasure.size < treasure.min_size) {
treasure.step *= -1;
}
}
/* draw snake body part */
function drawSnakeBody(ctx, snake, body) {
drawCircle(ctx, offset.x + body.x * unit.x, offset.y + body.y * unit.y, snake.size + 2, snake.color);
}
/* draw snake head */
function drawSnakeHead(ctx, snake) {
drawCircle(ctx, offset.x + snake.head.x * unit.x, offset.y + snake.head.y * unit.y, snake.size + 4, snake.color);
}
/* draw snake eyes */
function drawSnakeEyes(ctx, snake, outercolor, innercolor) {
var d = snake.direction;
var x = 0, y = 0;
var s = 1.6;
var s2 = s * 3;
if(d.x) {
// horizontal
x = unit.x / s2 * d.x;
y = unit.y / s2;
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s, outercolor);
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y - y + snake.head.y * unit.y, snake.size / s, outercolor);
x += d.x * s;
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s2, innercolor);
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y - y + snake.head.y * unit.y, snake.size / s2, innercolor);
}
else {
// vertical
x = unit.x / s2;
y = unit.y / s2 * d.y;
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s, outercolor);
drawCircle(ctx, offset.x - x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s, outercolor);
y += d.y * s;
drawCircle(ctx, offset.x + x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s2, innercolor);
drawCircle(ctx, offset.x - x + snake.head.x * unit.x, offset.y + y + snake.head.y * unit.y, snake.size / s2, innercolor);
}
}
/* draw game score */
function drawScore(ctx, snake) {
ctx.fillStyle = 'black';
ctx.fillText('score:' + snake.score, 102, fontsize);
ctx.fillText('level:' + snake.level, canvas.width - 202, fontsize);
}
/* empty canvas */
function clearCanvas(ctx, x, y, w, h, color) {
fillRect(ctx, x, y, w, h, color);
}
/* draw a filled rectangle */
function fillRect(ctx, x, y, w, h, c) {
ctx.fillStyle = c;
ctx.fillRect(x, y, w, h);
}
/* draw a rectangle */
function drawRect(ctx, x, y, w, h, c) {
ctx.strokeStyle = c;
ctx.strokeRect(x, y, w, h);
}
/* draw a filled circle */
function drawCircle(ctx, x, y, r, color) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
/* handle keydown events */
function keyDownHandler(e) {
if(e.keyCode == 37) {
keys.left = 1;
}
else if(e.keyCode == 38) {
keys.up = 1;
}
else if(e.keyCode == 39) {
keys.right = 1;
}
else if(e.keyCode == 40) {
keys.down = 1;
}
}
/* handle keyup events */
function keyUpHandler(e) {
if(e.keyCode == 37) {
keys.left = 0;
}
else if(e.keyCode == 38) {
keys.up = 0;
}
else if(e.keyCode == 39) {
keys.right = 0;
}
else if(e.keyCode == 40) {
keys.down = 0;
}
}
/* publicly exposed members */
var handlers = {
started: false,
ate: null,
leveled: null,
gameOver: null,
start: function() {
this.started = true;
start();
}
};
return handlers;
})();