Using p5.js how would i add a health gui to this code: function Enemy(x, y, img) {
Question:
Using p5.js how would i add a health gui to this code:
function Enemy(x, y, img) {
this.x = x
this.y = y
this.r = 30
this.img = img
this.xdir = 2
this.grow = function() {
console.log('laser beam hit alien')
}
this.shiftDown = function() {
this.xdir *= -1
this.y += this.r
}
this.move = function(dir) {
this.x = this.x + this.xdir // move x direction
}
this.show = function() {
noStroke()
fill(255, 0, 200, 150)
rectMode(CENTER) // draw rectange via centre
ellipse(this.x, this.y, this.r * 2, this.r + 2)
imageMode(CENTER)
image(img, this.x, this.y)
}
}
// Game state constants
const LOADING = 0;
const MAIN_MENU = 1;
const PLAY = 2;
const GAME_OVER = 3;
let Galaga;
let menuship;
let loadship;
let gameover;
let loadsound;
let menusound;
let gamesound;
let file;
let ship;
let enemyWave = [];
let playerLasers = [];
let playerOneImg;
let playerLeftImg;
let playerRightImg;
let enemyShipImg;
let playerLaserImg;
let stars = [];
let currentState = 2;
function preload() {
Galaga = loadImage('./img/galaga.png');
menuship = loadImage('./img/menuship.jpg');
loadship = loadImage('./img/loading.png');
gameover = loadImage('./img/finish.jpg');
loadsound = loadSound('./sounds/load.mp3');
menusound = loadSound('./sounds/main.mp3');
gamesound = loadSound('./sounds/game.mp3');
file = loadStrings('./data/load.txt')
playerOneImg = loadImage("./img/player.png");
playerLeftImg = loadImage("./img/playerLeft.png");
playerRightImg = loadImage("./img/playerRight.png");
playerLaserImg = loadImage("./img/laserRed.png");
enemyShipImg = loadImage("./img/enemyShip.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
textSize(20);
ship = new Ship();
let randomAmountOfEnemys = random(0, 5);
for (let i = 0; i < randomAmountOfEnemys; i++) {
let randomHeight = random(10, 100);
let randomWidth = random(0, 300);
enemyWave[i] = new Enemy(i * 90 + 90, randomHeight, enemyShipImg);
}
for (let i = 0; i < 100; i++) {
stars[i] = new Star();
}
}
function draw() {
background(0);
if(currentState == 0){
drawLoadingScreen();
}
if(currentState == 1){
drawMainMenuScreen();
}
if(currentState == 2){
drawPlayScreen();
}
if(currentState == 3){
drawGameOverScreen();
}
}
/*
* Draw the loading screen
*/
function drawLoadingScreen(){
background(0);
image(loadship, 600, 300);
loadship.resize(800, 400);
loadsound.setVolume(0.02);
loadsound.play();
}
/*
* Draw the main menu screen
*/
function drawMainMenuScreen(){
background(0);
image(Galaga, 600, 300);
Galaga.resize(700, 300);
image(menuship, 200, 100);
text('Click to begin', 600, 60);
textSize(32);
fill(255, 255, 62);
text('Click to begin', 200, 600);
menusound.setVolume(0.02);
menusound.play();
}
function drawPlayScreen(){
background(0);
ship.show(playerOneImg);
ship.move();
for (let i = 0; i < stars.length; i++) {
stars[i].show();
stars[i].move();
}
for (let i = 0; i < playerLasers.length; i++) {
playerLasers[i].show();
playerLasers[i].move();
for (let j = 0; j < enemyWave.length; j++) {
if (playerLasers[i].hits(enemyWave[j])) {
enemyWave[j].grow();
playerLasers[i].evaporate();
enemyWave.splice(0, 1);
console.log('length of enemyWave ' + enemyWave.length);
}
}
}
let edge = false;
for (let i = 0; i < enemyWave.length; i++) {
enemyWave[i].show();
enemyWave[i].move();
if (enemyWave[i].x > width || enemyWave[i].x < 0) {
edge = true;
}
}
if (edge) {
for (let i = 0; i < enemyWave.length; i++) {
enemyWave[i].shiftDown();
}
}
for (let i = playerLasers.length - 1; i >= 0; i--) {
if (playerLasers[i].toDelete) {
playerLasers.splice(i, 1);
}
}
}
function keyReleased() {
if (key != ' ') {
ship.setDir(0);
}
}
function keyPressed() {
if (key === ' ') {
let playerLaser = new PlayerLaser(ship.x, height - 150, playerLaserImg)
playerLasers.push(playerLaser);
}
if (keyCode === RIGHT_ARROW) {
console.log('moving right');
ship.show(playerRightImg);
ship.setDir(1);
} else if (keyCode === LEFT_ARROW) {
console.log('moving left');
ship.show(playerLeftImg);
ship.setDir(-1);
}
}
function drawGameOverScreen() {
background(0);
gamesound.setVolume(0.1);
gamesound.loop();
image(gameover, 500, 300);
gameover.resize(800, 400);
}
function mouseClicked(){
if (currentState == 0){
currentState = 1;
} else if (currentState == 1){
currentState = 2;
} else if (currentState == 2) {
currentState == 3;
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill