@@ -192,6 +192,9 @@ const CONFIG_DIFFICULTY = {
192192} ;
193193
194194function main ( ctime ) {
195+ const canvas = document . getElementById ( 'snakeCanvas' ) ;
196+ if ( ! canvas ) return ; // Exit loop if the game modal has been closed
197+
195198 window . requestAnimationFrame ( main ) ;
196199 if ( ( ctime - lastPaintTime ) / 1000 < 1 / speed ) {
197200 return ;
@@ -286,6 +289,18 @@ function gameEngine() {
286289 ctx . fillRect ( food . x * 20 , food . y * 20 , 18 , 18 ) ;
287290}
288291
292+ function restartGame ( ) {
293+ const selector = document . getElementById ( 'difficultySelect' ) ;
294+ applyDifficultySettings ( ) ;
295+ if ( selector ) selector . disabled = true ;
296+ document . getElementById ( 'game-over-overlay' ) . classList . add ( 'hidden' ) ;
297+ direction = { x : 1 , y : 0 } ;
298+ snakeArr = [ { x : 13 , y : 10 } ] ;
299+ score = 0 ;
300+ document . getElementById ( 'score' ) . innerHTML = score ;
301+ food = { x : 6 , y : 7 } ;
302+ }
303+
289304// Initialize execution listeners
290305function initSnakeGame ( ) {
291306 window . requestAnimationFrame ( main ) ;
@@ -333,6 +348,7 @@ function initSnakeGame() {
333348 applyDifficultySettings ( ) ;
334349 // Prevent changing parameter matrices on an active engine run
335350 if ( selector ) selector . disabled = true ;
351+ document . getElementById ( 'game-over-overlay' ) . classList . add ( 'hidden' ) ;
336352 direction = { x : 1 , y : 0 } ; // Start moving right
337353 } ) ;
338354
0 commit comments