added restart functionality
This commit is contained in:
parent
729056c200
commit
f3e613810b
|
|
@ -19,7 +19,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="start" id="start">
|
<div class="start" id="start">
|
||||||
Start
|
Click to Play
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="sky">
|
<div class="sky">
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ function fall() {
|
||||||
element.style.top = (element.offsetTop + 10) + "px";
|
element.style.top = (element.offsetTop + 10) + "px";
|
||||||
var closestElement = document.elementFromPoint(element.offsetLeft, element.offsetTop+10);
|
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 (!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";
|
element.className = "explosion";
|
||||||
console.log("Explosion")
|
console.log("Explosion")
|
||||||
setTimeout(() => {element.remove()}, 3000);
|
setTimeout(() => {element.remove()}, 3000);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ function showDisplay(mode) {
|
||||||
function load() {
|
function load() {
|
||||||
document.addEventListener("keydown", getKey)
|
document.addEventListener("keydown", getKey)
|
||||||
document.addEventListener("keyup", stop)
|
document.addEventListener("keyup", stop)
|
||||||
intervals.push(setInterval(move, 10));
|
|
||||||
document.getElementById("start").addEventListener("click", startGame)
|
document.getElementById("start").addEventListener("click", startGame)
|
||||||
|
|
||||||
showDisplay("none");
|
showDisplay("none");
|
||||||
|
|
@ -20,9 +19,11 @@ function load() {
|
||||||
|
|
||||||
/*Starts game functionality*/
|
/*Starts game functionality*/
|
||||||
function startGame() {
|
function startGame() {
|
||||||
|
document.getElementById("player").className = "character";
|
||||||
showDisplay("block");
|
showDisplay("block");
|
||||||
document.getElementById("start").style.display = "none";
|
document.getElementById("start").style.display = "none";
|
||||||
document.getElementsByClassName("weapon")[0].style.display = "none";
|
document.getElementsByClassName("weapon")[0].style.display = "none";
|
||||||
|
intervals.push(setInterval(move, 10));
|
||||||
intervals.push(setInterval(spawnEnemy, 2500));
|
intervals.push(setInterval(spawnEnemy, 2500));
|
||||||
setTimeout(intervals.push(setInterval(fall, 50)), 2500);
|
setTimeout(intervals.push(setInterval(fall, 50)), 2500);
|
||||||
intervals.push(setInterval(checkExplosion, 1));
|
intervals.push(setInterval(checkExplosion, 1));
|
||||||
|
|
@ -45,6 +46,14 @@ function endGame() {
|
||||||
clearInterval(alien[1]);
|
clearInterval(alien[1]);
|
||||||
alien[0].remove();
|
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);
|
document.addEventListener("DOMContentLoaded", load);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue