added life system + basic game over screen
This commit is contained in:
38
web/game.js
38
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;
|
||||
@@ -71,11 +94,12 @@ async function Movementloop() {
|
||||
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);
|
||||
|
||||
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)
|
||||
fill(255, 165, 0)
|
||||
circle(500, 100, 50);
|
||||
|
||||
score();
|
||||
text(time, 10, 20);
|
||||
|
||||
if (shot == false) {
|
||||
if (shot == false || hit == true) {
|
||||
shoot(playerPosX, playerPosY);
|
||||
} else {
|
||||
shot = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user