-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
81 lines (63 loc) · 2.42 KB
/
game.js
File metadata and controls
81 lines (63 loc) · 2.42 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
// game.js — state, game loop, resize, and boot
// draw() is defined in render.js; input listeners are set up in input.js
var COLS = 20;
var ROWS = 20;
var FPS = 9;
var CELL; // pixels per cell, computed in resize()
// ── State ─────────────────────────────────────────────────────────────────
var snake, dir, nextDir, food, score, best, phase, loopTimer;
// phase: 'idle' | 'running' | 'dead'
function init() {
snake = [{ x: 10, y: 10 }, { x: 9, y: 10 }, { x: 8, y: 10 }];
dir = { x: 1, y: 0 };
nextDir = { x: 1, y: 0 };
score = 0;
placeFood();
}
function placeFood() {
do {
food = { x: Math.floor(Math.random() * COLS), y: Math.floor(Math.random() * ROWS) };
} while (snake.some(function(s) { return s.x === food.x && s.y === food.y; }));
}
function start() {
if (loopTimer) clearInterval(loopTimer);
init();
phase = 'running';
loopTimer = setInterval(tick, 1000 / FPS);
}
// ── Game loop ──────────────────────────────────────────────────────────────
function tick() {
dir = nextDir;
var head = { x: (snake[0].x + dir.x + COLS) % COLS,
y: (snake[0].y + dir.y + ROWS) % ROWS };
if (snake.some(function(s) { return s.x === head.x && s.y === head.y; })) {
phase = 'dead';
best = Math.max(best, score);
clearInterval(loopTimer);
draw();
return;
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
placeFood();
} else {
snake.pop();
}
draw();
}
// ── Resize ────────────────────────────────────────────────────────────────
function resize() {
var size = Math.min(window.innerWidth, window.innerHeight);
CELL = Math.floor(size / COLS);
var px = CELL * COLS;
canvas.width = px;
canvas.height = px;
draw();
}
window.addEventListener('resize', resize);
// ── Boot ──────────────────────────────────────────────────────────────────
best = 0;
phase = 'idle';
init();
resize();