Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
///////////////////////////////////////////////////////////////////////////////////////////////////
/*Meteor Shower
 
 -By:Aaron Sutton
 
 -Date of Last Edit: October 4th,2015
 
 -Instructions: You are a U.F.O travelling through a meteor storm, try to dodge the meteors coming 
 at you. The more you dodge, the more points you'll score but if you get hit you'll have to restart!
 Be careful!
 
 -Controls: Use the "S" key to go down, and use the "W" key to go up.*/

///////////////////////////////////////////////////////////////////////////////////////////////////

//state variables
int score;

//key bindings
boolean upPressed=false;
boolean downPressed=false;

//States where your ship is, it always starts at 200
float shipLocation=200.;

//sets the size of the asteroids
int asteroidSize=30;

//Set asteroid positions
float asteroidX=0;
float asteroidY=random(9, 151);
float asteroidX2=10;
float asteroidY2=random(152, 301);
float asteroidX3=30;
float asteroidY3=random(302, 401);
float asteroidX4=30;
float asteroidY4=random(302, 401);
float asteroidX5=0;
float asteroidY5=random(9, 151);
//set background asteroid positions
float astX=0;
float astY=0;
float astX2=300;
float astY2=300;
float astX3=340;
float astY3=100;


//start set up
void setup() {
  size(400, 400);
  rectMode(CORNER);
  noStroke();
  smooth();
}

//start draw cycle
void draw() {

  //draw black background
  background(0);
  stars();
  //the stars use a white stroke so it must be removed after every time the stars are drawn as nothing else uses a stroke
  noStroke();
  //The planets/asteroids in the background that gives it a moving effect
  asteroidBackground();
  backgroundMovement1();
  backgroundMovement2();
  backgroundMovement3();
  //draw U.F.O
  ship();
  updateShip();
  constrainShip();

  //draws 5 asteroids on field
  asteroidFinal();
}

//draw the spaceship
void ship() {
  fill(43, 90, 203);
  ellipse(370, shipLocation+4, 15, 11);
  fill(220, 0, 0);
  ellipse(370, shipLocation, 20, 15);
  fill(136, 55, 147);
  ellipse(370, shipLocation-5, 10, 5);
  fill(203, 255, 252, 150);
  ellipse(370, shipLocation-5, 15, 11);
  fill(shipLocation+150, shipLocation+100, shipLocation+60);
  ellipse(363, shipLocation+1, 3, 3);
  ellipse(370, shipLocation+3, 3, 3);  
  ellipse(377, shipLocation+1, 3, 3);
}

//create score counter
void scoreCounter() {
  textSize(15);
  //Display users score
  fill(254, 255, 28);
  text("Score:", 20, 380);
  text(score, 67, 380);
}

//create the asteroids
void asteroid()
{
  fill (147, 107, 55);
  ellipse(asteroidX, asteroidY, asteroidSize, asteroidSize);
  fill(75, 58, 32);
  ellipse(asteroidX+2, asteroidY+8, 10, 7);
  ellipse(asteroidX-3, asteroidY-6, 7, 7);
  ellipse(asteroidX+8, asteroidY-3, 3, 3);
}

//sets the movement for the asteroids
void asteroidMove()
{ 
  asteroidX=asteroidX+3;

  if (asteroidX>410) {
    asteroidX=0;
    asteroidY=random(9, 151);
    score++;
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
/*Next asteroids all contain similar variables, only with different positions.
 Allows for more than one asteroid on screen at a time.
 They all have different respawn rates and locations, but function very similarly*/
/////////////////////////////////////////////////////////////////////////////////////////////////

void asteroid2()
{
  fill (147, 107, 55);
  ellipse(asteroidX2, asteroidY2, asteroidSize, asteroidSize);
  fill(75, 58, 32);
  ellipse(asteroidX2+2, asteroidY2+8, 10, 7);
  ellipse(asteroidX2-3, asteroidY2-6, 7, 7);
  ellipse(asteroidX2+8, asteroidY2-3, 3, 3);
}

void asteroidMove2()
{ 
  asteroidX2=asteroidX2+3;

  if (asteroidX2>410) {
    asteroidX2=10;
    asteroidY2=random(152, 301);
    score++;
  }
}

void asteroid3()
{
  fill (147, 107, 55);
  ellipse(asteroidX3, asteroidY3, asteroidSize, asteroidSize);
  fill(75, 58, 32);
  ellipse(asteroidX3+2, asteroidY3+8, 10, 7);
  ellipse(asteroidX3-3, asteroidY3-6, 7, 7);
  ellipse(asteroidX3+8, asteroidY3-3, 3, 3);
}

void asteroidMove3()
{ 
  asteroidX3=asteroidX3+3;

  if (asteroidX3>410) {
    asteroidX3=30;
    asteroidY3=random(302, 401);
    score++;
  }
}

void asteroid4()
{
  fill (147, 107, 55);
  ellipse(asteroidX4, asteroidY4, asteroidSize, asteroidSize);
  fill(75, 58, 32);
  ellipse(asteroidX4+2, asteroidY4+8, 10, 7);
  ellipse(asteroidX4-3, asteroidY4-6, 7, 7);
  ellipse(asteroidX4+8, asteroidY4-3, 3, 3);
}

void asteroidMove4()
{ 
  asteroidX4=asteroidX4+3;

  if (asteroidX4>410) {
    asteroidX4=0;
    asteroidY4=random(302, 401);
    score++;
  }
}

void asteroid5()
{
  fill (147, 107, 55);
  ellipse(asteroidX5, asteroidY5, asteroidSize, asteroidSize);
  fill(75, 58, 32);
  ellipse(asteroidX5+2, asteroidY5+8, 10, 7);
  ellipse(asteroidX5-3, asteroidY5-6, 7, 7);
  ellipse(asteroidX5+8, asteroidY5-3, 3, 3);
}

void asteroidMove5()
{ 
  asteroidX5=asteroidX5+3;

  if (asteroidX5>410) {
    asteroidX5=30;
    asteroidY5=random(9, 151);
    score++;
  }
}

/*detects if the U.F.O is hit by an asteroid
 if so, score counter resets*/
void asteroidCollision()
{
  if (asteroidX - 15 <= 400 && asteroidX + 15 >=400 )
  {
    if (asteroidY - 15 <= shipLocation + 5 && asteroidY +15 >= shipLocation - 5)
    {
      System.out.println("POWPOW!");
      score=0;
    }
  }
  if (asteroidX2 - 15 <= 400 && asteroidX2 + 15 >=400 )
  {
    if (asteroidY2 - 15 <= shipLocation + 5 && asteroidY2 +15 >= shipLocation - 5)
    {
      System.out.println("POWPOW!");
      score=0;
    }
  }
  if (asteroidX3 - 15 <= 400 && asteroidX3 + 15 >=400 )
  {
    if (asteroidY3 - 15 <= shipLocation + 5 && asteroidY3 +15 >= shipLocation - 5)
    {
      System.out.println("POWPOW!");
      score=0;
    }
  }
  if (asteroidX4 - 15 <= 400 && asteroidX4 + 15 >=400 )
  {
    if (asteroidY4 - 15 <= shipLocation + 5 && asteroidY4 +15 >= shipLocation - 5)
    {
      System.out.println("POWPOW!");
      score=0;
    }
  }
  if (asteroidX5 - 15 <= 400 && asteroidX5 + 15 >=400 )
  {
    if (asteroidY5 - 15 <= shipLocation + 5 && asteroidY5 +15 >= shipLocation - 5)
    {
      System.out.println("POWPOW!");
      score=0;
    }
  }
}

//Puts 5 asteroids into one function, allows for less clutter in the draw cycle.
void asteroidFinal() {
  asteroid();
  asteroid2();
  asteroid3();
  asteroid4(); 
  asteroid5();
  //movement update
  asteroidMove();
  asteroidMove2();
  asteroidMove3();
  asteroidMove4();  
  asteroidMove5();
  scoreCounter();
  // Collision
  asteroidCollision();
}

//draw the asteroids in the background
void asteroidBackground() {
  fill(72, 58, 36);
  ellipse(astX, astY, 300, 300);
  fill(64, 66, 56);
  ellipse(astX2, astY2, 150, 150);
  fill(175, 157, 129);
  ellipse(astX3, astY3, 100, 100);
}

//sets the background to move and reset
void backgroundMovement1() {
  astX=astX+0.3;

  if (astX>550) {
    astX=-150;
  }
}

void backgroundMovement2() {
  astX2=astX2+0.6;

  if (astX2>480) {
    astX2=-80;
  }
}

void backgroundMovement3() {
  astX3=astX3+0.8;

  if (astX3>450) {
    astX3=-50;
  }
}

//draws stars in the background
//referenced from Cylon Lander, but changed slightly
void stars() {
  stroke(random(130, 230));
  point(20, 30);
  point(320, 130);
  point(120, 360);
  point(40, 300);
  point(130, 200);
  point(360, 290);
  point(80, 70);
  point(50, 130);
  point(250, 110);
  point(55, 270);
  point(140, 200);
  point(290, 290);
  point(290, 30);
  point(10, 220);
  point(120, 100);
  point(60, 100);
  point(100, 300);
  point(380, 360);
  point(320, 300);
  point(190, 260);
  point(280, 320);
  point(79, 240);
}

//updates the movements for the ship
void updateShip() {
  if (upPressed==true) {
    shipLocation-=3.;
  }
  if (downPressed==true) {
    shipLocation+=3.;
  }
}

//Key binding controls.
void keyPressed() {
  if (key=='W' || key=='w') {
    upPressed=true;
    downPressed=false;
  }
  if (key=='S'||key=='s') {
    downPressed=true;
    upPressed=false;
  }
  if (shipLocation<25) {
    upPressed=false;
  }
}

//knows when you release a key
void keyReleased() {
  if (key=='W'||key=='w') {
    upPressed=false;
    shipLocation=shipLocation;
  }
  if (key=='S'||key=='s') {
    shipLocation=shipLocation;
    downPressed=false;
  }
}

//constrains the ship to window borders
void constrainShip() {
  if (shipLocation+25<shipLocation) {
    shipLocation=24;
  }
  if (shipLocation<10) {
    shipLocation=11;
  }
  if (shipLocation+375<shipLocation) {
    shipLocation=375;
  }
  if (shipLocation>390) {
    shipLocation=389;
  }
}