-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsnake.js
409 lines (361 loc) · 9.49 KB
/
jsnake.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"use strict";
var LEFT = "LEFT", RIGHT = "RIGHT", UP = "UP", DOWN = "DOWN";
var RUNNING = "RUNNING", GAMEOVER = "GAMEOVER", PAUSED = "PAUSED";
var reverseDir = function(direction){
switch(direction){
case UP: return DOWN;
case DOWN: return UP;
case LEFT: return RIGHT;
case RIGHT: return LEFT;
}
};
var logState = function(state){
document.getElementById("score").innerHTML = state.score;
document.getElementById("length").innerHTML = state.snake.cells.length;
document.getElementById("snake").innerHTML = snakeHead(state).pos.x + " " + snakeHead(state).pos.y;
document.getElementById("fruit").innerHTML = state.fruit.pos.x + " " + state.fruit.pos.y;
document.getElementById("tick").innerHTML = state.tickDelay + "ms";
document.getElementById("state").innerHTML = state.gameState;
};
var randomPoint = function(width,height){
return {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height),
};
};
var wrap = function(value, limit){
if( value < 0){
return value + limit;
}
if( value >= limit){
return value - limit;
}
return value;
};
var wrapPoint = function(point, width, height){
return {
x: wrap(point.x, width),
y: wrap(point.y, height),
};
};
var clear = function(ctx){
ctx.fillStyle = "White";
ctx.fillRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
};
var drawSnake = function(ctx, state){
ctx.lineWidth = state.snake.cells[0].girth * state.field.scaleFactor / 5;
for(var i=0; i < state.snake.cells.length; i++){
var cell = state.snake.cells[i];
var x = cell.pos.x * state.field.scaleFactor;
var y = cell.pos.y * state.field.scaleFactor;
var girth = cell.girth * state.field.scaleFactor;
ctx.fillStyle = i === 0 ? "Blue" : "Red";
if( i === 0){
ctx.fillRect(x, y, girth, girth);
} else if( i % 3 === 0){
switch(cell.dir){
case UP:
case DOWN:
ctx.fillRect(x, y, girth/2, girth);
break;
case LEFT:
case RIGHT:
ctx.fillRect(x, y, girth, girth/2);
break;
}
} else if( i % 3 === 1){
switch(cell.dir){
case UP:
case DOWN:
ctx.fillRect(x+(girth*1/4), y, girth/2, girth);
break;
case LEFT:
case RIGHT:
ctx.fillRect(x, y+(girth*1/4), girth, girth/2);
break;
}
} else {
switch(cell.dir){
case UP:
case DOWN:
ctx.fillRect(x+(girth*1/2), y, girth/2, girth);
break;
case LEFT:
case RIGHT:
ctx.fillRect(x, y+(girth*1/2), girth, girth/2);
break;
}
}
ctx.strokeStyle = "White";
i && ctx.strokeRect(x, y, girth, girth);
/*
if( i > 1){
var boneWidth = cell.girth * state.field.scaleFactor / 5;
switch(cell.dir){
case UP:
ctx.fillRect(x+(girth/2)-(boneWidth/2), y-(girth/2), boneWidth, girth);
break;
case DOWN:
ctx.fillRect(x+(girth/2)-(boneWidth/2), y+(girth/2), boneWidth, girth);
break;
case LEFT:
ctx.fillRect(x-(girth/2), y+(girth/2)-(boneWidth/2), girth, boneWidth);
break;
case RIGHT:
ctx.fillRect(x+(girth/2), y+(girth/2)-(boneWidth/2), girth, boneWidth);
break;
}
}
*/
}
};
var drawFruit = function(ctx, state){
var x = state.fruit.pos.x * state.field.scaleFactor;
var y = state.fruit.pos.y * state.field.scaleFactor;
var s = state.snake.cells[0].girth * state.field.scaleFactor;
ctx.fillStyle = "Green";
ctx.fillRect(x,y,s,s);
};
var moveSnake = function(state, movementDelta){
for(var i=state.snake.cells.length-1; i >= 0; i--){
state.snake.cells[i].pos = wrapPoint(
getNextPos(state.snake.cells[i].pos, state.snake.cells[i].dir),
state.field.width,
state.field.height,
);
if(i===0){
state.snake.cells[i].dir = state.snake.cells[i].dir;
} else {
state.snake.cells[i].dir = state.snake.cells[i-1].dir;
}
}
};
var getNewFruitPos = function(state){
var p;
var collision = true;
while(collision){
p = randomPoint(state.field.width, state.field.height);
collision = false;
for(var i=0; i < state.snake.cells.length; i++){
var cell = state.snake.cells[i];
if(p.x === cell.pos.x && p.y === cell.pos.y){
collision = true;
break;
}
}
}
return p;
};
var initializeCtx = function(state){
var canvas = document.getElementById("canvas");
canvas.width = state.field.width * state.field.scaleFactor;
canvas.height = state.field.height * state.field.scaleFactor;
return canvas.getContext("2d");
};
var getNextPos = function(startPos, direction){
let movementDelta = 1;
switch(direction){
case UP:
return {
x: startPos.x,
y: startPos.y - movementDelta,
};
break;
case RIGHT:
return {
x: startPos.x + movementDelta,
y: startPos.y,
};
break;
case DOWN:
return {
x: startPos.x,
y: startPos.y + movementDelta,
};
break;
case LEFT:
return {
x: startPos.x - movementDelta,
y: startPos.y,
};
break;
}
};
var createInitialSnake = function(state, startPos){
var initialDirection = RIGHT;
var lastPos = startPos;
for(var i=0; i<state.snake.initalLength; i++){
state.snake.cells.push({
pos: lastPos,
dir: initialDirection,
girth: 1,
});
lastPos = getNextPos(lastPos, reverseDir(initialDirection));
}
state.nextDir = initialDirection;
};
var turnSnake = function(state, movementDelta){
if( snakeHead(state).dir !== state.nextDir && snakeHead(state).dir !== reverseDir(state.nextDir)){
snakeHead(state).dir = state.nextDir;
//moveSnake(state, movementDelta);
}
};
var growSnake = function(state, lengthDelta){
var tail = snakeTail(state);
state.snake.cells.push({
pos: getNextPos(tail.pos, reverseDir(tail.dir)),
dir: tail.dir,
girth: 1,
});
};
var snakeHead = function(state){
return state.snake.cells[0];
};
var snakeTail = function(state){
return state.snake.cells[state.snake.cells.length - 1];
};
var getInitialGameState = function(){
var defaultWidth = 50;
var defaultHeight = 25;
return {
gameState: RUNNING,
tickDelay: 100,
field: {
scaleFactor: 25,
width: defaultWidth,
height: defaultHeight,
},
score: 0,
snake: {
initalLength: 5,
cells: [],
},
fruit: {
pos: randomPoint(defaultWidth, defaultHeight)
},
};
};
var onload = function(){
var movementDelta = 1;
var lengthDelta = 1;
var scoreDelta = 1;
var tickDelta = -2;
var state;
var startNewGame = function(){
if(state){
window.cancelAnimationFrame(state.animationId);
window.clearTimeout(state.tickId);
}
state = getInitialGameState();
var startPos = {
x: Math.floor(state.field.width / 2),
y: Math.floor(state.field.height / 2),
};
var ctx = initializeCtx(state);
clear(ctx);
createInitialSnake(state, startPos);
//run the animation sequence
state.animationId = window.requestAnimationFrame(function(){
drawFrame(ctx,state);
});
//start the ticker
state.tickId = setTimeout(function(){
tick(state);
});
var pauseButtonHandler = function(e){
if(togglePause(state)){
e.target.style.borderStyle = 'inset';
} else {
e.target.style.borderStyle = 'outset';
}
};
document.getElementById('pauseToggle').removeEventListener('click', pauseButtonHandler);
document.getElementById('pauseToggle').addEventListener('click', pauseButtonHandler);
var keyDownHandler = function(e) { handleKeyDown(e, state)};
document.removeEventListener('keydown', keyDownHandler);
document.addEventListener('keydown', keyDownHandler);
};
var tick = function(state){
var head = snakeHead(state);
// check if we ate ourselves
for(var i=1; i < state.snake.cells.length; i++){
var cell = state.snake.cells[i];
if(head.pos.x === cell.pos.x && head.pos.y === cell.pos.y){
state.gameState = GAMEOVER;
new Audio('tick.wav').play();
}
}
if(state.gameState === RUNNING){
// check if we ate the fruit
if(head.pos.x === state.fruit.pos.x && head.pos.y === state.fruit.pos.y){
// add new fruit to the field
state.fruit.pos = getNewFruitPos(state);
// increase the snake's width
growSnake(state, lengthDelta);
// increment point counter
state.score += scoreDelta;
state.tickDelay += tickDelta;
new Audio('ui2.wav').play();
}
turnSnake(state, movementDelta);
moveSnake(state, movementDelta);
//schedule next call
state.tickId = setTimeout(function(){tick(state);}, state.tickDelay);
}
logState(state);
};
var drawFrame = function(ctx, state){
state.animationId = window.requestAnimationFrame(function(){drawFrame(ctx,state);});
clear(ctx);
drawSnake(ctx, state);
drawFruit(ctx, state);
};
var togglePause = function(state){
if(state.gameState === PAUSED){
state.gameState = RUNNING;
//start the ticker
state.tickId = setTimeout(function(){tick(state);});
return false;
} else if(state.gameState === RUNNING){
state.gameState = PAUSED;
//clearTimeout(state.tickId);
//state.tickId = null;
return true;
}
};
var handleKeyDown = function(e, state){
switch(e.keyCode){
case 37://Left Arrow
case 72:// h key
case 65:// a key
state.nextDir = LEFT;
//turnSnake(state, LEFT, movementDelta);
break;
case 38://Up Arrow
case 75:// k key
case 87:// w key
state.nextDir = UP;
//turnSnake(state, UP, movementDelta);
break;
case 39://Right Arrow
case 76:// l key
case 68:// d key
state.nextDir = RIGHT;
//turnSnake(state, RIGHT, movementDelta);
break;
case 40://Down Arrow
case 74:// j key
case 83:// s key
state.nextDir = DOWN;
//turnSnake(state, DOWN, movementDelta);
break;
case 80:// p key
togglePause(state);
break;
case 82:// r key
startNewGame();
break;
}
};
document.getElementById('newGameButton').addEventListener('click', startNewGame);
};