Skip to content

Commit 4fecf32

Browse files
committed
finish video 30
1 parent 5682d26 commit 4fecf32

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

30 - Whack A Mole/index-START.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,54 @@ <h1>Whack-a-mole! <span class="score">0</span></h1>
3636
const holes = document.querySelectorAll('.hole');
3737
const scoreBoard = document.querySelector('.score');
3838
const moles = document.querySelectorAll('.mole');
39+
let lastHole;
40+
let timeUp = false;
41+
let score = 0;
3942

43+
function randomTime(min, max) {
44+
return Math.round(Math.random() * (max - min) + min);
45+
}
46+
47+
function randomHole(holes) {
48+
const index = Math.floor(Math.random() * holes.length);
49+
const hole = holes[index];
50+
51+
if (hole === lastHole) {
52+
return randomHole(holes);
53+
}
54+
55+
lastHole = hole;
56+
return hole;
57+
}
58+
59+
function peep() {
60+
const time = randomTime(200, 1000);
61+
const hole = randomHole(holes);
62+
63+
hole.classList.add('up');
64+
setTimeout(() => {
65+
hole.classList.remove('up');
66+
if(!timeUp) peep();
67+
}, time);
68+
}
69+
70+
function startGame() {
71+
scoreBoard.textContent = 0;
72+
timeUp = false;
73+
score = 0;
74+
peep();
75+
76+
setTimeout(() => timeUp = true, 10000);
77+
}
78+
79+
function bonk(e) {
80+
if(!e.isTrusted) return; // cheater!
81+
score ++;
82+
this.classList.remove('up');
83+
scoreBoard.textContent = score;
84+
}
85+
86+
moles.forEach(mole => mole.addEventListener('click', bonk));
4087
</script>
4188
</body>
4289
</html>

0 commit comments

Comments
 (0)