From a7af93e26bab7a296508d7d3e20fdac83a135d93 Mon Sep 17 00:00:00 2001 From: mxxxOrz12 <402674230ma@gmail.com> Date: Fri, 17 May 2024 16:32:55 +0800 Subject: [PATCH 1/2] button --- game.js | 13 +++++++++---- index.html | 1 + style.css | 4 ++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/game.js b/game.js index 83e3077e..2fccefcc 100644 --- a/game.js +++ b/game.js @@ -1,13 +1,13 @@ const SNAKE_SPEED = 5; -const gameBoard = document.getElementById('game-board'); +const gameBoard = document.getElementById("game-board"); let isGameOver = false; const main = () => { update(); draw(); if (isGameOver) { - alert('Game Over'); + alert("Game Over"); clearInterval(gameLoop); } }; @@ -15,14 +15,14 @@ const main = () => { let gameLoop = setInterval(main, 1000 / SNAKE_SPEED); const update = () => { - console.log('Updating'); + console.log("Updating"); updateSnake(); updateFood(); isGameOver = checkGameOver(); }; const draw = () => { - gameBoard.innerHTML = ''; + gameBoard.innerHTML = ""; drawSnake(gameBoard); drawFood(gameBoard); }; @@ -31,6 +31,11 @@ const checkGameOver = () => { return snakeOutOfBounds() || snakeIntersectSelf(); }; +const restart = () => { + document.getElementById("rest").addEventListener("click", () => { + resetGame(); + }); +}; const resetGame = () => { // Make sure the game loop is not still running clearInterval(gameLoop); diff --git a/index.html b/index.html index 90a58140..dac3c290 100644 --- a/index.html +++ b/index.html @@ -14,5 +14,6 @@
+