Skip to content

Commit 99bf82d

Browse files
committed
Lesson 30
1 parent 832a987 commit 99bf82d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

30 - Whack A Mole/index-START.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,61 @@ <h1>Whack-a-mole! <span class="score">0</span></h1>
3939
const holes = document.querySelectorAll('.hole');
4040
const scoreBoard = document.querySelector('.score');
4141
const moles = document.querySelectorAll('.mole');
42+
let lastHole;
43+
let timeUp = false;
44+
let score = 0;
45+
46+
function randomTime(min, max) {
47+
return Math.round(Math.random() * (max - min + min));
48+
}
49+
50+
function randomHole(holes) {
51+
const index = Math.floor(Math.random() * holes.length);
52+
const hole = holes[index];
53+
54+
if (hole === lastHole) {
55+
return randomHole(holes);
56+
}
57+
58+
lastHole = hole;
59+
60+
return hole;
61+
}
62+
63+
function peep() {
64+
const time = randomTime(200, 1000);
65+
const hole = randomHole(holes);
66+
67+
hole.classList.add('up');
68+
setTimeout(() => {
69+
hole.classList.remove('up');
70+
71+
if (!timeUp) {
72+
peep();
73+
}
74+
}, time);
75+
}
76+
77+
function startGame() {
78+
scoreBoard.textContent = 0;
79+
timeUp = false;
80+
score = 0;
81+
peep();
82+
83+
setTimeout(() => (timeUp = true), 10000);
84+
}
85+
86+
function bonk(event) {
87+
if (!event.isTrusted) {
88+
return;
89+
}
90+
91+
score += 100;
92+
this.classList.remove('up');
93+
scoreBoard.textContent = score;
94+
}
95+
96+
moles.forEach(mole => mole.addEventListener('click', bonk));
4297
</script>
4398
</body>
4499
</html>

0 commit comments

Comments
 (0)