xxxxxxxxxx
206
// require https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js
// require /microgames/sketches/p5.play.js
let gameIsWon = false;
let gameIsStarted = false;
let gameIsDone = false;
const SCREEN_WIDTH = 240;
const SCREEN_HEIGHT = 160;
const PIXEL_FACTOR = 2;
const IMAGES = {};
let xWing = null;
const XWING_SPEED = 3;
const XWING_START_X = 40;
const XWING_START_Y = (SCREEN_HEIGHT * PIXEL_FACTOR) / 2;
const XWING_MOVE_INCREMENT = 10;
const XWING_TRIGGER_POSITION = 100;
let xWingIsMovingAway = false;
let laser = null;
let laserIsShot = false;
const LASER_SPEED = 8;
let thermalExhaustPort = null;
const TEP_MARGIN = 20;
let gameFrames = 0;
function preload() {
IMAGES.xwing = loadImage("XWing.png");
IMAGES.laser = loadImage("Laser.png");
IMAGES.tep = loadImage("TEP.png");
IMAGES.bg = loadImage("May4th_BG_GBA.png");
}
function setup() {
createCanvas(SCREEN_WIDTH * PIXEL_FACTOR, SCREEN_HEIGHT * PIXEL_FACTOR);
// calculate with GBA sizes
xWing = createSprite(XWING_START_X, XWING_START_Y, 40, 40);
xWing.addImage("main", IMAGES.xwing);
xWing.scale = 2;
thermalExhaustPort = createSprite(width - 20, height / 2, 20, 16);
thermalExhaustPort.addImage("main", IMAGES.tep);
thermalExhaustPort.scale = 2;
thermalExhaustPort.immovable = true;
laser = createSprite(-100, 0, 10, 3);
laser.addImage("main", IMAGES.laser);
laser.scale = 2;
noSmooth();
textSize(20);
}
function draw() {
background(60);
if (!gameIsStarted) {
push();
fill(255);
textAlign(CENTER);
text("Start Micro Game\n Press Space", width / 2, height / 2);
pop();
} else {
if (gameIsDone) {
push();
fill(255);
textAlign(CENTER);
text(gameIsWon ? "You won!" : "You lost…", width / 2, height / 2);
text("Restart: Press Space", width / 2, height / 2 + 40);
pop();
} else {
checkGame();
}
}
}
const shootLaser = () => {
if (!laserIsShot && !xWingIsMovingAway) {
console.log("shoot");
laserIsShot = true;
laser.position.x = xWing.position.x;
laser.position.y = xWing.position.y;
laser.setSpeed(LASER_SPEED, 0);
}
};
const checkGame = () => {
if( gameFrames < 60 ){
push();
fill(0);
textAlign(CENTER);
text("Destroy the Death Start", width / 2, 20);
pop();
}
image(IMAGES.bg, 0, 0, width, height);
if (!xWingIsMovingAway) {
if (keyIsDown(UP_ARROW)) {
xWing.position.y -= XWING_MOVE_INCREMENT;
}
if (keyIsDown(DOWN_ARROW)) {
xWing.position.y += XWING_MOVE_INCREMENT;
}
}
if (!gameIsWon) {
thermalExhaustPort.collide(laser, () => {
console.log("You hit it!");
if (gameIsStarted && !gameIsDone) {
gameIsWon = true;
}
});
}
if (xWing.position.x > width - XWING_TRIGGER_POSITION || laserIsShot) {
xWing.setSpeed(XWING_SPEED, -90);
xWing.rotation = -90;
xWingIsMovingAway = true;
}
if (xWingIsMovingAway) {
if (xWing.position.y < -80) {
if (laserIsShot) {
if (laser.position.x > width + 20) {
endGame();
}
} else {
endGame();
}
}
}
drawSprites();
if (gameIsWon) {
push();
rectMode(CENTER);
blendMode(SCREEN);
fill("red");
noStroke();
rect(
thermalExhaustPort.position.x,
thermalExhaustPort.position.y,
thermalExhaustPort.width,
thermalExhaustPort.height
);
pop();
}
gameFrames++;
};
const startGame = () => {
gameIsStarted = true;
laserIsShot = false;
xWing.position.x = XWING_START_X;
xWing.position.y = XWING_START_Y;
xWing.rotation = 0;
xWing.setSpeed(XWING_SPEED, 0);
xWingIsMovingAway = false;
laser.position.x = -100;
laser.position.y = 0;
laser.setSpeed(0, 0);
thermalExhaustPort.position.y = random(0 + TEP_MARGIN, height - TEP_MARGIN);
gameFrames = 0;
};
const resetGame = () => {
gameIsStarted = false;
gameIsDone = false;
gameIsWon = false;
};
const endGame = () => {
gameIsDone = true;
};
function keyPressed() {
if (key === " ") {
// start game
if (!gameIsStarted) {
startGame();
// restart game if game has ended
} else if (gameIsDone) {
resetGame();
// shoot if game is running
} else {
shootLaser();
}
}
}