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 @@
+ diff --git a/style.css b/style.css index 2697b10f..4083cd18 100644 --- a/style.css +++ b/style.css @@ -28,3 +28,7 @@ body { background-color: red; border: 0.25vmin solid gray; } +#reset { + padding: 20px; + border: 1px solid; +} From e0dbd6196a5081eeaeb120f621d9ac87de52e23a Mon Sep 17 00:00:00 2001 From: mxxxOrz12 <402674230ma@gmail.com> Date: Fri, 17 May 2024 16:35:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86button=E7=9A=84?= =?UTF-8?q?=E7=82=B9=E5=87=BBbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 7 ++++--- style.css | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/game.js b/game.js index 2fccefcc..73580519 100644 --- a/game.js +++ b/game.js @@ -31,11 +31,12 @@ const checkGameOver = () => { return snakeOutOfBounds() || snakeIntersectSelf(); }; -const restart = () => { - document.getElementById("rest").addEventListener("click", () => { +const restart = document + .getElementById("reset") + .addEventListener("click", () => { resetGame(); }); -}; + const resetGame = () => { // Make sure the game loop is not still running clearInterval(gameLoop); diff --git a/style.css b/style.css index 4083cd18..56635baa 100644 --- a/style.css +++ b/style.css @@ -31,4 +31,7 @@ body { #reset { padding: 20px; border: 1px solid; + position: absolute; + top: 500px; + right: 100px; }