-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
130 lines (113 loc) · 3 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const laserGunSound ='http://www.freesound.org/data/previews/42/42106_70164-lq.mp3';
// --- state varibles
let player;
let spaceship;
let grid;
let points;
let timer;
let currentlyPlaying;
let message;
// --- cached elements
const startBtn = document.getElementById("startBtn");
const playAgainBtn = document.getElementById("playAgainBtn");
const gridArea = document.getElementById("grid");
const messageEl = document.getElementById("message");
const sound = new Audio();
// --- event listeners
startBtn.addEventListener("click", startGame);
gridArea.addEventListener("click", handleGridClick);
playAgainBtn.addEventListener("click", playAgain);
// --- functions
init()
function init() {
timer = 13;
points = 0;
grid = [null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null];
currentlyPlaying = false;
message = "Click the spaceship with the tip of the laser gun"
sound.src = laserGunSound;
render()
}
function startGame(evt) {
evt.preventDefault();
if (currentlyPlaying === true) {
return;
}
currentlyPlaying = true;
message = "";
updateGrid();
remainingTime();
}
function updateGrid() {
for (let i = 0; i < grid.length; i++) {
grid[i] = null;
}
if (timer === 0) {
renderGrid();
return;
}
let randomIdx = Math.floor(Math.random() * grid.length);
grid[randomIdx] = "sp";
setTimeout(updateGrid, 1500);
renderGrid();
}
function handleGridClick(evt) {
let i = parseInt(evt.target.id, 10)
if (grid[i] === "sp") {
points += 100;
grid[i] = null;
}
sound.currentTime = 0;
sound.play();
render();
}
function remainingTime() {
timer--;
if (timer > 0) {
setTimeout(remainingTime, 1000);
}
if (timer === 0) {
message = "Game over!";
if (points >= 500) {
message = "You saved humanity!";
} updateGrid();
}
render();
}
function playAgain(evt) {
if (timer === 0){
init();
startGame(evt);
}
}
function renderMessage() {
messageEl.innerText = message;
}
function renderPoints() {
const pointsEl = document.getElementById("points-display");
pointsEl.innerText = points;
}
function renderRemainingTime() {
const timerEl = document.getElementById("time-display");
timerEl.innerText = timer;
}
function renderGrid () {
grid.forEach(function(gridValue, gridIdx) {
const gridEl = document.getElementById(gridIdx)
if (gridValue !== null) {
gridEl.classList.add("spaceship");
} else {
gridEl.classList.remove("spaceship");
}
})
}
function render() {
renderRemainingTime();
renderPoints();
renderGrid();
renderMessage();
}