added comments

This commit is contained in:
Joshua Perry 2022-04-29 10:56:51 +01:00
parent 004c9755ef
commit 988843eb55
3 changed files with 12 additions and 0 deletions

View File

@ -8,6 +8,7 @@ function setVars() {
document.onloadend = setVars;
/*Controls vertical movement*/
function verticalMovement(keyCode) {
var playerLeft = player.offsetLeft;
var playerTop = player.offsetTop;
@ -33,6 +34,7 @@ function verticalMovement(keyCode) {
}
}
/*Controls horizontal movement*/
function horizontalMovement(keyCode) {
var playerLeft = player.offsetLeft;
var playerTop = player.offsetTop;
@ -58,6 +60,7 @@ function horizontalMovement(keyCode) {
}
}
/*Controls movement interval*/
function move() {
if (currentKey == 0) {
verticalMovement(lastKey);
@ -73,11 +76,13 @@ function move() {
}
/*Gets triggered keystroke*/
function getKey(event) {
lastKey = currentKey;
currentKey = event.keyCode;
}
/*Identifies if no keys are pressed*/
function stop(event) {
if (currentKey == lastKey) {
currentKey = 0;

View File

@ -2,6 +2,7 @@
var aliens = [];
var bombs = [];
/*Spawns Enemies*/
function spawnEnemy() {
var alien = document.createElement("div");
alien.className = "alien";
@ -19,6 +20,7 @@ function spawnEnemy() {
}
}
/*Spawns a bomb*/
function spawnBomb() {
var alien = aliens[Math.floor(Math.random() * aliens.length)];
var bomb = document.createElement("div");
@ -33,6 +35,7 @@ function spawnBomb() {
console.log("Bomb Spawned")
}
/*Makes a random bomb fall a set amount and explodes if colliding with the floor*/
function fall() {
var bomb = bombs[Math.floor(Math.random() * bombs.length)];
bomb[0].style.top = (bomb[0].offsetTop + 10) + "px";
@ -52,6 +55,7 @@ function fall() {
}
}
/*Checks to see if player is colliding with exploded bomb*/
function checkExplosion() {
for (let element of document.getElementsByClassName("explosion")) {
var elemRect = element.getBoundingClientRect();

View File

@ -6,6 +6,7 @@ function showDisplay(mode) {
}
}
/*Registers interval functions and trigger functions*/
function load() {
document.addEventListener("keydown", getKey)
document.addEventListener("keyup", stop)
@ -16,6 +17,7 @@ function load() {
document.getElementById("start").style.display = "block";
}
/*Starts game functionality*/
function startGame() {
showDisplay("block");
document.getElementById("start").style.display = "none";
@ -23,4 +25,5 @@ function startGame() {
setInterval(spawnEnemy, 2500);
setInterval(checkExplosion, 10);
}
document.addEventListener("DOMContentLoaded", load);