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;
@@ -76,6 +99,7 @@ async function Movementloop() {
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,10 +131,14 @@ 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);
if (!(lives == 0)) {
// draw player
fill(0, 255, 255)
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
@@ -122,9 +150,11 @@ function draw() {
score();
text(time, 10, 20);
if (shot == false) {
if (shot == false || hit == true) {
shoot(playerPosX, playerPosY);
} else {
shot = false;
}
}
}