Skip to content

Commit 7e51eb7

Browse files
Kanavpreet-Singhsumn2u
authored andcommitted
added reset button to change difficulty levels
1 parent f01f16a commit 7e51eb7

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

examples/Whack-A-Mole/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ <h1>Whack-a-Mole 🦫</h1>
2020
</select>
2121
<p>Time: <span id="time-left">30</span>s</p>
2222
<button id="start-btn">Start Game</button>
23+
<button id="reset-btn" disabled>Reset Game</button>
2324
</div>
2425

2526
<div class="grid">

examples/Whack-A-Mole/index.mjs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const scoreDisplay = document.getElementById("score");
33
const timeLeftDisplay = document.getElementById("time-left");
44
const difficultySelect = document.getElementById("difficulty");
55
const startBtn = document.getElementById("start-btn");
6+
const resetBtn = document.getElementById("reset-btn");
67

78
let score = 0;
89
let gameActive = false;
@@ -100,13 +101,17 @@ startBtn.addEventListener("click", () => {
100101
scoreDisplay.textContent = score;
101102
gameActive = true;
102103
startBtn.textContent = "Playing...";
103-
const { totalTimeSec } = LEVELS[difficultySelect.value] || LEVELS.easy;
104+
startBtn.disabled = true;
105+
resetBtn.disabled = false;
106+
difficultySelect.disabled = true; // Lock difficulty during game
107+
108+
const { totalTimeSec } = LEVELS[difficultySelect.value] || LEVELS.beginner;
104109
timeLeftDisplay.textContent = totalTimeSec;
105110

106111
// Kick off the loop
107112
showMole();
108113

109-
// Stop after 30 seconds
114+
// Stop after specified duration
110115
clearTimeout(endTimer);
111116
clearInterval(tickTimer);
112117

@@ -127,6 +132,32 @@ startBtn.addEventListener("click", () => {
127132
timeLeftDisplay.textContent = 0;
128133
if (currentMole && currentMole.isConnected) currentMole.remove();
129134
startBtn.textContent = "Start Game";
135+
startBtn.disabled = false;
136+
difficultySelect.disabled = false; // Re-enable difficulty selection
130137
alert(`⏱️ Time's up! Your score: ${score}`);
131138
}, totalMs);
132139
});
140+
141+
// Reset game logic
142+
resetBtn.addEventListener("click", () => {
143+
if (!gameActive) return;
144+
145+
// Stop the game
146+
gameActive = false;
147+
clearTimeout(moleTimer);
148+
clearTimeout(endTimer);
149+
clearInterval(tickTimer);
150+
151+
// Clean up mole
152+
if (currentMole && currentMole.isConnected) currentMole.remove();
153+
154+
// Reset UI
155+
score = 0;
156+
scoreDisplay.textContent = score;
157+
const { totalTimeSec } = LEVELS[difficultySelect.value] || LEVELS.beginner;
158+
timeLeftDisplay.textContent = totalTimeSec;
159+
startBtn.textContent = "Start Game";
160+
startBtn.disabled = false;
161+
resetBtn.disabled = true;
162+
difficultySelect.disabled = false; // Allow difficulty change after reset
163+
});

examples/Whack-A-Mole/styles.css

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ h1 {
4444
font-weight: bold;
4545
}
4646

47-
#start-btn {
47+
#start-btn,
48+
#reset-btn {
4849
padding: 8px 20px;
4950
background: #fdbb2d;
5051
border: none;
@@ -54,11 +55,27 @@ h1 {
5455
transition: transform 0.2s, background 0.3s;
5556
}
5657

57-
#start-btn:hover {
58+
#start-btn:hover:not(:disabled),
59+
#reset-btn:hover:not(:disabled) {
5860
transform: scale(1.05);
5961
background: #ffc94d;
6062
}
6163

64+
#start-btn:disabled,
65+
#reset-btn:disabled {
66+
background: #555;
67+
cursor: not-allowed;
68+
opacity: 0.6;
69+
}
70+
71+
#reset-btn {
72+
background: #e74c3c;
73+
}
74+
75+
#reset-btn:hover:not(:disabled) {
76+
background: #c0392b;
77+
}
78+
6279
.grid {
6380
display: grid;
6481
grid-template-columns: repeat(3, 100px);

0 commit comments

Comments
 (0)