Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
// Harrison Kerr 991438373
// Meteor Dodge
// Dodge the meteors that are flying towards the player
// Use the mouse to move the spacecraft

Meteor[] meteor;
Stars[] stars = new Stars[200];


void setup() {
  //set the canvas size
  size(400, 400);

  //creates the meteors
  meteor = new Meteor[15];
  for (int i = 0; i < meteor.length; i++) {
    meteor[i] = new Meteor(random(0, 400), random(-200, 0), random(5, 10), random(5, 10), random(1,4), random(0,0.5));
  }
  //creates the starts
  for (int i = 0; i < stars.length; i++) {
    stars[i] = new Stars(int(random(0, 400)), int(random(0, 400)));
  }
}

void draw() {
  //draw the background
  background(0);

  //draws the stars
  for (int i=0; i <stars.length; i++) {
    stars[i].display();
  }
  
  //draw the planets
  fill(0, 0, 255);
  ellipseMode(CENTER);
  ellipse(200,200, 60, 60);

fill(152,71,71);
ellipse(370,355,200,200);

  //draw the space ship
  fill(255);
  triangle(mouseX +10, mouseY + 20, mouseX-10, mouseY+20, mouseX, mouseY-30);
  
  meteorfunction();
  
}

void meteorfunction() {
  
  for (int i = 0; i <meteor.length; i++) {
    meteor[i].move();
  }

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

//when the player dies
  void death(){
    
    fill(0);
    rect(0,0,400,400);
    fill(255,0,0);
    textSize(50);
    text("YOU DIED",100,200);
    stop();
           
  }
class Meteor {

  PVector position;
  PVector velocity;

  float meteorWidth;
  float meteorHeight;

  Meteor(float tempPositionX, float tempPositionY, float tempMeteorWidth, float tempMeteorHeight, float tempVelocityY, float tempVelocityX) {
    position = new PVector(tempPositionX, tempPositionY);
    velocity  = new PVector(tempVelocityX, tempVelocityY);


    meteorWidth = tempMeteorWidth;
    meteorHeight = tempMeteorHeight;
  }

  void move() {
    
    position.add(velocity);
collisionDetection();  
}
  

  void display() {
    fill(#DE664E);

    ellipse(position.x, position.y, meteorWidth, meteorHeight);
    if (position.y >= 400) {
      position.y = 0;
      position.x = random(0, 400);
    }
  }
  
  void collisionDetection() {
    if  (mouseX +10 > position.x -10 && mouseX -10 < position.x+10 && mouseY + 10 > position.y-10 && mouseY-10 < position.y+10) {
     position.x = 0;
     position.y = 0;
      death();
    }
  }
}
class Stars {
  float xPosition;
  float yPosition;
  
  Stars(float tempPositionX, float tempPositionY) {
    xPosition = tempPositionX;
    yPosition = tempPositionY;
  }
  
  void display(){
    fill(255,245,203,int(random(100,150)));
   ellipse(xPosition,yPosition,5,5);
          }
  
}