Merge pull request #5 from jpez-development/scoring

Scoring
This commit is contained in:
Joshua Perry 2022-05-29 14:14:27 +01:00 committed by GitHub
commit ee5ec25b69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 1 deletions

View File

@ -1,6 +1,7 @@
/* enemy spawning logic */ /* enemy spawning logic */
var aliens = []; var aliens = [];
var bombs = []; var bombs = [];
var score = 0;
/*Spawns Enemies*/ /*Spawns Enemies*/
function spawnEnemy() { function spawnEnemy() {
@ -12,7 +13,7 @@ function spawnEnemy() {
alien.style.left = Math.floor(Math.random() * document.body.offsetWidth) + "px"; alien.style.left = Math.floor(Math.random() * document.body.offsetWidth) + "px";
if (document.elementFromPoint(alien.offsetLeft, alien.offsetTop).classList.contains("alien") == false) { if (document.elementFromPoint(alien.offsetLeft, alien.offsetTop).classList.contains("alien") == false) {
document.body.appendChild(alien); document.body.appendChild(alien);
alienLogic = setInterval(spawnBomb, 3000); alienLogic = setInterval(spawnBomb, Math.floor(Math.random() * 3000-1000) + 1000);
aliens.push([alien, alienLogic]); aliens.push([alien, alienLogic]);
console.log("Alien Spawned") console.log("Alien Spawned")
break; break;
@ -29,6 +30,12 @@ function spawnBomb() {
bomb.style.top = "70px"; bomb.style.top = "70px";
bomb.style.zIndex = "1"; bomb.style.zIndex = "1";
bomb.style.display = "absolute"; bomb.style.display = "absolute";
if (Math.floor(Math.random() * 4) < 2) {
bomb.style.transform = "rotate(45deg)";
}
else if (Math.floor(Math.random() * 4) > 2) {
bomb.style.transform = "rotate(135deg)";
}
alien[0].appendChild(bomb); alien[0].appendChild(bomb);
bombs.push(bomb); bombs.push(bomb);
console.log("Bomb Spawned") console.log("Bomb Spawned")
@ -37,6 +44,14 @@ function spawnBomb() {
/*Makes a random bomb fall a set amount and explodes if colliding with the floor*/ /*Makes a random bomb fall a set amount and explodes if colliding with the floor*/
function fall() { function fall() {
for (let element of bombs) { for (let element of bombs) {
if (element.style.transform === "rotate(45deg)") {
element.style.top = (element.offsetTop + 10) + "px";
element.style.left = (element.offsetLeft + 10) + "px";
}
else if (element.style.transform === "rotate(135deg)") {
element.style.top = (element.offsetTop + 10) + "px";
element.style.left = (element.offsetLeft - 10) + "px";
}
element.style.top = (element.offsetTop + 10) + "px"; element.style.top = (element.offsetTop + 10) + "px";
var elemRect = element.getBoundingClientRect(); var elemRect = element.getBoundingClientRect();
var bodyRect = document.body.getBoundingClientRect(); var bodyRect = document.body.getBoundingClientRect();
@ -48,6 +63,8 @@ function fall() {
setTimeout(() => {element.remove()}, 3000); setTimeout(() => {element.remove()}, 3000);
bombs.splice(bombs.indexOf(element),1); bombs.splice(bombs.indexOf(element),1);
console.log("Bomb Despawned"); console.log("Bomb Despawned");
score += 1;
} }
} }
} }
@ -59,6 +76,7 @@ function checkExplosion() {
var elemRect = element.getBoundingClientRect(); var elemRect = element.getBoundingClientRect();
var playerRect = document.getElementById("player").getBoundingClientRect(); var playerRect = document.getElementById("player").getBoundingClientRect();
if (elemRect.bottom >= playerRect.top && elemRect.right >= playerRect.left && elemRect.left <= playerRect.right && elemRect.top-40 <= playerRect.bottom) { if (elemRect.bottom >= playerRect.top && elemRect.right >= playerRect.left && elemRect.left <= playerRect.right && elemRect.top-40 <= playerRect.bottom) {
score -= 1;
if (lives <= 1) { if (lives <= 1) {
document.getElementById("player").className = "character dead"; document.getElementById("player").className = "character dead";
endGame(); endGame();

View File

@ -19,6 +19,9 @@ function load() {
/*Starts game functionality*/ /*Starts game functionality*/
function startGame() { function startGame() {
if (document.body.contains(document.getElementById("scoreBoard"))) {
document.getElementById("scoreBoard").remove();
}
document.getElementById("player").className = "character"; document.getElementById("player").className = "character";
lives = 3; lives = 3;
showDisplay("block"); showDisplay("block");
@ -55,6 +58,49 @@ function endGame() {
showDisplay("none"); showDisplay("none");
button.style.display = "block"; button.style.display = "block";
text.style.display = "block"; text.style.display = "block";
setScore();
scoreBoard();
}
function setScore() {
let name = prompt("Input your initials (e.g. JFK)", "AAA");
localStorage.setItem(name, score);
}
function getScores() {
var values = [];
var keys = Object.keys(localStorage);
for (let i of keys) {
values.push([localStorage.getItem(i), i]);
}
return values;
}
function scoreBoard() {
var board = document.createElement("table");
var scores = getScores();
scores.sort(twoDimensionalSort);
for (let i in scores) {
var row = document.createElement("tr");
var col1 = document.createElement("td");
var col2 = document.createElement("td");
col1.innerHTML = scores[i][0];
col2.innerHTML = scores[i][1];
row.appendChild(col1);
row.appendChild(col2);
board.appendChild(row);
}
board.id = "scoreBoard";
board.style.marginTop = "250px";
board.style.marginLeft = "auto";
board.style.marginRight = "auto";
board.style.display = "table";
document.body.appendChild(board);
}
function twoDimensionalSort(a, b) {
return b[0] - a[0];
} }
document.addEventListener("DOMContentLoaded", load); document.addEventListener("DOMContentLoaded", load);