diff --git a/web/game.js b/web/game.js index 621f9dd..c1e07a7 100644 --- a/web/game.js +++ b/web/game.js @@ -10,12 +10,14 @@ let playerPosX = 500 let playerPosY = 300; let playerSpeed = 3; let booleanArray = window.booleanArray; +let lives = 2; let shotSpeed = 5; let projectile; let projSize = 5; let shot; +let hit; let shotPosX = 500; let shotPosY = 100; @@ -38,6 +40,27 @@ async function score() { } } +function life() { + textAlign(CENTER); + text(lives, 500, 20); + if (hit == true) { + playerPosX = 500; + playerPosY = 300; + hit = false; + lives -= 1; + } + if (lives == 0) { + fill(255, 0, 0); + textSize(40); + textAlign(CENTER); + text("Game Over", 500, 300); + } + if (key == ' ') { + lives = 2; + time = 0; + } +} + function keyPressed() { if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) { playerPosX -= playerSpeed; @@ -58,24 +81,25 @@ async function Movementloop() { // event.detail contains the booleanArray let booleanArray = event.detail; // Use booleanArray here... - - if (booleanArray[1]) { - playerPosX += playerSpeed; - } - if (booleanArray[3]) { - playerPosX -= playerSpeed; - } - if (booleanArray[2]) { - playerPosY += playerSpeed; - } - if (booleanArray[0]) { - playerPosY -= playerSpeed; - } -}); + + if (booleanArray[1]) { + playerPosX += playerSpeed; + } + if (booleanArray[3]) { + playerPosX -= playerSpeed; + } + if (booleanArray[2]) { + playerPosY += playerSpeed; + } + if (booleanArray[0]) { + playerPosY -= playerSpeed; + } + }); } function shoot(directionX, directionY) { shot = true; + hit = false; if (!projectile) { // Set the initial position of the projectile to the player's position @@ -97,7 +121,7 @@ function shoot(directionX, directionY) { if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) { projectile = null; - shot = false; + hit = true; } else if (projectile.x < 0 || projectile.x > width || projectile.y < 0 || projectile.y > height) { projectile = null; @@ -107,24 +131,30 @@ function shoot(directionX, directionY) { // the function draw() is called every frame function draw() { + textSize(10); + textAlign(LEFT); keyPressed(); + life(); // draw background background(0, 0, 0, 100); - // draw player - fill(0, 255, 255) - circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize); - - // draw boss - fill(255,165,0) - circle(500, 100, 50); + if (!(lives == 0)) { + // draw player + fill(0, 255, 255) + circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize); - score(); - text(time, 10, 20); - - if (shot == false) { - shoot(playerPosX, playerPosY); - } else { - shot = false; + // draw boss + fill(255, 165, 0) + circle(500, 100, 50); + + score(); + text(time, 10, 20); + + if (shot == false || hit == true) { + shoot(playerPosX, playerPosY); + } else { + shot = false; + } } + } \ No newline at end of file