-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmole.js
More file actions
104 lines (86 loc) · 2.59 KB
/
mole.js
File metadata and controls
104 lines (86 loc) · 2.59 KB
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
let score = 0;
let molesLeft = 30;
let popupLength = 3000;
let hideTimeout;
let clickable = false;
function popUpRandomMole() {
if (molesLeft <= 0) {
document
.querySelector(".sb__game-over")
.classList.remove("sb__game-over--hidden");
return;
}
const moleHeads = document.querySelectorAll(".wgs__mole-head");
if (moleHeads.length === 0) {
return;
}
const moleIndex = Math.floor(Math.random() * moleHeads.length);
const moleHead = moleHeads[moleIndex];
clickable = true;
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED
moleHead.classList.remove(
"wgs__mole-head--hidden",
"wgs__mole-head--whacked"
);
molesLeft -= 1;
document.querySelector(".sb__moles").innerHTML = molesLeft;
hideTimeout = setTimeout(() => hideMole(moleHead), popupLength);
}
function hideMole(mole) {
clickable = false;
mole.classList.add("wgs__mole-head--hidden");
setTimeout(popUpRandomMole, 500);
}
window.addEventListener("DOMContentLoaded", () => {
setTimeout(popUpRandomMole, 0);
const moleHeads = document.querySelectorAll(".wgs__mole-head");
for (let moleHead of moleHeads) {
moleHead.addEventListener("click", (event) => {
if (!clickable) return;
score += 1;
document.querySelector(".sb__score").innerHTML = score;
popupLength -= popupLength / 10;
clearTimeout(hideTimeout);
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED FOR THE BONUS
event.target.classList.add("wgs__mole-head--whacked");
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED
event.target.addEventListener(
"animationend",
() => {
event.target.classList.add("wgs__mole-head--hidden");
hideMole(event.target);
},
{ once: true }
);
});
}
});
// Audio controls
document.addEventListener("DOMContentLoaded", () => {
const bgAudio = document.getElementById("bg-audio");
const toggleBtn = document.getElementById("toggle-audio");
let musicStarted = false;
// Start music on first click anywhere
document.body.addEventListener(
"click",
() => {
if (!musicStarted) {
bgAudio.play().catch((err) => console.log("Autoplay blocked:", err));
toggleBtn.textContent = "🔊";
musicStarted = true;
}
},
{ once: true }
);
// Toggle button to pause/resume music
toggleBtn.addEventListener("click", (e) => {
e.stopPropagation(); // prevent triggering first-click play again
if (bgAudio.paused) {
bgAudio.play();
toggleBtn.textContent = "🔊";
} else {
bgAudio.pause();
toggleBtn.textContent = "🔇";
}
});
});