-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.ts
45 lines (41 loc) · 1.37 KB
/
game.ts
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
const interval = 100;
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const context = canvas.getContext("2d");
context!.strokeStyle = "#abc32f";
const beepSound = new Audio('./assets/beep.mp3');
const world = new World(400, 400, 20, context, canvas)
const snake = new Snake()
const food = new Food()
snake.world = world
food.world = world
const pSpan = document.getElementById('snakeSize');
function draw(): void {
listenUserKeyBoards();
food.generateRandomFoodPosition();
setInterval(() => {
world.clearRect();
world.drawLinesHorizontal();
world.drawLinesVertical()
food.generateFood();
snake.drawSnake();
const isColliding = food.isColliding(snake.positionX, snake.positionY)
if (isColliding) {
snake.eatFood();
beepSound.play();
pSpan!.innerHTML = snake.size.toString();
}
world.save();
}, interval);
}
/** Return the user key pressed that matches the array */
function listenUserKeyBoards(): void {
document.addEventListener('keydown', (event) => {
const justListen: Direction[] = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"];
const keypressed = justListen.find((element) => {
return element == event.key;
});
if (keypressed != null) {
snake.direction = keypressed;
}
});
}