From f3e613810be63bf01187013c00cb3ce43098c5b7 Mon Sep 17 00:00:00 2001 From: Joshua Perry <45966243+jpez-development@users.noreply.github.com> Date: Fri, 29 Apr 2022 12:10:37 +0100 Subject: [PATCH] added restart functionality --- game.html | 2 +- scripts/enemy.js | 2 +- scripts/launch.js | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/game.html b/game.html index 78bafb6..0512622 100644 --- a/game.html +++ b/game.html @@ -19,7 +19,7 @@
- Start + Click to Play
diff --git a/scripts/enemy.js b/scripts/enemy.js index 8759e1c..78bc4f5 100644 --- a/scripts/enemy.js +++ b/scripts/enemy.js @@ -40,7 +40,7 @@ function fall() { element.style.top = (element.offsetTop + 10) + "px"; var closestElement = document.elementFromPoint(element.offsetLeft, element.offsetTop+10); if (!closestElement.classList.contains("sky") && !closestElement.classList.contains("alien") && !closestElement.classList.contains("bomb") && !closestElement.classList.contains("explosion")) { - if(Math.floor(Math.random() * 100) < 10 || element.bottom >= document.body.bottom) { + if(Math.floor(Math.random() * 100) < 10 || element.bottom >= document.body.bottom || closestElement.classList.contains("character")) { element.className = "explosion"; console.log("Explosion") setTimeout(() => {element.remove()}, 3000); diff --git a/scripts/launch.js b/scripts/launch.js index 98ce956..b7d64c0 100644 --- a/scripts/launch.js +++ b/scripts/launch.js @@ -11,7 +11,6 @@ function showDisplay(mode) { function load() { document.addEventListener("keydown", getKey) document.addEventListener("keyup", stop) - intervals.push(setInterval(move, 10)); document.getElementById("start").addEventListener("click", startGame) showDisplay("none"); @@ -20,9 +19,11 @@ function load() { /*Starts game functionality*/ function startGame() { + document.getElementById("player").className = "character"; showDisplay("block"); document.getElementById("start").style.display = "none"; document.getElementsByClassName("weapon")[0].style.display = "none"; + intervals.push(setInterval(move, 10)); intervals.push(setInterval(spawnEnemy, 2500)); setTimeout(intervals.push(setInterval(fall, 50)), 2500); intervals.push(setInterval(checkExplosion, 1)); @@ -45,6 +46,14 @@ function endGame() { clearInterval(alien[1]); alien[0].remove(); } + + var button = document.getElementById("start"); + var text = document.createElement("p"); + text.innerHTML = "GAME OVER"; + button.appendChild(text); + showDisplay("none"); + button.style.display = "block"; + text.style.display = "block"; } document.addEventListener("DOMContentLoaded", load);