The spaceship engages in fights with enemies by using energy machine guns and lasers. The most challenging part actually comes in making all the visuals work correctly.
Four characters/sounds:
- Firing spaceship
- Explosion
- Charge laser
- Shoot laser
//Jason Jiang
//Section E
///Storyline: The spaceship engages fights with enemies through using energy machine gun and laser.
//setting up variables for stars
var star = [];
var N = 10; //number of starts
var dx = 10; //speed of star
var w = 600; //x location of enemy
var dShip = 10; //speed of enemy
var a = 0;//scale of laser beam
var imgX = 120;//x location of ship
var imgY = 100;//y location of ship
var angle = 0;//angle of ship
var laserCharged = false; //whether laser is charged
var laserFired = false; //whether laser is fired
var enemyDestroyed = false; //whether enemy is destroyed
//defining assets
var machineGun;
var explosion1;
var explosion2;
var explosion3;
var laserEnergy;
var laserBeam;
var flyEngine;
var machineGun;
var explosion;
var charge;
var laser;
//add assets
function preload(){
spaceShip = loadImage("https://i.imgur.com/D1QDG4A.png");
spaceShipFire = loadImage("https://i.imgur.com/h8bQMXa.png");
enemy1 = loadImage("https://i.imgur.com/OE8l2tK.png");
explosion1 = loadImage("https://i.imgur.com/c9tLLTo.png");
explosion2 = loadImage("https://i.imgur.com/kLTBXIa.png");
explosion3 = loadImage("https://i.imgur.com/0zhmrzn.png");
laserEnergy = loadImage("https://i.imgur.com/bmn9wZX.png");
laserBeam = loadImage("https://i.imgur.com/ABposfH.png");
machineGun = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/machineGun.mp3");
explosion = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/explosion.wav");
charge = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/charge.wav")
laser = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/laser.wav")
}
// setup for audio generation
function soundSetup() {
machineGun.setVolume(0.1);
explosion.setVolume(0.1);
charge.setVolume(0.2);
laser.setVolume(0.1);
}
//Create galaxy background
function updateStar(){
this.x -= dx;
}
function createStar(){
strokeWeight(3);
stroke(255);
point(this.x, this.y);
}
function generateStar(sx, sy){
for (var i=0; i<N; i++){
var s = {
x: sx, y: sy,
stepFunction: updateStar,
drawFunction: createStar
}
}
return s;
}
function setup() {
createCanvas(400, 200);
frameRate(10);
imageMode(CENTER);
for (var i=0; i<N; i++){
var s = generateStar(random(width), random(height))
star.push(s);
}
useSound();
}
function draw() {
background(0);
//setting up sequence for charging laser and firing laser
if (a >= 1){
if(laserCharged == false){
laserCharged = true
a = 0
charge.stop()
laser.play();
}
else if(laserCharged == true & laserFired == false){
laserFired = true
a = 0
laser.pause();
}
}
//resetting enemy position if destroyed
if (enemyDestroyed == true){
w = 600;
}
//Update star
for (var i=0; i<N; i++){
var s = star[i]
s.stepFunction();
s.drawFunction();
//Remove star that is out of bounds
if (s.x < 0) {
var s = generateStar(random(width+5, width+500), random(height))
star.splice(i, 1, s);
}
}
//flying spaceship
if (frameCount >= 0 & frameCount < 30) {
flyinSpace(imgX, imgY, 0);
}
//killing enemy
if (frameCount >= 30 & frameCount < 60) {
//play ship firing sound
if (w == 600){
machineGun.loop();
}
//ship firing
shipFiring(w);
//enemy Ship approaching
enemyShip(w, height/2);
w -= dShip;
}
if (frameCount >= 60 & frameCount < 70) {
//stop firing
flyinSpace(imgX, imgY, 0);
//play explosion sound when the enemy is close
if(frameCount == 60){
machineGun.stop();
explosion.play()
}
explode(w, height/2, 60);
}
if (frameCount >= 70 & frameCount < 110) {
//setting up next enemy
enemyDestroyed = false;
if(frameCount == 70){
charge.loop();
}
//dodge laser
if(dist(imgX, imgY, 120, 160)>0 & laserCharged == true){
flyinSpace(imgX, imgY, 0);
imgY += dShip;
}
else{
flyinSpace(imgX, imgY, 0);
}
//enemy Ship
enemyShip(w, height/2);
w -= 0.5*dShip;
//charge laser
if (laserCharged == false){
laserCharge(w, height/2, a);
a+=0.05;
}
//fire laser
else if(laserCharged == true & laserFired == false){
laserCharge(w, height/2, 1);
laserFire(w, height/2, -a);
a+=0.05;
}
}
if (frameCount >= 110 & frameCount < 140) {
//preparing for next shot
if (frameCount == 110){
laserCharged = false;
laserFired = false;
charge.loop();
}
//enemy Ship
enemyShip(w, height/2);
w -= 0.5*dShip;
//ship fire laser
//rotate ship
if (angle>=-18){
flyinSpace(imgX, imgY, angle);
angle-=3;
}
//fire laser after rotation
else{
//charge laser
flyinSpace(imgX, imgY, angle)
if (laserCharged == false){
laserCharge(imgX, imgY, a);
a+=0.1;
}
//fire laser
else if(laserCharged == true & laserFired == false){
laserCharge(imgX, imgY, 1);
laserFire(imgX, imgY, a, angle);
a+=0.1;
}
}
}
if (frameCount >= 140 & frameCount < 150) {
flyinSpace(imgX, imgY, angle);
//play explosion sound when the enemy is close
if(frameCount == 140){
explosion.play();
}
explode(w, height/2, 140);
}
if (frameCount >= 150){
background(0);
noLoop();
}
}
//adding spaceship
function flyinSpace(x, y, a){
push()
translate(x, y);
rotate(radians(a));
image(spaceShip, 0, 0);
pop()
}
function shipFiring(w){
//ship firing
if(w%20 == 0){
image(spaceShipFire, 120, height/2);
}
else{
image(spaceShip, 120, height/2);
}
}
function enemyShip(w, h){
//enemy ship flying
image(enemy1, w, h);
}
function explode(w, h, f){
//create explosion when the enemy is close
image(explosion1, w, h);
if(frameCount-f >= 3){
image(explosion2, w, h);
}
if(frameCount-f >= 6){
image(explosion3, w, h);
enemyDestroyed = true;
}
}
//adding laser charge
function laserCharge(x, y, a){
push()
translate(x, y);
scale(a);
image(laserEnergy, 0, 0);
pop()
}
//adding laser beam
function laserFire(x, y, a, angle){
push()
translate(x, y);
rotate(radians(angle));
scale(-a, 1);
image(laserBeam, -211, -0.5);
pop()
}