Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////////////GLOBAL VARIABLES
//for simplicity, I made these variables based off the mouse co-ordinates
float mX = mouseX;
float mY = mouseY;

//Sets starting gravity for all three meteors, saves time
float gravity = 0.0; 
//Sets starting X position for meteors
float x = 200;

//Independent variables for each meteor
float gravity1 = gravity;
float x1 = x;
float gravity2 = gravity;
float x2 = x;
float gravity3 = gravity;
float x3 = x;


float speed = 1.0;
int score = 0;
int scoreCount = 0;
int meteorBonus = 10;
int lifeCount= 3;
boolean gameOver= false;
////////////////SETUP
void setup () {
  size (400, 400);
  background(0);

  //Randomly generated city
  for (int i =0; i<20; i++) {
    float buildHght = random(20, 80);
    float buildCol = random(50, 200);
    fill(buildCol);
    rectMode(CENTER);
    rect(10+20*i, 390, 20, buildHght);
    fill(255);
    rect(random(11, 16)+20*i, random(390-buildHght/3, 388), 4, 4);
    rect(random(4, 16)+20*i, random(392, 395), 4, 4);
    rect(random(5, 9)+20*i, random(390-buildHght/3, 388), 4, 4);
  }
}



//////////////////DRAW
void draw () {
  
  //once the game is over nothing else will run in background
  if (gameOver == false) {
    mX = mouseX;
    mY = mouseY;
    //acceleration and Speed cap 
    speed = (1000+frameCount)/1000; 
    speed = constrain(speed, 1, 5);
   
   //The players score goes up by 1 every second
    scoreCount ++ ;
    if (scoreCount >= frameRate) {
      score = score +1;
      scoreCount = 0;
    }

    //psuedo background
    noStroke();
    fill(0);
    rect(200, 180, 400, 360);
    rect(200, 380, 40, 40);

    //Randomly generated Star backgrond  
    starBackground();

    //Meteor Shower
    meteor1(220, 385);
    meteor2(100, 300);
    meteor3(15, 180);



    //Big Gun base 
    fill(180);
    stroke(100);
    strokeWeight(5);
    ellipse(200, 380, 40, 40);
    line(200, 380, 180 + mX/10, 350);

    //Laser Beam Function
    laserBeam();

    fill(180);
    stroke(100);
    strokeWeight(2);
  }  
  
  //Life system and game over
  if (lifeCount >= 3) {
    ellipse(10, 10, 10, 10);
    ellipse(20, 10, 10, 10);
    ellipse(30, 10, 10, 10);
  } else if (lifeCount >= 2) {
    ellipse(10, 10, 10, 10);
    ellipse(20, 10, 10, 10);
  } else if (lifeCount >= 1) {
    ellipse(10, 10, 10, 10);
  } else {
    background(200, 0, 0);
    fill(240);
    stroke(200);
    strokeWeight(20);
    line(100, 100, 300, 300);
    line(100, 300, 300, 100);
    ellipse(200, 200, 200, 200);
    fill(0);
    noStroke();
    ellipse(width/2-50, 170, 50, 70);
    ellipse(width/2+50, 170, 50, 70);
    gameOver= true;
  }

//Displays score and difficulty 
  println ("WAVE: " + speed + "!");  
  println ("Score: " + score + "!");
}

////////////////////LASER!
void laserBeam() {
  //Laser beam
  if (mousePressed) {
    if (mY > 350) {
      mY=350;
    }
    noStroke();
    fill(255, 0, 0);
    ellipse(180 + mX/10, 350, 8, 8);
    stroke(255, 0, 0);
    strokeWeight(3);
    line(180 + mX/10, 350, mX, mY);
  }
}
///////////////EXPLOSIONS!
void explosion () {

  noStroke();
  fill(255, 0, 0);
  ellipse(mX, mY, 12, 12);
}



//////////////////////////////// METEORS 
//////////////////////////////Meteor 1
void meteor1 (float rMin, float rMax) {
  boolean existence = true;

  float y = -20 + gravity1;
  float metScale = 30;  
  float xMin = x1 - metScale/2;
  float xMax = x1 + metScale/2;
  float yMin = y - metScale/2;
  float yMax = y + metScale/2;


  if (existence ==  true) {
    fill(200, 25, 25);
    stroke(255, 0, 0);
    strokeWeight(1);

    ellipse(x1, y, metScale, metScale);
  }
  if ((gravity1 > 410)|| ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax))) {

    metScale = 80;
    noStroke();
    fill(0);
    ellipse(x1, y, metScale, metScale);
  }

  if ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax)) {
    score = score +meteorBonus;
    existence = false;  
    explosion ();
    gravity1 = 0;
    x1 = random(rMin, rMax);
  } 

  if (gravity1 > 420) {
    gravity1 = 0;
    x1 = random(rMin, rMax);
    lifeCount --;
  }
  gravity1 = gravity1 + speed;
}
///////////////////////////////////Meteor 2
void meteor2 (float rMin, float rMax) {
  boolean existence = true;


  // float mX = mouseX;
  // float mY = mouseY;
  float y = -20 + gravity2;
  float metScale = 30;  
  float xMin = x2 - metScale/2;
  float xMax = x2 + metScale/2;
  float yMin = y - metScale/2;
  float yMax = y + metScale/2;

  if (existence ==  true) {
    fill(200, 25, 25);
    stroke(255, 0, 0);
    strokeWeight(1);

    ellipse(x2, y, metScale, metScale);
  }
  if ((gravity2 > 410)|| ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax))) {

    metScale = 80;
    noStroke();
    fill(0);
    ellipse(x2, y, metScale, metScale);
  }

  if ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax)) {
    score = score +meteorBonus;
    existence = false;  
    explosion ();
    gravity2 = 0;
    x2 = random(rMin, rMax);
  } 

  if (gravity2 > 420) {
    gravity2 = 0;
    x2 = random(rMin, rMax);
    lifeCount --;
  }
  gravity2 = gravity2 +speed;
  // println("Speed2 " + speed);
  //println("existence" + existence);
}
/////////////////////////////////////// Meteor 3
void meteor3 (float rMin, float rMax) {

  boolean existence = true;

  float y = -20 + gravity3;
  float metScale = 30;  
  float xMin = x3 - metScale/2;
  float xMax = x3 + metScale/2;
  float yMin = y - metScale/2;
  float yMax = y + metScale/2;
  if (existence ==  true) {
    fill(200, 25, 25);
    stroke(255, 0, 0);
    strokeWeight(1);

    ellipse(x3, y, metScale, metScale);
  }
  if ((gravity3 > 410)|| ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax))) {
    explosion ();
    metScale = 80;
    noStroke();
    fill(0);
    ellipse(x3, y, metScale, metScale);
  }

  if ((mousePressed) && (xMin < mX) &&(mX < xMax) && (yMin < mY)&&(mY < yMax)) {
    score = score +meteorBonus; 
    existence = false;  
    gravity3 = 0;
    x3 = random(rMin, rMax);
  } 

  if (gravity3 > 420) {
    gravity3 = 0;
    x3 = random(rMin, rMax);
    lifeCount --;
  }
  gravity3 = gravity3 + speed;
  //println("Speed3 " + speed);
}




////////////////////////////////////STARS
////////GLOBAL VARIABLES FOR STARS
//Random generated Star backgrond

float starXMax = 190;
float starYMax = 170;
float starXMin = 5;
float starYMin = 5;
int starCount = int (frameRate*5);

float starX1 = random(starXMin, starXMax);
float starY1 = random(starYMin, starYMax);

float starX2 = random(starXMin, starXMax);
float starY2 = random(starYMin, starYMax);

float starX3 = random(starXMin, starXMax);
float starY3 = random(starYMin, starYMax);

float starX4 = random(starXMin, starXMax);
float starY4 = random(starYMin, starYMax);

float starX5 = random(starXMin, starXMax);
float starY5 = random(starYMin, starYMax);

float starX6 = random(starXMin, starXMax);
float starY6 = random(starYMin, starYMax);

float starX7 = random(starXMin, starXMax);
float starY7 = random(starYMin, starYMax);

float starX8 = random(starXMin, starXMax);
float starY8 = random(starYMin, starYMax);

float starX9 = random(starXMin, starXMax);
float starY9 = random(starYMin, starYMax);

float starX10 = random(starXMin, starXMax);
float starY10 = random(starYMin, starYMax);

float starX11 = random(starXMin, starXMax);
float starY11 = random(starYMin, starYMax);

float starX12 = random(starXMin, starXMax);
float starY12 = random(starYMin, starYMax);

float starX13 = random(starXMin, starXMax);
float starY13 = random(starYMin, starYMax);

float starX14 = random(starXMin, starXMax);
float starY14 = random(starYMin, starYMax);

float starX15 = random(starXMin, starXMax);
float starY15 = random(starYMin, starYMax);

float starX16 = random(starXMin, starXMax);
float starY16 = random(starYMin, starYMax);

float starX17 = random(starXMin, starXMax);
float starY17 = random(starYMin, starYMax);

float starX18 = random(starXMin, starXMax);
float starY18 = random(starYMin, starYMax);

float starX19 = random(starXMin, starXMax);
float starY19 = random(starYMin, starYMax);

float starX20 = random(starXMin, starXMax);
float starY20 = random(starYMin, starYMax);

/////////////////Random generated Star backgrond
void starBackground() {
  //Changes background based on score, flicker, changes colors, or plain white
  if ((score >=250 ) && (score < 500)) {
    fill(105+abs(150* sin(frameCount/20.0)));
  } else if (score > 500) {
    fill(abs(255* sin(frameCount/60.0)), abs(255* sin(frameCount/30.0)), abs(255* sin(frameCount/10.0)), 105+abs(150* sin(frameCount/20.0)));
  } else {
    fill(255);
  }
  ////Draws a total of 20 stars in the in the bottom right corner
  // StarX Positive, StarY Positive
  rect(200 + starX1, 180 + starY1, 2, 2);
  rect(200 + starX2, 180 + starY2, 2, 2);
  rect(200 + starX3, 180 + starY3, 2, 2);
  rect(200 + starX4, 180 + starY4, 2, 2);
  rect(200 + starX5, 180 + starY5, 2, 2);
  rect(200 + starX6, 180 + starY6, 2, 2);
  rect(200 + starX7, 180 + starY7, 2, 2);
  rect(200 + starX8, 180 + starY8, 2, 2);
  rect(200 + starX9, 180 + starY9, 2, 2);
  rect(200 + starX10, 180 + starY10, 2, 2);
  rect(200 + starX11, 180 + starY1, 2, 2);
  rect(200 + starX12, 180 + starY2, 2, 2);
  rect(200 + starX13, 180 + starY3, 2, 2);
  rect(200 + starX14, 180 + starY4, 2, 2);
  rect(200 + starX15, 180 + starY5, 2, 2);
  rect(200 + starX16, 180 + starY6, 2, 2);
  rect(200 + starX17, 180 + starY7, 2, 2);
  rect(200 + starX18, 180 + starY8, 2, 2);
  rect(200 + starX19, 180 + starY9, 2, 2);
  rect(200 + starX20, 180 + starY10, 2, 2);

//Repeat in bottom left
  // StarX Negative, StarY Positive
  starX1 = starX1 * -1;
  starX2 = starX2 * -1;
  starX3 = starX3 * -1;
  starX4 = starX4 * -1;
  starX5 = starX5 * -1;
  starX6 = starX6 * -1;
  starX7 = starX7 * -1;
  starX8 = starX8 * -1;
  starX9 = starX9 * -1;
  starX10 = starX10 * -1;
  starX11 = starX11 * -1;
  starX12 = starX12 * -1;
  starX13 = starX13 * -1;
  starX14 = starX14 * -1;
  starX15 = starX15 * -1;
  starX16 = starX16 * -1;
  starX17 = starX17 * -1;
  starX18 = starX18 * -1;
  starX19 = starX19 * -1;
  starX20 = starX20 * -1;

  rect(200 + starX1, 180 + starY1, 2, 2);
  rect(200 + starX2, 180 + starY2, 2, 2);
  rect(200 + starX3, 180 + starY3, 2, 2);
  rect(200 + starX4, 180 + starY4, 2, 2);
  rect(200 + starX5, 180 + starY5, 2, 2);
  rect(200 + starX6, 180 + starY6, 2, 2);
  rect(200 + starX7, 180 + starY7, 2, 2);
  rect(200 + starX8, 180 + starY8, 2, 2);
  rect(200 + starX9, 180 + starY9, 2, 2);
  rect(200 + starX10, 180 + starY10, 2, 2);
  rect(200 + starX11, 180 + starY1, 2, 2);
  rect(200 + starX12, 180 + starY2, 2, 2);
  rect(200 + starX13, 180 + starY3, 2, 2);
  rect(200 + starX14, 180 + starY4, 2, 2);
  rect(200 + starX15, 180 + starY5, 2, 2);
  rect(200 + starX16, 180 + starY6, 2, 2);
  rect(200 + starX17, 180 + starY7, 2, 2);
  rect(200 + starX18, 180 + starY8, 2, 2);
  rect(200 + starX19, 180 + starY9, 2, 2);
  rect(200 + starX20, 180 + starY10, 2, 2);

//repeat in top left
  // StarX Negative, StarY Negative
  starY1 = starY1 * -1;
  starY2 = starY2 * -1;
  starY3 = starY3 * -1;
  starY4 = starY4 * -1;
  starY5 = starY5 * -1;
  starY6 = starY6 * -1;
  starY7 = starY7 * -1;
  starY8 = starY8 * -1;
  starY9 = starY9 * -1;
  starY10 = starY10 * -1;
  starY11 = starY11 * -1;
  starY12 = starY12 * -1;
  starY13 = starY13 * -1;
  starY14 = starY14 * -1;
  starY15 = starY15 * -1;
  starY16 = starY16 * -1;
  starY17 = starY17 * -1;
  starY18 = starY18 * -1;
  starY19 = starY19 * -1;
  starY20 = starY20 * -1;

  rect(200 + starX1, 180 + starY1, 2, 2);
  rect(200 + starX2, 180 + starY2, 2, 2);
  rect(200 + starX3, 180 + starY3, 2, 2);
  rect(200 + starX4, 180 + starY4, 2, 2);
  rect(200 + starX5, 180 + starY5, 2, 2);
  rect(200 + starX6, 180 + starY6, 2, 2);
  rect(200 + starX7, 180 + starY7, 2, 2);
  rect(200 + starX8, 180 + starY8, 2, 2);
  rect(200 + starX9, 180 + starY9, 2, 2);
  rect(200 + starX10, 180 + starY10, 2, 2);
  rect(200 + starX11, 180 + starY1, 2, 2);
  rect(200 + starX12, 180 + starY2, 2, 2);
  rect(200 + starX13, 180 + starY3, 2, 2);
  rect(200 + starX14, 180 + starY4, 2, 2);
  rect(200 + starX15, 180 + starY5, 2, 2);
  rect(200 + starX16, 180 + starY6, 2, 2);
  rect(200 + starX17, 180 + starY7, 2, 2);
  rect(200 + starX18, 180 + starY8, 2, 2);
  rect(200 + starX19, 180 + starY9, 2, 2);
  rect(200 + starX20, 180 + starY10, 2, 2);

//Repeat in top right 
  // StarX Positive, StarY Negative
  starX1 = starX1 * -1;
  starX2 = starX2 * -1;
  starX3 = starX3 * -1;
  starX4 = starX4 * -1;
  starX5 = starX5 * -1;
  starX6 = starX6 * -1;
  starX7 = starX7 * -1;
  starX8 = starX8 * -1;
  starX9 = starX9 * -1;
  starX10 = starX10 * -1;
  starX11 = starX11 * -1;
  starX12 = starX12 * -1;
  starX13 = starX13 * -1;
  starX14 = starX14 * -1;
  starX15 = starX15 * -1;
  starX16 = starX16 * -1;
  starX17 = starX17 * -1;
  starX18 = starX18 * -1;
  starX19 = starX19 * -1;
  starX20 = starX20 * -1; 


  rect(200 + starX1, 180 + starY1, 2, 2);
  rect(200 + starX2, 180 + starY2, 2, 2);
  rect(200 + starX3, 180 + starY3, 2, 2);
  rect(200 + starX4, 180 + starY4, 2, 2);
  rect(200 + starX5, 180 + starY5, 2, 2);
  rect(200 + starX6, 180 + starY6, 2, 2);
  rect(200 + starX7, 180 + starY7, 2, 2);
  rect(200 + starX8, 180 + starY8, 2, 2);
  rect(200 + starX9, 180 + starY9, 2, 2);
  rect(200 + starX10, 180 + starY10, 2, 2);
  rect(200 + starX11, 180 + starY1, 2, 2);
  rect(200 + starX12, 180 + starY2, 2, 2);
  rect(200 + starX13, 180 + starY3, 2, 2);
  rect(200 + starX14, 180 + starY4, 2, 2);
  rect(200 + starX15, 180 + starY5, 2, 2);
  rect(200 + starX16, 180 + starY6, 2, 2);
  rect(200 + starX17, 180 + starY7, 2, 2);
  rect(200 + starX18, 180 + starY8, 2, 2);
  rect(200 + starX19, 180 + starY9, 2, 2);
  rect(200 + starX20, 180 + starY10, 2, 2);

//If the player reaches a score above 750, then the stars change positions every 5 secs
  if (score > 750) {
    starCount ++;
    if (starCount >= frameRate*5) {
      starCount =0;
      starX1 = random(starXMin, starXMax);
      starY1 = random(starYMin, starYMax);

      starX2 = random(starXMin, starXMax);
      starY2 = random(starYMin, starYMax);

      starX3 = random(starXMin, starXMax);
      starY3 = random(starYMin, starYMax);

      starX4 = random(starXMin, starXMax);
      starY4 = random(starYMin, starYMax);

      starX5 = random(starXMin, starXMax);
      starY5 = random(starYMin, starYMax);

      starX6 = random(starXMin, starXMax);
      starY6 = random(starYMin, starYMax);

      starX7 = random(starXMin, starXMax);
      starY7 = random(starYMin, starYMax);

      starX8 = random(starXMin, starXMax);
      starY8 = random(starYMin, starYMax);

      starX9 = random(starXMin, starXMax);
      starY9 = random(starYMin, starYMax);

      starX10 = random(starXMin, starXMax);
      starY10 = random(starYMin, starYMax);

      starX11 = random(starXMin, starXMax);
      starY11 = random(starYMin, starYMax);

      starX12 = random(starXMin, starXMax);
      starY12 = random(starYMin, starYMax);

      starX13 = random(starXMin, starXMax);
      starY13 = random(starYMin, starYMax);

      starX14 = random(starXMin, starXMax);
      starY14 = random(starYMin, starYMax);

      starX15 = random(starXMin, starXMax);
      starY15 = random(starYMin, starYMax);

      starX16 = random(starXMin, starXMax);
      starY16 = random(starYMin, starYMax);

      starX17 = random(starXMin, starXMax);
      starY17 = random(starYMin, starYMax);

      starX18 = random(starXMin, starXMax);
      starY18 = random(starYMin, starYMax);

      starX19 = random(starXMin, starXMax);
      starY19 = random(starYMin, starYMax);

      starX20 = random(starXMin, starXMax);
      starY20 = random(starYMin, starYMax);
    }
  }
}