-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
118 lines (105 loc) · 3.22 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
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
let moleTimeouts = [];
let score = 0;
let highScore = 0;
let gameTime = 30;
let lengthInterval;
/** Create our own forEach (for loop w/ callback) */
function forLoop(iterative, callback) {
for(let i = 0; i < iterative.length; i++) {
callback(iterative[i]);
}
}
/** Gets milliseconds based on a ceiling */
function getMilliseconds(ceiling) {
return Math.random() * ceiling * 1000;
}
/** Hides a mole */
function hideMole(mole) {
const timeout = getMilliseconds(3) + 1000;
moleTimeouts.push(setTimeout(function () {
// Remove class to hide the mole
mole.classList.remove('-up');
showMole(mole);
}, timeout));
}
/** Shows a mole */
function showMole(mole) {
const timeInterval = getMilliseconds(10);
moleTimeouts.push(setTimeout(function () {
// Add class to show the mole
mole.classList.add('-up');
hideMole(mole);
}, timeInterval));
}
/** Updates the current score in the DOM */
function updateHighScore() {
if (score > highScore) {
highScore = score;
document.getElementById('high-score').innerHTML = highScore;
}
}
/** Sets the current score */
function setCurrentScore(currentScore) {
document.getElementById('current-score').innerHTML = currentScore;
}
/** Updates the current score in the DOM */
function updateCurrentScore() {
score += 100;
updateHighScore();
setCurrentScore(score);
}
/** Handles a mole click by hiding the mole and updating the score */
function handleMoleClick(event) {
event.target.classList.remove('-up');
updateCurrentScore();
}
/** Set count down time */
function setCountdown(countdown) {
document.getElementById('time').innerHTML = countdown;
}
/** Updates the timer count down */
function updateCountdown() {
gameTime--;
setCountdown(gameTime);
// If time is up, at 0
if (!gameTime) {
setCountdown("Time's up!");
clearInterval(lengthInterval);
handleStopGame();
}
}
/** Handles the click of the start button and begins the game */
function handleStartGame() {
// Hide welcome screen
document.getElementById('welcome-screen').classList.add('-hidden');
// Show game
document.getElementById('game').classList.remove('-hidden');
const moles = document.querySelectorAll('.mole');
// Game length timer
lengthInterval = setInterval(updateCountdown, 1000);
// Add intervals and click events for each mole
forLoop(moles, function (mole) {
mole.addEventListener('click', handleMoleClick);
showMole(mole);
});
// Disabled start button
document.getElementById('start-button').disabled = 'true';
}
/** Stops the game in motion */
function handleStopGame() {
// Clear all timeouts
forLoop(moleTimeouts, function (timeout) { clearTimeout(timeout) });
clearInterval(lengthInterval);
}
/** Resets the game to starting point */
function handleResetGame() {
const moles = document.querySelectorAll('.mole');
forLoop(moles, function (mole) { mole.classList.remove('-up') });
clearInterval(lengthInterval);
handleStopGame();
gameTime = 30;
score = 0;
setCurrentScore(score);
setCountdown(gameTime);
document.getElementById('start-button').removeAttribute('disabled');
}