From 437c94ed54c06cf45a302799b156ac807f5d5ac8 Mon Sep 17 00:00:00 2001 From: "U:Bodigat" <89653017+ubodigat@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:33:45 +0100 Subject: [PATCH] 13.12.2024 | Anpassung High Score Fenster --- games/snake/scripts.js | 76 ++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/games/snake/scripts.js b/games/snake/scripts.js index e500e0b..d6437ba 100644 --- a/games/snake/scripts.js +++ b/games/snake/scripts.js @@ -10,14 +10,24 @@ let direction = 'LEFT'; let foodCollected = false; let score = 0; let highScores = JSON.parse(localStorage.getItem('highScores')) || []; +let gameInterval; +let isPaused = false; placeFood(); -setInterval(gameLoop, 300); +startGame(); document.addEventListener('keydown', keyDown); draw(); +function startGame() { + gameInterval = setInterval(gameLoop, 300); +} + +function pauseGame() { + clearInterval(gameInterval); +} + function draw() { ctx.fillStyle = '#066e46'; ctx.fillRect(0, 0, canvas.width, canvas.height); @@ -76,34 +86,36 @@ function shiftSnake() { } function gameLoop() { - testGameOver(); - if (foodCollected) { - snake = [{ x: snake[0].x, y: snake[0].y }, ...snake]; - foodCollected = false; - score++; - } - - shiftSnake(); - - if (direction == 'LEFT') { - snake[0].x--; - } - - if (direction == 'RIGHT') { - snake[0].x++; - } - - if (direction == 'UP') { - snake[0].y--; - } - - if (direction == 'DOWN') { - snake[0].y++; - } - - if (snake[0].x == food.x && snake[0].y == food.y) { - foodCollected = true; - placeFood(); + if (!isPaused) { + testGameOver(); + if (foodCollected) { + snake = [{ x: snake[0].x, y: snake[0].y }, ...snake]; + foodCollected = false; + score++; + } + + shiftSnake(); + + if (direction == 'LEFT') { + snake[0].x--; + } + + if (direction == 'RIGHT') { + snake[0].x++; + } + + if (direction == 'UP') { + snake[0].y--; + } + + if (direction == 'DOWN') { + snake[0].y++; + } + + if (snake[0].x == food.x && snake[0].y == food.y) { + foodCollected = true; + placeFood(); + } } } @@ -123,6 +135,8 @@ function keyDown(e) { } document.getElementById('highScoresLink').onclick = function() { + isPaused = true; + pauseGame(); let modal = document.getElementById('highScoresModal'); let allHighScores = document.getElementById('allHighScores'); allHighScores.innerHTML = ''; @@ -135,12 +149,16 @@ document.getElementById('highScoresLink').onclick = function() { } document.getElementsByClassName('close')[0].onclick = function() { + isPaused = false; + startGame(); document.getElementById('highScoresModal').style.display = 'none'; } window.onclick = function(event) { let modal = document.getElementById('highScoresModal'); if (event.target == modal) { + isPaused = false; + startGame(); modal.style.display = 'none'; } } \ No newline at end of file