-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
48 lines (40 loc) · 1.21 KB
/
game.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
import {
update as updateSnake,
draw as drawSnake,
SNAKE_SPEED,
getSnakeHead,
snakeIntersection,
} from "./snake.js"; //מקשר את הדף גבה השני
import { update as updateFood, draw as drawFood } from "./food.js";
import { outSideGrid } from "./grid.js";
let lastRenderTime = 0;
let gameOver = false;
const gameBoard = document.querySelector("#game-board");
function main(currentTime) {
if (gameOver) {
if (confirm("you lost, press ok to restart")) {
window.location = "/";
}
return;
}
window.requestAnimationFrame(main); //גורם לפונקציה לחזור על עצמה שוב ושוב
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000;
if (secondsSinceLastRender < 1 / SNAKE_SPEED) return;
lastRenderTime = currentTime;
update(); //מעדכנת את כל הנתונים בסוף המשחק
draw(); //מציירת את הריבועים על המסך
}
window.requestAnimationFrame(main);
function update() {
updateSnake(gameBoard);
updateFood();
checkDeath();
}
function draw() {
gameBoard.innerHTML = "";
drawSnake(gameBoard);
drawFood(gameBoard);
}
function checkDeath() {
gameOver = outSideGrid(getSnakeHead()) || snakeIntersection();
}