added life system + basic game over screen
This commit is contained in:
34
web/game.js
34
web/game.js
@@ -10,12 +10,14 @@ let playerPosX = 500
|
|||||||
let playerPosY = 300;
|
let playerPosY = 300;
|
||||||
let playerSpeed = 3;
|
let playerSpeed = 3;
|
||||||
let booleanArray = window.booleanArray;
|
let booleanArray = window.booleanArray;
|
||||||
|
let lives = 2;
|
||||||
|
|
||||||
|
|
||||||
let shotSpeed = 5;
|
let shotSpeed = 5;
|
||||||
let projectile;
|
let projectile;
|
||||||
let projSize = 5;
|
let projSize = 5;
|
||||||
let shot;
|
let shot;
|
||||||
|
let hit;
|
||||||
let shotPosX = 500;
|
let shotPosX = 500;
|
||||||
let shotPosY = 100;
|
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() {
|
function keyPressed() {
|
||||||
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
|
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
|
||||||
playerPosX -= playerSpeed;
|
playerPosX -= playerSpeed;
|
||||||
@@ -76,6 +99,7 @@ async function Movementloop() {
|
|||||||
|
|
||||||
function shoot(directionX, directionY) {
|
function shoot(directionX, directionY) {
|
||||||
shot = true;
|
shot = true;
|
||||||
|
hit = false;
|
||||||
|
|
||||||
if (!projectile) {
|
if (!projectile) {
|
||||||
// Set the initial position of the projectile to the player's position
|
// 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) {
|
if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) {
|
||||||
projectile = null;
|
projectile = null;
|
||||||
shot = false;
|
hit = true;
|
||||||
}
|
}
|
||||||
else if (projectile.x < 0 || projectile.x > width || projectile.y < 0 || projectile.y > height) {
|
else if (projectile.x < 0 || projectile.x > width || projectile.y < 0 || projectile.y > height) {
|
||||||
projectile = null;
|
projectile = null;
|
||||||
@@ -107,10 +131,14 @@ function shoot(directionX, directionY) {
|
|||||||
|
|
||||||
// the function draw() is called every frame
|
// the function draw() is called every frame
|
||||||
function draw() {
|
function draw() {
|
||||||
|
textSize(10);
|
||||||
|
textAlign(LEFT);
|
||||||
keyPressed();
|
keyPressed();
|
||||||
|
life();
|
||||||
// draw background
|
// draw background
|
||||||
background(0, 0, 0, 100);
|
background(0, 0, 0, 100);
|
||||||
|
|
||||||
|
if (!(lives == 0)) {
|
||||||
// draw player
|
// draw player
|
||||||
fill(0, 255, 255)
|
fill(0, 255, 255)
|
||||||
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
|
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
|
||||||
@@ -122,9 +150,11 @@ function draw() {
|
|||||||
score();
|
score();
|
||||||
text(time, 10, 20);
|
text(time, 10, 20);
|
||||||
|
|
||||||
if (shot == false) {
|
if (shot == false || hit == true) {
|
||||||
shoot(playerPosX, playerPosY);
|
shoot(playerPosX, playerPosY);
|
||||||
} else {
|
} else {
|
||||||
shot = false;
|
shot = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user