added life system + basic game over screen

This commit is contained in:
Mees Roelofsz
2023-11-26 16:44:18 +01:00
parent e0cb5c2417
commit 328f494da0

View File

@@ -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;
@@ -59,23 +82,24 @@ async function Movementloop() {
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);
if (!(lives == 0)) {
// 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);
// draw boss
fill(255, 165, 0)
circle(500, 100, 50);
score();
text(time, 10, 20);
score();
text(time, 10, 20);
if (shot == false) {
shoot(playerPosX, playerPosY);
} else {
shot = false;
if (shot == false || hit == true) {
shoot(playerPosX, playerPosY);
} else {
shot = false;
}
}
}