Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*//////////////////
 **Sacrifice For the Planet by Matthew Murchison
 **
 **The goal of this game is to sacrifece ones ship to save the planet bellow. The planet and ship boath have health, and either way death is inevitabel.
 **Control the ship with your mouse, and watch out for debris that hps over your ship!
 **
 ** Most of the code for the astroids is bassed off of the  gone_Debrising example provided in class,
 **after several of my own attemps I decided to consult the example as I was getting no where. Many thanks for that example!
/*//////////////////


////////////////////
//Global Variables//
////////////////////
int planetHealth = 5;
int uFOHealth = 10;
int gameOver = 1;
int loopEndInstuctions = 0;

// declare Debris related vars, bassed on gone_fishing example
float DebrisPositionX, DebrisPositionY;
boolean DebrisBite=false;
float DebrisBiteTime=0;
float DebrisTop, DebrisBottom;
float mouthLeft, mouthRight, mouthBottom, mouthTop;
//control when the debris gose back to the top
boolean hit = false;

void setup() {
  size(400, 400);
  smooth();
  frameRate(60);
  initializeDebris();
}

void initializeDebris() {
  // set initial Debris position
  DebrisPositionX=random(0, width);
  DebrisPositionY=random(height);
}

void draw() {
  ////////
  //Draw//
  ////////
  //Background
  setBackground();
  //Earth
  setEarth();
  //UFO
  setUFO();
  //Debris
  updateDebrisPosition();
  drawDebris();

  //UI
  instructions();
  doPlanetHealth();
  doUFOHealth();
  textOverlay();

  collisionShip();
  collisionPlanet();


  //////////
  //Update//
  //////////
  //astroids
  //Game Start
  //Game over
}

void setBackground() {
  background(0);
  drawStars();
  drawMoon();
  drawSun();

  //println("UFO health is " + uFOHealth + " .");
}

void drawStars() {
  //Give the stars a random brightness
  float rand1 = random(150, 255);
  float rand2 = random(0, 255);
  float rand3 = random(100, 255);
  strokeWeight(3);
  //Star 1
  stroke(rand1);
  point(300, 200);
  //Star 2
  stroke(rand2);
  point(100, 210);
  //Star 3
  stroke(rand3);
  point(200, 100);
  //Star 4
  stroke(rand1);
  point(390, 150);
  //Star 5
  stroke(rand2);
  point(210, 190);
  //Star 6
  stroke(rand3);
  point(110, 250);
} 

void drawMoon() {
  //make shadow fliker
  float rand1 = random(0, 10);
  //Moon
  stroke(0);
  fill(200, 210, 190);
  ellipse(0, 125, 200, 200);
  //Moon Shadow
  strokeWeight(0);
  stroke(0);
  fill(150, 155, 130);
  ellipse(-10, 140, 175, 165);
}

void drawSun() {
  // make the sun fliker
  float rand1 = random(200, 255);
  float rand2 = random(50, 100);
  float rand3 = random(0, 50);

  strokeWeight(0);
  stroke(0);
  //Sun center
  fill(200, 250, 0, rand1);
  ellipse(400, 60, 100, 100);
  //Sun glow
  fill(200, 250, 0, rand2);
  ellipse(400, 70, 130, 130);
  //Sun glow outer
  fill(200, 250, 0, rand3);
  ellipse(400, 80, 150, 150);
}

void setEarth() {
  //atmoshphere
  strokeWeight(0);
  stroke(0);
  fill(10, 10, 200, 150);
  ellipse(200, 410, 700, 100);
  //outer atmoshpere
  fill(10, 10, 200, 100);
  strokeWeight(0);
  stroke(0);
  ellipse(200, 390, 600, 100);
  //earth
  strokeWeight(2);
  stroke(255);
  fill(10, 200, 10);
  ellipse(200, 430, 800, 100);
}

void setUFO() {
  strokeWeight(1);
  stroke(0);
  //UFO bubble
  fill(150);
  ellipse(mouseX, 325, 25, 40);
  //UFO body
  fill(200);
  ellipse(mouseX, 325, 100, 25);
  //UFOline
  fill(150);
  rect(mouseX-55, 322, 110, 4);
}

void instructions() {
  //Show the instuctions ufor 1 second;
  int instruction = 1;
  strokeWeight(2);
  stroke(0);
  fill(255);
  while (instruction == 1) {
    text("You must sacrifce your ship to save the planet, block that debris!", 10, 15);
    text("Be carefull, sometimes the debris will skip by you!", 70, 30);
    text("Take all precautions.", 120, 45);

    instruction = 0;
  }
}

void  doPlanetHealth() {
  //show planet health
  if (planetHealth == 5 ) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    ellipse(20, 300, 12, 12);
    ellipse(20, 320, 10, 10);
    ellipse(20, 340, 10, 10);
    ellipse(20, 360, 10, 10);
    ellipse(20, 380, 10, 10);
  } else if (planetHealth==4) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    ellipse(20, 320, 12, 12);
    ellipse(20, 340, 10, 10);
    ellipse(20, 360, 10, 10);
    ellipse(20, 380, 10, 10);
  } else if (planetHealth==3) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    ellipse(20, 340, 12, 12);
    ellipse(20, 360, 10, 10);
    ellipse(20, 380, 10, 10);
  } else if (planetHealth==2) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    ellipse(20, 340, 12, 12);
    ellipse(20, 360, 10, 10);
  } else if (planetHealth==1) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    ellipse(20, 360, 12, 12);
  } else if (planetHealth==0) {
    gameOver = 3;
  }
}
void  doUFOHealth() {
  rectMode(CENTER);
  //show UFO health
  if (uFOHealth == 10 ) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 300, 8, 8);
    rect(380, 308, 5, 5);
    rect(380, 316, 5, 5);
    rect(380, 324, 5, 5);
    rect(380, 332, 5, 5);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==9) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 308, 8, 8);
    rect(380, 316, 5, 5);
    rect(380, 324, 5, 5);
    rect(380, 332, 5, 5);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==8) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 316, 8, 8);
    rect(380, 324, 5, 5);
    rect(380, 332, 5, 5);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==7) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 324, 8, 8);
    rect(380, 332, 5, 5);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==6) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 332, 8, 8);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==5) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 332, 8, 8);
    rect(380, 340, 5, 5);
    rect(380, 348, 5, 5);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==4) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 348, 8, 8);
    rect(380, 356, 5, 5);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==3) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 314, 8, 8);
    rect(380, 364, 5, 5);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==2) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 364, 8, 8);
    rect(380, 372, 5, 5);
  } else if (uFOHealth==1) {
    stroke(0);
    strokeWeight(1);
    fill(255);
    rect(380, 372, 8, 8);
  } else if (uFOHealth==0) {
    gameOver = 2;
  }
  rectMode(CORNER);
}

void textOverlay() {
  //give messgaes depending on end game
  if (gameOver == 1) {
    //if you avoided the astroids:
  } else if (gameOver == 3) {
    text("The planet died, you have failed.", width/2, width/2);
    //if you got hit by the astroids:
  } else if (gameOver == 2) {
    text("You died for the planet! You have (sortof) won!.", width/2, width/2);
  }
}



////////////
//Astroids//
////////////

void updateDebrisPosition() {
  // move Debris top to bottom
  DebrisPositionX++;

  // if Debris has moved off screen, reset Debris to the top of the screen
  if (DebrisPositionX>400 || hit == true) {
    hit = false;
    // set the debris to the top, on a random horrizontal. Borrowed almost directly from gone_fishing
    DebrisPositionX=0;
    DebrisPositionY= random(0, 400);
  }
}

void drawDebris() {
  // Debris body
  rectMode(CORNERS);
  //Set the debris top location and bottom location
  DebrisTop=DebrisPositionX-20;
  DebrisBottom=DebrisPositionX+20;
  stroke(224, 0, 0);
  fill(224, 0, 0);
  // bottom
  //Draw the debris
  rect(DebrisPositionY-5, DebrisPositionX+5, DebrisPositionY+5, DebrisPositionX-5);

  // noStroke();
  // Debris tail
  // triangle(DebrisPositionY+45, DebrisPositionX, DebrisPositionY+75+sin(frameCount/2)*5, DebrisTop+5, DebrisPositionY+75+sin(frameCount/2)*5, DebrisBottom-5);
}


void collisionShip() {
  rectMode(CORNERS);
  //Draw a bounding box for bug testing
  //fill(200,200,200,100);
  //rect(mouseX + 50, 320, mouseX -50, 350);
  // detect is the debris has hit the UFO
  if (DebrisBottom > 320 && DebrisBottom < 350 && DebrisPositionX-5 <= mouseX+50 && DebrisPositionX+5 >= mouseX-50) {
    //take health of the UFO
    uFOHealth -= 1;
    //send the debris back to the top
    hit = true;
  }
  rectMode(CORNER);
}
void  collisionPlanet() {
  //check if the planet has been hit
  if (DebrisBottom == 400) {
    //take health of the planet
    planetHealth = planetHealth-1;
  }
}