@@ -10,6 +10,7 @@ const startButton = document.getElementById("startButton");
1010const gameContainer = document . querySelector ( ".game-container" ) ;
1111const canvasContainer = document . querySelector ( ".canvas-container" ) ;
1212const easterEggVideo = document . getElementById ( "easterEggVideo" ) ;
13+ const pausedMessageElement = document . getElementById ( "pausedMessage" ) ;
1314
1415// --- Audio Elements ---
1516const eatSound = new Audio ( 'media/eat.mp3' ) ;
@@ -39,6 +40,7 @@ let changingDirection = false;
3940let gameLoopInterval = null ;
4041let gameActive = false ;
4142let currentSpeed = INITIAL_SPEED_MS ;
43+ let isPaused = false ;
4244
4345// --- Touch Controls ---
4446const btnUp = document . getElementById ( "btnUp" ) ;
@@ -127,7 +129,7 @@ function drawFood() {
127129}
128130
129131function moveSnake ( ) {
130- if ( ! gameActive ) return ;
132+ if ( ! gameActive || isPaused ) return ;
131133
132134 // snake[0] is the head
133135 const head = { x : snake [ 0 ] . x + dx , y : snake [ 0 ] . y + dy } ;
@@ -194,7 +196,7 @@ function handleKeyDown(event) {
194196 return ;
195197 }
196198
197- if ( ! gameActive || changingDirection ) return ;
199+ if ( ! gameActive || changingDirection || isPaused ) return ;
198200
199201 const keyPressed = event . key ;
200202
@@ -231,7 +233,7 @@ function handleKeyDown(event) {
231233// handle touch control for mobile players
232234function handleTouchControl ( newDx , newDy ) {
233235 // make sure the previous direction is executed before a new direction is set
234- if ( changingDirection || ! gameActive ) return ;
236+ if ( changingDirection || ! gameActive || isPaused ) return ;
235237
236238 // just translate condition to booleans for better readability
237239 const goingUp = dy === - 1 ;
@@ -272,6 +274,7 @@ function checkGameOver() {
272274}
273275
274276function gameLoop ( ) {
277+ if ( isPaused ) return ;
275278 if ( checkGameOver ( ) ) {
276279 endGame ( ) ;
277280 return ;
@@ -291,9 +294,11 @@ function startGame() {
291294 dx = 1 ;
292295 dy = 0 ;
293296 changingDirection = false ;
297+ isPaused = false ;
294298 scoreElement . textContent = `Score: ${ score } ` ;
295299 gameOverElement . classList . add ( "hidden" ) ;
296300 startMessageElement . classList . add ( "hidden" ) ;
301+ pausedMessageElement . classList . add ( "hidden" ) ;
297302
298303 const startX = Math . floor ( GRID_SIZE / 2 ) ;
299304 const startY = Math . floor ( GRID_SIZE / 2 ) ;
@@ -385,6 +390,34 @@ function triggerEasterEgg() {
385390 } ;
386391}
387392
393+ // Function to pause the game
394+ function pauseGame ( ) {
395+ isPaused = true ;
396+ pausedMessageElement . classList . remove ( "hidden" ) ;
397+ }
398+
399+ // Function to resume the game
400+ function resumeGame ( ) {
401+ isPaused = false ;
402+ pausedMessageElement . classList . add ( "hidden" ) ;
403+ }
404+
405+ // Function to handle space key press
406+ document . addEventListener ( 'keydown' , ( event ) => {
407+ if ( event . code === 'Space' ) {
408+ // is game over or not started, play the game
409+ if ( ! gameActive &&
410+ ( ! gameOverElement . classList . contains ( "hidden" ) || ! startMessageElement . classList . contains ( "hidden" ) )
411+ ) {
412+ startGame ( ) ;
413+ } else if ( isPaused ) {
414+ resumeGame ( ) ;
415+ } else {
416+ pauseGame ( ) ;
417+ }
418+ }
419+ } ) ;
420+
388421// --- Event Listeners ---
389422document . addEventListener ( "keydown" , handleKeyDown ) ;
390423restartButton . addEventListener ( "click" , startGame ) ;
0 commit comments