-
Notifications
You must be signed in to change notification settings - Fork 0
/
mole.js
58 lines (43 loc) · 1.43 KB
/
mole.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
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;
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);
hideMole(event.target);
event.target.classList.add('wgs__mole-head--hidden');
//event.target.classList.add('wgs__mole-head--whacked');
});
}
});