-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGameHomepage.js
60 lines (54 loc) · 1.58 KB
/
SnakeGameHomepage.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
(function(){
function SnakeGameHomepage() {
function initialize() {
drawHomepage();
window.addEventListener("keydown", catchUserEvents);
document.querySelector(".homepage .start").addEventListener("click", startGame);
}
function catchUserEvents(e) {
if (
(e.keyCode === 13 || e.which === 13) // Return
||
(e.keyCode === 32 || e.which === 32) // Spacebar
) {
startGame();
}
}
function drawHomepage() {
var main = document.querySelector("main");
main.classList.add("homepage");
main.appendChild(document.querySelector(".templates .homepageLayout"));
}
function hideHomepage() {
document.querySelector(".templates").appendChild(
document.querySelector(".homepageLayout")
);
document.querySelector("main").classList.remove("homepage");
}
function startGame() {
var difficultySpeed =
document.querySelector(
".homepage .difficulty .setting input:checked"
).value;
window.removeEventListener("keydown", catchUserEvents);
document.querySelector(".homepage .start").removeEventListener("click", startGame);
hideHomepage();
window.SnakeGame.player.initialize(
{
"timePerFrame" : difficultySpeed
}
);
}
return {
"initialize" : initialize
}
}
if (window.SnakeGame === undefined) {
window.SnakeGame = {
"homepage" : SnakeGameHomepage()
};
} else {
window.SnakeGame.homepage = SnakeGameHomepage();
}
window.SnakeGame.homepage.initialize();
}());