-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (31 loc) · 979 Bytes
/
index.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
'use strict'
import gameFactory from './game.js'
import {config} from './config.js'
const CANVAS_WIDTH = config.SIZE[0] * config.TILE_SIZE
const CANVAS_HEIGHT = config.SIZE[1] * config.TILE_SIZE
const canvas = document.querySelector('.game-canvas')
const context = canvas.getContext('2d')
const game = gameFactory((snakePoints) => {
context.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
snakePoints.forEach(([x, y]) => {
const drawnTileSize = config.TILE_SIZE - 2 * config.TILE_PADDING;
context.fillRect(
x * config.TILE_SIZE + config.TILE_PADDING,
y * config.TILE_SIZE + config.TILE_PADDING,
drawnTileSize,
drawnTileSize
)
})
})
setInterval(game.tick, 200)
const KEY_CODE_TO_DIRECTION = {
'37': 'left',
'38': 'top',
'39': 'right',
'40': 'bottom'
}
document.body.addEventListener('keydown', event => {
if (event.keyCode >= 37 && event.keyCode <= 40) {
game.onArrowEvent(KEY_CODE_TO_DIRECTION[event.keyCode])
}
})