Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* 
 See how good of a Captain you are! Avoid space debris,
 survive as long as you can. Your score is based on 
 how long you can pilot the ship without smashing into
 oncoming debris while speeding through space!
 -------------------------------------------------------
 An Object Based Toy Created by: Harry Pitts
 */

//Global Variables
int seconds = millis();
int numberOfSpaceRocks = 40;

//Space Debris array
Debris[] debris = new Debris[numberOfSpaceRocks];

//Initializing Classes
ScoreBoard scoreBoard;
Player player1;

void setup() {
  
  size(600, 600);
  background(7, 7, 7);
  smooth();
  rectMode(CORNERS);
  scoreBoard = new ScoreBoard();
  player1 = new Player();
  initializeDebris();
}

void initializeDebris() {

  for (int i = 0; i < debris.length; i++) {
    debris[i]=new Debris(random(3, width), 0, random(-2, 5), random(1, 3));
  }
}
void draw() {

  //background update
  background(7, 7, 7);
  //Calling classes
  scoreBoard.displayScore();
  player1.playerDisplay();

  for (int i = 0; i < debris.length; i++) {
    debris[i].debrisCollision();
    debris[i].debrisUpdate();
    debris[i].debrisDisplay();
  }
}
class Player {

  void playerDisplay() {

    //Ship Underside and Struts
    fill(168, 162, 138);
    triangle(mouseX-6, mouseY-2, mouseX+6, mouseY-2, mouseX, mouseY+20);
    triangle(mouseX-2, mouseY+10, mouseX, mouseY+17, mouseX-18, mouseY+23);
    triangle(mouseX+2, mouseY+10, mouseX, mouseY+17, mouseX+18, mouseY+23);
    ellipse(mouseX, mouseY+17, 7, 7);

    //Ship Disc Body 
    fill(193, 187, 158);
    ellipse(mouseX, mouseY-10, 25, 25);

    //Ships Bridge
    fill(168, 162, 138);
    ellipse(mouseX, mouseY-10, 15, 15);

    //Ship Engines
    fill(193, 187, 158);
    rect(mouseX-19, mouseY+10, mouseX-15, mouseY+29);
    rect(mouseX+19, mouseY+10, mouseX+15, mouseY+29);

    //Ship Engine collision lights
    fill(226, 43, 43);
    rect(mouseX-19, mouseY+10, mouseX-15, mouseY+14);
    fill(81, 173, 226);
    rect(mouseX+19, mouseY+10, mouseX+15, mouseY+14);
  }
}
class ScoreBoard {

  //int seconds = millis();
  void displayScore() {

    //score counter
    seconds++;
    fill(255);
    textSize(25);
    text("Time Alive: " +seconds, 30, 50);
  }
}
class Debris {

  PVector velocity = new PVector();
  PVector initialPosition = new PVector();

  //Space Debris Constructor
  Debris(float x, float y, float velocityX, float velocityY) {

    velocity.x = velocityX;
    velocity.y = velocityY + 2;

    initialPosition.x = x;
    initialPosition.y = y;
  } 
  void debrisUpdate() {

    //Updates Debris position and resets location on screen
    initialPosition.add(velocity); 
    if (initialPosition.y > height) {
      initialPosition.x = random(width);
      initialPosition.y = 0;
    }
  } 
  void debrisDisplay() {

    fill(107, 113, 122);
    noStroke();
    ellipse(initialPosition.x, initialPosition.y, random(7, 15), random(7, 15));
  }
  void debrisCollision() {

    //Checks if the player is in contact with space debris
    if (dist(initialPosition.x, initialPosition.y, mouseX, mouseY)<=20) {
      background(234, 91, 51);
      //Reset's the score timer  
      seconds=0;
      println("Captain! we are taking damage to our shields!");
    }
  }
}