Skip to content

Commit a74211a

Browse files
Add game over modal popup to Whack-a-Mole
1 parent a6e5f66 commit a74211a

1 file changed

Lines changed: 76 additions & 3 deletions

File tree

web-app/js/projects/whack-a-mole.js

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ function getWhackAMoleHTML() {
1313
<button class="btn-primary" id="resetWhackBtn">Reset</button>
1414
</div>
1515
<div id="whackMessage" class="whack-message">Hit the mole when it appears.</div>
16+
<div id="gameOverModal" class="whack-modal">
17+
<div class="whack-modal-content">
18+
<h2>🏆 Game Over!</h2>
19+
<p id="finalScoreText"></p>
20+
<p id="motivationalText"></p>
21+
22+
<div class="whack-modal-actions">
23+
<button id="playAgainBtn" class="btn-primary">Play Again</button>
24+
<button id="closeModalBtn" class="btn-primary">Close</button>
25+
</div>
26+
</div>
27+
</div>
1628
</div>
1729
</div>
1830
<style>
@@ -50,8 +62,39 @@ function getWhackAMoleHTML() {
5062
cursor: not-allowed;
5163
transform: none;
5264
}
53-
</style>
54-
`;
65+
.whack-modal {
66+
display: none;
67+
position: fixed;
68+
inset: 0;
69+
background: rgba(0, 0, 0, 0.7);
70+
justify-content: center;
71+
align-items: center;
72+
z-index: 9999;
73+
}
74+
75+
.whack-modal.show {
76+
display: flex;
77+
}
78+
79+
.whack-modal-content {
80+
background: #1f2937;
81+
color: white;
82+
padding: 2rem;
83+
border-radius: 16px;
84+
text-align: center;
85+
min-width: 320px;
86+
}
87+
88+
.whack-modal-actions {
89+
margin-top: 1rem;
90+
display: flex;
91+
gap: 10px;
92+
justify-content: center;
93+
}
94+
95+
</style>
96+
`;
97+
5598
}
5699

57100
function initWhackaMole() {
@@ -61,6 +104,11 @@ function initWhackaMole() {
61104
const scoreEl = document.getElementById('whackScore');
62105
const timeEl = document.getElementById('whackTime');
63106
const messageEl = document.getElementById('whackMessage');
107+
const gameOverModal = document.getElementById('gameOverModal');
108+
const finalScoreText = document.getElementById('finalScoreText');
109+
const motivationalText = document.getElementById('motivationalText');
110+
const playAgainBtn = document.getElementById('playAgainBtn');
111+
const closeModalBtn = document.getElementById('closeModalBtn');
64112

65113
if (!board || !startBtn || !resetBtn || !scoreEl || !timeEl || !messageEl) return;
66114

@@ -128,6 +176,23 @@ function initWhackaMole() {
128176

129177
moleId = setTimeout(showMole, 850);
130178
}
179+
function showGameOverModal() {
180+
181+
finalScoreText.textContent = `Your score: ${score}`;
182+
183+
if (score < 5) {
184+
motivationalText.textContent =
185+
"Keep practicing! You'll get faster!";
186+
} else if (score < 15) {
187+
motivationalText.textContent =
188+
"Nice job! Great reflexes!";
189+
} else {
190+
motivationalText.textContent =
191+
"Amazing! You're a Whack-a-Mole champion!";
192+
}
193+
194+
gameOverModal.classList.add('show');
195+
}
131196

132197
function stopGame(finalMessage) {
133198
gameActive = false;
@@ -142,7 +207,7 @@ function initWhackaMole() {
142207
hole.textContent = '🕳️';
143208
});
144209
if (window.AudioManager) AudioManager.play("game_over");
145-
messageEl.textContent = finalMessage;
210+
showGameOverModal();
146211
startBtn.disabled = false;
147212
}
148213

@@ -167,6 +232,14 @@ function initWhackaMole() {
167232
}
168233

169234
startBtn.addEventListener('click', startGame);
235+
playAgainBtn.addEventListener('click', () => {
236+
gameOverModal.classList.remove('show');
237+
startGame();
238+
});
239+
240+
closeModalBtn.addEventListener('click', () => {
241+
gameOverModal.classList.remove('show');
242+
});
170243
resetBtn.addEventListener('click', () => {
171244
clearInterval(timerId);
172245
timerId = null;

0 commit comments

Comments
 (0)