-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
54 lines (45 loc) · 1.4 KB
/
web.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
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
// Mutable state
let state = initialState()
// Position helpers
const x = c => Math.round(c * canvas.width / state.cols)
const y = r => Math.round(r * canvas.height / state.rows)
// Game loop draw
const draw = () => {
// clear
ctx.fillStyle = '#232323'
ctx.fillRect(0, 0, canvas.width, canvas.height)
// draw snake
ctx.fillStyle = 'rgb(0,200,50)'
state.snake.map(p => ctx.fillRect(x(p.x), y(p.y), x(1), y(1)))
// draw apples
ctx.fillStyle = 'rgb(255,50,0)'
ctx.fillRect(x(state.apple.x), y(state.apple.y), x(1), y(1))
// add crash
if (state.snake.length == 0) {
ctx.fillStyle = 'rgb(255,0,0)'
ctx.fillRect(0, 0, canvas.width, canvas.height)
}
}
// Game loop update
const step = t1 => t2 => {
if (t2 - t1 > 100) {
state = next(state)
draw()
window.requestAnimationFrame(step(t2))
} else {
window.requestAnimationFrame(step(t1))
}
}
// Key events
window.addEventListener('keydown', e => {
switch (e.key) {
case 'w': case 'h': case 'ArrowUp': state = enqueue(state, NORTH); break
case 'a': case 'j': case 'ArrowLeft': state = enqueue(state, WEST); break
case 's': case 'k': case 'ArrowDown': state = enqueue(state, SOUTH); break
case 'd': case 'l': case 'ArrowRight': state = enqueue(state, EAST); break
}
})
// Main
draw(); window.requestAnimationFrame(step(0))