-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
52 lines (42 loc) · 1.01 KB
/
scripts.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
// THIS IS FOR UPDATING AND DRAWING SNAKE
import {
SNAKE_SPEED,
update as updateSnake,
draw as drawSnake,
getSnakeHead,
snakeIntersection,
} from "./snake.js";
import { outsideGrid } from "./grid.js";
import { update as updateFood, draw as drawFood } from "./food.js";
let previousTime = 0;
let gameOver = false;
const gameBoard = document.getElementById("game-board");
function main(currentTime) {
if (gameOver) {
if (confirm("You lost. Press ok to restart the game!")) {
window.location = "/";
}
return;
}
window.requestAnimationFrame(main);
let differ = (currentTime - previousTime) / 1000;
if (differ < 1 / SNAKE_SPEED) return;
previousTime = currentTime;
update();
draw();
}
window.requestAnimationFrame(main);
function update() {
updateSnake();
updateFood();
checkDeath();
}
function draw() {
gameBoard.innerHTML = "";
drawSnake(gameBoard);
drawFood(gameBoard);
}
function checkDeath() {
gameOver = outsideGrid(getSnakeHead()) || snakeIntersection();
}
main();