Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0220669

Browse files
committed
Mole game with standard functionality
1 parent ae2046b commit 0220669

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

30 - Whack A Mole/index-START.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,44 @@ <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 lastIndex;
40+
let timeUp = true;
41+
42+
function randomTime(min, max) {
43+
return Math.round(Math.random() * (max - min) + min);
44+
}
3945

46+
function randomHole(holes) {
47+
const index = Math.floor(Math.random() * holes.length);
48+
if (index === lastIndex) {
49+
return randomHole(holes);
50+
}
51+
lastIndex = index;
52+
return holes[index];
53+
}
54+
55+
function showMole() {
56+
const hole = randomHole(holes);
57+
hole.classList.add('up');
58+
setTimeout(() => {
59+
hole.classList.remove('up');
60+
if (!timeUp) showMole();
61+
}, randomTime(200, 1000));
62+
}
63+
64+
function startGame() {
65+
timeUp = false;
66+
scoreBoard.textContent = 0;
67+
showMole();
68+
setTimeout(() => timeUp = true);
69+
}
70+
71+
function cachedMole() {
72+
scoreBoard.textContent = parseInt(scoreBoard.textContent) + 1;
73+
this.parentElement.classList.remove('up');
74+
}
75+
76+
moles.forEach(mole => mole.addEventListener('click', cachedMole));
4077
</script>
4178
</body>
4279
</html>

0 commit comments

Comments
 (0)