This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
74 lines (62 loc) · 2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
draw as drawSnake,
update as updateSnake,
getSpeed,
getSnakeHead,
snakeIntersection,
} from './snake.js';
import { draw as drawFood, update as updateFood } from './food.js';
import { draw as drawPoints, getPoints } from './points.js';
import { draw as drawHangman, hasFoundTheWord } from './hangman.js';
import { outsideGrid } from './grid.js';
const gameboard = document.getElementById('gameboard');
const pointsboard = document.getElementById('pointsboard');
const hangmanboard = document.getElementById('hangmanboard');
const lettersboard = document.getElementById('lettersboard');
const leaderboard = document.getElementById('leaderboard');
let lastRenderTime = 0;
let gameOver = false;
function main(currentTime) {
const hasFoundIt = hasFoundTheWord();
if (gameOver || hasFoundIt) {
if (hasFoundIt) {
document.getElementById('app').display = 'none';
document.getElementById('victory').display = 'flex';
}
const profile = prompt('What is your name?') || 'Unknown';
const profileEntry = { points: getPoints(), wordFound: hasFoundTheWord() };
window.localStorage.setItem(profile, JSON.stringify(profileEntry));
if (
confirm(`${hasFoundIt ? 'You won!' : 'You lost.'} Press ok to restart.`)
) {
window.location = '/';
}
return;
}
window.requestAnimationFrame(main);
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000;
if (secondsSinceLastRender < 1 / getSpeed()) return;
lastRenderTime = currentTime;
update();
draw();
}
window.requestAnimationFrame(main);
function update() {
updateSnake();
updateFood();
checkDeath();
}
function draw() {
gameboard.innerHTML = '';
pointsboard.innerHTML = '';
hangmanboard.innerHTML = '';
lettersboard.innerHTML = '';
leaderboard.innerHTML = '';
drawSnake(gameboard);
drawFood(gameboard);
drawPoints(pointsboard);
drawHangman(hangmanboard, lettersboard, leaderboard);
}
function checkDeath() {
gameOver = outsideGrid(getSnakeHead()) || snakeIntersection();
}