From b8215df3fc1879c2e05ab6932c2186d28dff27d8 Mon Sep 17 00:00:00 2001 From: Priyanshu Date: Sat, 22 Aug 2026 00:12:27 +0530 Subject: [PATCH] fix: add restart button after game over in Space Invaders --- Games/Space_Invaders/index.html | 8 +++++- Games/Space_Invaders/space.css | 43 +++++++++++++++++++++++++++++++++ Games/Space_Invaders/space.js | 35 ++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Games/Space_Invaders/index.html b/Games/Space_Invaders/index.html index e59bd3b408..fa6ce29a91 100644 --- a/Games/Space_Invaders/index.html +++ b/Games/Space_Invaders/index.html @@ -15,6 +15,12 @@ font-size: 30px; padding: 5px; ">

Space Invaders

- +
+ +
+

Game Over

+ +
+
\ No newline at end of file diff --git a/Games/Space_Invaders/space.css b/Games/Space_Invaders/space.css index 17e623047b..866ffcd693 100644 --- a/Games/Space_Invaders/space.css +++ b/Games/Space_Invaders/space.css @@ -5,4 +5,47 @@ body { #board { background-color: black; +} + +.game-wrapper { + position: relative; + display: inline-block; +} + +#game-over-screen { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + background-color: rgba(0, 0, 0, 0.75); + color: white; +} + +#final-score { + font-size: 20px; + margin-bottom: 15px; +} + +#restart-btn { + padding: 10px 20px; + font-family: 'Courier New', Courier, monospace; + font-size: 16px; + cursor: pointer; + background-color: white; + color: black; + border: none; + border-radius: 4px; +} + +#restart-btn:hover { + background-color: #dddddd; +} + +.hide { + display: none; } \ No newline at end of file diff --git a/Games/Space_Invaders/space.js b/Games/Space_Invaders/space.js index 2642925dca..2d3837dd95 100644 --- a/Games/Space_Invaders/space.js +++ b/Games/Space_Invaders/space.js @@ -44,12 +44,21 @@ let bulletVelocityY = -10; //bullet moving speed let score = 0; let gameOver = false; +let gameOverScreen; +let finalScoreElem; +let restartBtn; + window.onload = function() { board = document.getElementById("board"); board.width = boardWidth; board.height = boardHeight; context = board.getContext("2d"); //used for drawing on the board + gameOverScreen = document.getElementById("game-over-screen"); + finalScoreElem = document.getElementById("final-score"); + restartBtn = document.getElementById("restart-btn"); + restartBtn.addEventListener("click", restartGame); + //draw initial ship // context.fillStyle="green"; // context.fillRect(ship.x, ship.y, ship.width, ship.height); @@ -70,6 +79,30 @@ window.onload = function() { document.addEventListener("keyup", shoot); } +function endGame() { + gameOver = true; + finalScoreElem.textContent = "Game Over — Score: " + score; + gameOverScreen.classList.remove("hide"); +} + +function restartGame() { + gameOverScreen.classList.add("hide"); + + ship.x = shipX; + ship.y = shipY; + + alienArray = []; + bulletArray = []; + alienColumns = 3; + alienRows = 2; + alienVelocityX = 1; + + score = 0; + gameOver = false; + + createAliens(); +} + function update() { requestAnimationFrame(update); @@ -101,7 +134,7 @@ function update() { context.drawImage(alienImg, alien.x, alien.y, alien.width, alien.height); if (alien.y >= ship.y) { - gameOver = true; + endGame(); } } }