Question: Using p5.js how would i add code to read in data for the enemy ship location // Game state constants const LOADING = 0; const

Using p5.js how would i add code to read in data for the enemy ship location

// 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 ship

let enemyWave = []

let playerLasers = []

let playerOneImg

let playerLeftImg

let playerRightImg

let enemyShipImg

let playerLaserImg

let stars = []

let currentState = 0;

function preload() {

Galaga = loadImage('./img/galaga.png');

menuship = loadImage('./img/menuship.jpg');

loadship = loadImage('./img/loading.png');

gameover = loadImage('./img/finish.jpg');

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);

}

/*

* 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', 800, 800);

textSize(32);

fill(255, 102, 62);

}

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, 4);

}

}

}

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--) {

playerLasers.splice(i, 1);

}

}

function keyReleased() {

if (key != ' ') {

ship.setDir(0);

}

}

function keyPressed() {

if (key === ' ') {

let playerLaser = new PlayerLaser(ship.x, height - 200, playerLaserImg);

playerLasers.push(playerLaser);

}

if (keyCode === RIGHT_ARROW) {

ship.show(playerRightImg);

ship.setDir(1);

} else if (keyCode === LEFT_ARROW) {

ship.show(playerLeftImg);

ship.setDir(-1);

}

}

function drawGameOverScreen() {

background(0);

image(gameover, 500, 300);

gameover.resize(800, 400);

}

/*

* Mouse click triggers the transition between the menu and play screens.

*/

function mouseClicked(){

if (currentState == 0){

currentState = 1;

} else if (currentState == 1){

currentState = 2;

} else if (currentState == 2) {

currentState == 3;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!