Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* //////////////////////////////////////////////////////////////////////////////// */
/*                               ASTEROID AVOID by ANDREEA POP                      */
/* The basic premise of this game is to collect as many fuel cans as you can while  */
/* avoiding the asteroids flying in your direction. NOTE: The collision on the      */
/* asteroids is a bit wonky and the spaceship may clip through an asteroid before   */
/* the hit is detected. The ship will follow your mouse, and clicking the right     */
/* mouse will collect fuel cans. To save your fingers from constant tapping, you    */
/* can simply hold down the mouse. Missing a fuel can will lower your score by 2.   */
/* Hitting an asteroid will restart the game and your score.                        */
/*                                                                                  */
/* Thank you to whoever made these programs, without you I'd still be stumped on    */
/* how to make collisions and crying.                                               */
/*                                                                                  */
/* - http://www-acad.sheridanc.on.ca/PROG14998/2015/oo-toy/oo_toy_33/               */
/* - http://www-acad.sheridanc.on.ca/PROG14998/2015/oo-toy/oo_toy_10/               */
/* //////////////////////////////////////////////////////////////////////////////// */

//Define the objects in the program
Spaceship spaceShip;
ArrayList <Asteroids> asteroidsList;

//arrays - stars
int numberOfStars = 20;
Stars[] spaceStars = new Stars[numberOfStars];

//arrays - fuel
int numberOfFuelCans = 2;
Fuel[] spaceFuel = new Fuel[numberOfFuelCans];

//collision padding, how far from the pos.x/y will collision be detected
int padding = 30;

void setup() {
  //set up window size + frame rate
  size(800, 600);
  frameRate(60);

  //instructions
  println("Hold down the mouse button to collect fuel cans! Missing one will lower your score.");
  println("Avoid the asteroids or they'll reset you.");

  //initilization
  spaceShip = new Spaceship();
  asteroidsList = new ArrayList();

  for (int i=0; i<spaceFuel.length; i+=1) {
    spaceFuel[i] = new Fuel();
  }

  for (int i=0; i<10; i++) {
    Asteroids spaceAsteroids=new Asteroids();
    asteroidsList.add(spaceAsteroids);
  }

  for (int i=0; i<spaceStars.length; i++) {
    spaceStars[i]=new Stars(random(width, width+100), random(0, height), random(-15, -5));
  }
}

void draw() {
  //updates
  for (int i=0; i<spaceStars.length; i++) {
    spaceStars[i].update();
  }
  for (int i=0; i<asteroidsList.size (); i++) {
    asteroidsList.get(i).update();
  }

  for (int i=0; i<spaceFuel.length; i+=1) {
    spaceFuel[i].update();
  }

  //display
  background(0);

  for (int i=0; i<spaceStars.length; i++) {
    spaceStars[i].display();
  }

  for (int i=0; i<asteroidsList.size (); i++) {
    asteroidsList.get(i).display();
  }

  for (int i=0; i<spaceFuel.length; i+=1) {
    spaceFuel[i].display();
    spaceFuel[i].collected();
    spaceFuel[i].scoreDisplay();
  }

  //show the spaceship on screen
  spaceShip.display();
  collision();
}

void collision() {
  for (int i=0; i<asteroidsList.size (); i+=1) {
    if (dist(spaceShip.shipTop.x, spaceShip.shipTop.y, asteroidsList.get(i).position.x, asteroidsList.get(i).position.y) < padding) {
      setup();
      scoreFuel = 0;
    }

    if (dist(spaceShip.shipBottom.x, spaceShip.shipBottom.y, asteroidsList.get(i).position.x, asteroidsList.get(i).position.y) < padding) {
      setup();
      scoreFuel = 0;
    }
  }
}
class Asteroids {

  float asteroidSize;
  color asteroidColor;

  //position/velocity vector
  PVector position;
  PVector velocity;

  Asteroids() {
    //resets the p when offscreen
    reset();
  }

  void update() {
    position.add(velocity);
    if (position.x<-150) {
      reset();
    }
  }

  void setAsteroidSize() {
    asteroidSize=random(60, 150);
  }

  void setAsteroidColor() {
    asteroidColor=color(random(140, 165), random(140, 165), random(140, 165));
  }

  void display() {
    stroke(0, 50);
    fill(asteroidColor);
    ellipse(position.x, position.y, asteroidSize, asteroidSize);
    noStroke();
  }    

  void reset() {
    //reset the asteroid when it goes 150px offscreen
    position = new PVector (width+200, random(0, height));
    velocity = new PVector (random(-6, -1), random(-1, 1));
    setAsteroidSize();
    setAsteroidColor();
  }
}
//score variables
int scoreFuel;

class Fuel {

  //size variables
  int fuelWide = 35;
  int fuelHeight = 30;

  //position/velocity vector
  PVector position;
  PVector velocity;

  Fuel() {
    //resets the fuel can when it is picked up
    reset();
  }

  void update() {
    position.add(velocity);

    //reset the fuel can if it goes off screen
    if (position.x<-100) {
      reset();
      scoreFuel = scoreFuel - 2;
    }
    
    if (scoreFuel<0) {
      scoreFuel=0;
    }
  }

  void display() {
    //no strokes
    noStroke();
    rectMode(CENTER);
    fill(170, 45, 45);
    rect(position.x, position.y, 30, 35);
    fill(137, 127, 127);
    rect(position.x-5, position.y-20, 15, 5);
    fill(211,182,117);
    rect(position.x, position.y, 20,10);
  }

  //summon new fuel can when it's collected
  void reset() {
    //start at the right of the screen at different heights/velocity
    position = new PVector(width+fuelWide, random(10, height-10));
    velocity = new PVector(random(-5, -2), 0);
  }

  //score
  void collected() {
    if (mousePressed && dist(mouseX, mouseY, position.x, position.y) < fuelWide) {
      reset();
      scoreDisplay();
      scoreFuel += 1;
    }
  }

  void scoreDisplay() {
    fill(255);
    textAlign (LEFT);
    textSize (15);

    text("Fuel Cans:", 20, 30);
    text(scoreFuel, 140, 30);
  }
}
class Spaceship {

  PVector shipMouse;
  PVector shipTop;
  PVector shipBottom;

  Spaceship() {
    //let the ship follow the mouse w/a small delay
    shipMouse = new PVector(mouseX, mouseY);
    shipTop = new PVector(mouseX-35, mouseY-7);
    shipBottom = new PVector (mouseX-35, mouseY+7);
  }

  void display() {
    shipMouse.set(mouseX, mouseY);
    shipTop.set(mouseX-35, mouseY-20);
    shipBottom.set(mouseX-35, mouseY+20);

    //fire
    fill(211, 108, 23);
    triangle(mouseX-30, mouseY-5, mouseX-(random(50, 80)), mouseY, mouseX-30, mouseY+5);
    fill(255, 216, 59);
    triangle(mouseX-30, mouseY-2, mouseX-(random(50, 65)), mouseY, mouseX-30, mouseY+2);

    //ship wings
    fill(188, 74, 74);
    triangle(mouseX+20, mouseY, mouseX-30, mouseY-20, mouseX-30, mouseY);

    //ship body
    fill(247);
    ellipse(mouseX, mouseY, 70, 20);

    //ship wings: side
    fill(188, 74, 74);
    rect(mouseX-15, mouseY, 30, 5);

    //window
    fill(71, 194, 227);
    triangle(mouseX+10, mouseY-10, mouseX+37, mouseY-2, mouseX+10, mouseY);
  }
}
class Stars {

  PVector position = new PVector();
  PVector velocity = new PVector();

  Stars(float x, float y, float velocityX) {
    position.x=x;
    position.y=y;
    velocity.x=velocityX;
  }

  void update() {
    position.add(velocity);

    if (position.x<-40) {
      position.x=width+80;
      position.y=random(height);
    }
  }

  void display() {
    fill(random(150,255));
    rect(position.x, position.y, 120, 3);
  }
}