added restart functionality

This commit is contained in:
Joshua Perry 2022-04-29 12:10:37 +01:00
parent 729056c200
commit f3e613810b
3 changed files with 12 additions and 3 deletions

View File

@ -19,7 +19,7 @@
</div>
<div class="start" id="start">
Start
Click to Play
</div>
<div class="sky">

View File

@ -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);

View File

@ -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);