@@ -3,6 +3,7 @@ const scoreDisplay = document.getElementById("score");
33const timeLeftDisplay = document . getElementById ( "time-left" ) ;
44const difficultySelect = document . getElementById ( "difficulty" ) ;
55const startBtn = document . getElementById ( "start-btn" ) ;
6+ const resetBtn = document . getElementById ( "reset-btn" ) ;
67
78let score = 0 ;
89let 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+ } ) ;
0 commit comments