Merge pull request #2 from jpez-development/spawning

Spawning
This commit is contained in:
Joshua Perry 2022-04-29 10:58:08 +01:00 committed by GitHub
commit 750edfac26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 6 deletions

View File

@ -5,6 +5,7 @@
<link rel="stylesheet" href="game.css" /> <link rel="stylesheet" href="game.css" />
<script src="scripts/launch.js"></script> <script src="scripts/launch.js"></script>
<script src="scripts/control.js"></script> <script src="scripts/control.js"></script>
<script src="scripts/enemy.js"></script>
</head> </head>
<body> <body>
@ -39,7 +40,6 @@
</div> </div>
<div class="alien" id="alien">
</body> </body>

View File

@ -1 +1,26 @@
/* enemy spawning logic */ /* enemy spawning logic */
var aliens = [];
function spawnEnemy() {
var alien = document.createElement("div");
alien.className = "alien";
alien.id = "alien";
while (true) {
alien.style.left = Math.floor(Math.random() * document.body.offsetWidth) + "px";
if (document.elementFromPoint(alien.offsetLeft, alien.offsetTop).classList.contains("alien") == false) {
document.body.appendChild(alien);
aliens.push(alien);
break;
}
}
}
function spawnBomb() {
var alien = aliens[0];
var bomb = document.createElement("div");
bomb.className = "bomb";
bomb.style.left = "28px";
bomb.style.top = "70px";
bomb.style.zIndex = "-1";
alien.appendChild(bomb);
}

View File

@ -1,14 +1,24 @@
/*Add events to webpage*/ /*Add events to webpage*/
function loadScripts() { function showDisplay(mode) {
for (let element of document.body.getElementsByTagName("*")) {
element.style.display = mode;
}
}
function load() {
document.addEventListener("keydown", getKey) document.addEventListener("keydown", getKey)
document.addEventListener("keyup", stop) document.addEventListener("keyup", stop)
setInterval(move, 10); setInterval(move, 10);
document.getElementById("start").addEventListener("click", startGame) document.getElementById("start").addEventListener("click", startGame)
showDisplay("none");
document.getElementById("start").style.display = "block";
} }
function startGame() { function startGame() {
var button = document.getElementById("start"); showDisplay("block");
button.style.display = "none"; document.getElementById("start").style.display = "none";
document.getElementsByClassName("weapon")[0].style.display = "none";
} }
document.addEventListener("DOMContentLoaded", loadScripts); document.addEventListener("DOMContentLoaded", load);