Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* 
 INTERACTIVE TOY ASSIGNMENT
 Project Name: Galactic Blaster
 By: Ashborn Binapal
 
 
 While travelling through space you are told about red oncoming asteroids.
 Your mission: Destroy the oncoming red asteroids before its too late (its never too late) and
 increase your score to reach the Galactic Crusader title.
 
 //////////////////////CONTROLS/////////////////////////
 Ship Movement = W,A,S,D
 Fire = P
 Display Score = Spacebar
 */

//Ship Variables
int shipX=300;
int shipY=300;
int speed=4;
int gem1=196;
int gem2=26;
int gem3=26;
int noseColour=77;

//Score Variable
int score;

//Sneaky Banana Variable
int bananaX=45;
boolean bananaBlessed=false;

//Laser Variables
float laserX=shipX;
float laserY=shipY;
int laserspeed=15;

//Boundary Variables
int boundaryX1=0;
int boundaryX2=400;
int boundaryY1=0;
int boundaryY2=400;

//Asteroid Variables
float asteroidX=200;
float asteroidY=boundaryY1-100;
float asteroidSpeed=3.5;
boolean destroyed=false;

//Switch Variables
boolean goRight=false;
boolean goLeft=false;
boolean goDown=false;
boolean goUp=false;
boolean laserOn=false;

void setup() {
  size (400, 400);
  background(0);
  rectMode(CENTER);
}



void draw() {

  drawBackground();
  //Resets rectMode to CENTER after draw background
  rectMode(CENTER);
  noStroke();

  //Function Callbacks
  moveChar();
  moveLaser();
  drawPlanets();
  drawBanana();
  drawStars();
  drawLaser();
  drawFlame();
  drawShuttle();
  drawAsteroid();
  updateAsteroid();
  moveAsteroid();
  scoreAchievements();
}



void drawShuttle() {

  //SHIP NOSE
  fill(noseColour, noseColour, noseColour);
  rect(shipX, shipY-30, 8, 20);
  rect(shipX, shipY-40, 5, 20);
  rect(shipX, shipY-50, 3, 20); 

  //GUNS
  fill(noseColour, noseColour, noseColour);

  //Gun (Left)
  rect(shipX-30, shipY+2, 4, 7);
  rect(shipX-30, shipY, 2, 9);

  //Gun (Right)
  rect(shipX+30, shipY+2, 4, 7);
  rect(shipX+30, shipY, 2, 9);

  //WINGS

  //Wing (Left)
  fill(35, 45, 178);
  triangle(shipX, shipY+10, shipX, shipY-35, shipX-50, shipY+30);

  //Wing (Right)
  triangle(shipX, shipY+10, shipX, shipY-35, shipX+50, shipY+30);

  //THRUSTER
  fill(178, 158, 80);
  rect(shipX, shipY+15, 15, 10);

  //BODY
  fill(255, 235, 159);
  rect(shipX, shipY, 20, 20);
  triangle(shipX+10, shipY-10, shipX+20, shipY+20, shipX, shipY+10);
  triangle(shipX-10, shipY-10, shipX-20, shipY+20, shipX, shipY+10);
  triangle(shipX+10, shipY-10, shipX+15, shipY-5, shipX, shipY+10);
  triangle(shipX-10, shipY-10, shipX-15, shipY-5, shipX, shipY+10);

  //WINDOW
  fill(177, 182, 255);
  ellipse(shipX, shipY-15, 18, 40);
  fill(255);
  ellipse(shipX+5, shipY-20, 4, 8);
  ellipse(shipX+5, shipY-10, 2, 4);

  //GEM
  fill(gem1, gem2, gem3);
  ellipse(shipX, shipY+10, 5, 10);
}



//FLAME
void drawFlame() {

  fill(255, 0, 0, 150);
  ellipse(shipX, shipY+22, random(7, 14), random(24, 38));
  fill(250, 158, 38, 150);
  ellipse(shipX, shipY+22, random(5, 10), random(16, 32));
  fill(254, 255, 41, 150);
  ellipse(shipX, shipY+22, 6, random(8, 16));
}



//STARS
void drawStars() {

  fill(245, 245, 245, 200);
  rect(35, (frameCount % 80)*10, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(50, (frameCount % 200)*2, sin(frameCount/2), 2+sin(frameCount/2));
  rect(80, (frameCount % 120)*4, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(150, (frameCount % 95)*6, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(260, (frameCount % 64)*8, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(180, (frameCount % 77)*7, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(320, (frameCount % 116)*12, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(350, (frameCount % 200)*2.5, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(225, (frameCount % 154)*3, 2+sin(frameCount/2), 2+sin(frameCount/2));
  rect(145, (frameCount % 99)*5, 2+sin(frameCount/2), 2+sin(frameCount/2));
}



//PLANETS
void drawPlanets() {

  //Medium Planet (purple)
  fill(173, 25, 193, 80);
  ellipse(100, -20+(frameCount % 280)*4, 20, 20);

  //Large Planet (green)
  fill(133, 234, 162, 80);
  ellipse(245, -40+(frameCount % 180)*3, 40, 40);

  //Tiny Planet (light orange)
  fill(234, 199, 80, 100);
  ellipse(375, -10+(frameCount % 235)*6, 10, 10);
}



//BANANA (Special Cameo for one of my friends)
void drawBanana() {

  //Banana Body
  fill(247, 255, 36);
  ellipse(bananaX, -30+(frameCount % 750)*2, 30, 30);

  //Sphere that blends with background
  fill(8, 8, 8);
  ellipse(35, -30+(frameCount % 750)*2, 30, 30);

  //Banana Ends
  fill(50, 50, 50);
  ellipse(40, -45+(frameCount % 750)*2, 5, 5);
  ellipse(40, -15+(frameCount % 750)*2, 5, 5);
}



//ASTEROID
void drawAsteroid() {

  //Draws Asteroid if destroyed value is false
  if (destroyed == false) {
    fill(100, 100, 100);
    ellipse(asteroidX, asteroidY, 50, 50);
    fill(162, 162, 162);
    ellipse(asteroidX-5, asteroidY-8, 15, 12);
    ellipse(asteroidX+10, asteroidY+5, 8, 10);
    ellipse(asteroidX-15, asteroidY+10, 8, 8);
    fill(237, 33, 27, 100);
    ellipse(asteroidX, asteroidY, 60, 60);
  }
}



//UPDATE ASTEROID
void updateAsteroid() {

  //Resets Asteroid and changes X position if laser values enter its hit box
  if (laserX < asteroidX+25 && laserX > asteroidX-25 && laserY < asteroidY+25 && laserY > asteroidY-25) {
    destroyed = true;
    asteroidX = random(100, 380);
    asteroidY = boundaryY1-100;
    score = score + 1;
    asteroidSpeed = random(4, 8);
    println ("KA-BOOM");
    destroyed = false;
  }

  //Resets Asteroid and changes X position if it hits the bottom of the screen
  if (asteroidY >= boundaryY2+25) {
    destroyed = true;
    asteroidY = boundaryY1 - 100;
    asteroidX = random(80, 380);
    asteroidSpeed = random(4, 8);
    destroyed = false;
  }
}



//MOVE ASTEROID
void moveAsteroid() {

  //Constantly changes asteroidY to make it move
  asteroidY = asteroidY + asteroidSpeed;
}



//SWITCHES
//These are what change the boolean value to true to allow movement
void keyPressed() {

  if (key == 'd') {
    goRight = true;
  }

  if (key == 'a') {
    goLeft = true;
  }

  if (key == 's') {
    goDown = true;
  }

  if (key == 'w') {
    goUp = true;
  }

  if (key == 'p') {
    laserOn = true;
  }

  if (key == ' ') {
    print ("SCORE: ");
    println ( score);
  }
}



//SWITCHES OFF
//These are what change the boolean value to false to stop movement on release
void keyReleased() {

  if (key == 'd') {
    goRight = false;
  }

  if (key == 'a') {
    goLeft = false;
  }

  if (key == 's') {
    goDown = false;
  }

  if (key == 'w') {
    goUp = false;
  }
}



//This function controls the ship's movement
void moveChar() {

  if (shipX < boundaryX2) {
    if (goRight == true) {
      shipX= shipX+speed;
      laserX= laserX+speed;
    }
  }

  if (shipX > boundaryX1) {
    if (goLeft == true) {
      shipX= shipX-speed;
      laserX= laserX-speed;
    }
  }

  if (shipY < boundaryY2) {
    if (goDown == true) {
      shipY= shipY+speed;
      laserY= laserY+speed;
    }
  }

  if (shipY > boundaryY1) {
    if (goUp == true) {
      shipY= shipY-speed;
      laserY= laserY-speed;
    }
  }
}



//LASER
void drawLaser() {

  //Creates the laser if it fits parameters
  if (laserOn == true) {
    if (laserY > 0) {
      fill(255, 255, 3, 200);
      rect (laserX, laserY, 4, 20);
    }
  }
}



//MOVE LASER
void moveLaser() {

  //Moves laser's x and y positions
  if (laserOn == true) {
    if (laserY > boundaryY1) {
      laserY= laserY - laserspeed;
    }
  } 
  //Resets laser position after it hits Y value of 0
  if (laserY <= boundaryY1) {
    laserOn=false;
    laserY=shipY;
  }
}


//SCORE CHECKPOINTS AND EXTRAS
//Colour of gem changes based on achievement
void scoreAchievements() {

  if (score == 10) {
    println("LEARNING THE BASICS = Destroy 10 Asteroids. +1 SCORE");
    gem2=220;
    score = 11;
  }

  if (score == 20) {
    println("GETTING THE HANG OF THIS = Destroy 20 Asteroids. +1 SCORE");
    gem3=220;
    score = 21;
  }

  if (score == 30) {
    println("GALACTIC CRUSADER = Destroy 30 Asteroids. +1 SCORE");
    gem1=20;
    gem2=245;
    gem3=20;
    score = 31;
  }

  if (score == 50) {
    println("Thats....alot of asteroids = Destroy 50 Asteroids. +1 SCORE");
    gem1=10;
    gem2=10;
    gem3=240;
    score = 51;
  } 

  if (score == 70) {
    println("Ok I think the threat is gone = Destroy 70 Asteroids. +1 SCORE");
    gem1=210;
    gem2=20;
    gem3=160;
    score = 71;
  }

  if (score == 100) {
    println(".... = Destroy 100 Asteroids. +1 SCORE");
    gem1=18;
    gem2=18;
    gem3=18;
    score = 101;
  }

  if (shipX <= 45 && shipX >= 35 && shipY >= -30+(frameCount % 750)*2-5) {
    if (shipY <= -30+(frameCount % 750)*2 + 5) {
      if (bananaBlessed==false) {
        println("You have recieved the mystical BANANA BLESSING");
        noseColour=200;
        bananaBlessed = true;
      }
    }
  }
}



//BACKGROUND
void drawBackground() {

  rectMode(CORNERS);
  fill(8, 8, 8);
  rect(0, 0, 400, 400);
}


/*REFERENCES
 
 Controller Idea adapted from: http://www-acad.sheridanc.on.ca/PROG14998/2017/interactive-toy/darryl_feniquito_interactive_toy/index.html
 Author: Darryl Feniquito
 (Asteroid reset idea was based around their "switches" idea too)
 
 Laser and asteroid collision adapted and improved from: http://www-acad.sheridanc.on.ca/PROG14998/2016/interactive-toy/ata_dogan_interactive_toy/index.html
 Author: Ata Dogan
 
 */