Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* 
 OBJECT ORIENTED TOY: FLYING PIG
 Author: Heather Cleveland
 The pig follows rhe mouse, avoid the fences and collect the bacon bits!
 */

//ARRAY NAMES

//cloud array 
Cloud[] clouds; 

//fence array 
Leftpost[] leftFencePost;
Rightpost[] rightFencePost;
Middlepost[] middleFencePost;
//score commented out because I couldn't get it to function 
//Scoreboard scoreBoard; 
//bacon array
Baconbit[] baconBits; 

//Variables Declared 
//score
int score = 0; 
int maxScore= 5; 
//counter
float timeCounter;
//clouds
int cloudsSize=10;
//fence arrays 
int leftFencePostSize=1;
int rightFencePostSize = 1;
int middleFencePostSize = 1;
//bacon size
int baconBitsSize=1;

//initilize pig class
Piggie porkers =new Piggie(); 

void setup() {

  //initialize 
  ellipseMode(CENTER); 
  size( 400, 400); 
  noStroke();
  frameRate(60); 
  //counter to have fences, clouds, and bacon spawn timely 
  timeCounter = 100; 

  //score meter
  //scoreBoard= new Scoreboard(); 
  //cloud array
  clouds=new Cloud[cloudsSize]; 
  //fence array
  leftFencePost= new Leftpost[leftFencePostSize]; 
  rightFencePost = new Rightpost[rightFencePostSize];
  middleFencePost = new Middlepost[middleFencePostSize];
  //bacon array
  baconBits= new Baconbit[baconBitsSize];

  //Call Arrays
  //call fence array
  callLeftPost();
  callRightPost();
  callMiddlePost();
  //calls cloud array
  makeNewClouds();
  //calls bacon array
  makeBacon();
}

void draw() {
  noStroke(); 
  //causes timer to work
  timeCounter = timeCounter - 1; 
  //resets arrays, resets timer
  if (timeCounter <= 0) {
    makeBacon(); 
    callLeftPost();
    callRightPost(); 
    callMiddlePost(); 
    timeCounter=100;
  }
  //makes new clouds at different interval than everything else
  if (timeCounter == 20) {
    makeNewClouds();
  }

  //draw background
  background( 140, 160, 255);

  //UPDATE

  //move clouds
  for (int i=0; i<clouds.length; i++) {
    clouds[i].move();
  }
  //move bacon
  for (int i=0; i<baconBits.length; i++) {
    baconBits[i].moveBacon();
    //should remove bacon if hit but it doesn't
    //indicates collision between player and fence
    if (dist( baconBits[i].position.x, baconBits[i].position.y, mouseX, mouseY) < 40) {
      baconBits[i]. resetBacon();
      println("yum");
    }
  }

  ////move leftpost 
  for (int i=0; i<leftFencePost.length; i++) {
    leftFencePost[i].moveLeftPost();
    //indicates collision between player and fence
    if (dist( leftFencePost[i].fencePosition.x, leftFencePost[i].fencePosition.y, mouseX, mouseY) < 40) {
      println("OUCH");
    }
  }

  ////move right post
  for (int i=0; i<rightFencePost.length; i++) {
    rightFencePost[i].moveRightPost();
    //indicates collision between player and fence
    if (dist( rightFencePost[i].fencePosition.x, leftFencePost[i].fencePosition.y, mouseX, mouseY) < 40) {
      println("OUCH");
    }
  }
  ////move middle post
  for (int i=0; i<middleFencePost.length; i++) {
    middleFencePost[i].moveMiddlePost();
    //indicates collision between player and fence
    if (dist( middleFencePost[i].fencePosition.x, leftFencePost[i].fencePosition.y, mouseX, mouseY) < 100) {
      println("OUCH");
    }
  }

  //DISPLAY

  //displays clouds
  for (int i=0; i<clouds.length; i++) {
    clouds[i].display();
  }
  //hill
  fill(235, 238, 252, 70);
  ellipse( 200, 400, 600, 380);
  ellipse( 200, 400, 800, 480);
  ellipse( 200, 400, 1000, 580);
  ellipse( 200, 400, 1200, 680);
  ellipse( 200, 400, 1400, 780);
  fill( 100, 230, 100); 
  ellipse( 200, 400, 600, 340);
   fill( 70, 200, 70, 70); 
  ellipse( 200, 400, 500, 240);
  ellipse( 200, 400, 400, 140);
  ellipse( 200, 400, 300, 40);


  //displays left post 
  for (int i=0; i<leftFencePost.length; i++) {
    leftFencePost[i].displayLeftPost();
  }
  //displays right post 
  for (int i=0; i<rightFencePost.length; i++) {
    rightFencePost[i].displayRightPost();
  }
  //displays middle post 
  for (int i=0; i<middleFencePost.length; i++) {
    middleFencePost[i].displayMiddlePost();
  }

  //displays bacon
  for (int i=0; i<baconBits.length; i++) {
    baconBits[i].displayBacon();
  }

  //pig object
  drawPiggie();
  //scoreBoard.display();
}

void callLeftPost() {
  for (int i=0; i<leftFencePost.length; i++) {
    leftFencePost[i]= new Leftpost( 170, 10, 5, 160, -3, 2, 5);
  }
}

void callRightPost() {
  for (int i=0; i<rightFencePost.length; i++) {
    rightFencePost[i]= new Rightpost( 200, 10, 5, 160, 3, -2, 5);
  }
}
void callMiddlePost() {
  for (int i=0; i<middleFencePost.length; i++) {
    middleFencePost[i]= new Middlepost( 170, random( 90, 200), 30, 40, -3, .2, 5);
  }
}

void makeBacon() {
  for (int i=0; i<baconBits.length; i++) {
    baconBits[i]=new Baconbit( random( 160, 240), random( 200, 260), 1, 30, random(-4, 5), random( -4, 5), 100);
  }
}
class Baconbit {
  //variables
  PVector position; 
  PVector velocity; 

  float baconWidth;
  float baconHeight;
  float colorOpacity; 

  //constructor - intialization 
  Baconbit( float tempPositionX, float tempPositionY, float tempBaconWidth, float tempBaconHeight, float tempBaconVelocityX, float tempBaconVelocityY, float tempBaconOpacity) {
    noStroke(); 
    position=new PVector (tempPositionX, tempPositionY); 
    velocity=new PVector (tempBaconVelocityX, tempBaconVelocityY); 

    baconWidth= tempBaconWidth;
    baconHeight= tempBaconHeight;
    colorOpacity = tempBaconOpacity;
  }

  void moveBacon() {
    position.add(velocity);
  }

  void displayBacon() {
    fill (#8B7264); 
    baconWidth= baconWidth +1;
    baconHeight = baconHeight + 1; 
    rect( position.x, position.y, baconWidth, baconHeight);
    fill(#F0DFCE);
    rect( position.x+9, position.y, baconWidth/5, baconHeight);
  }
  void resetBacon() {
    colorOpacity = 0;
  }
}
class Cloud {
  //variables
  PVector position; 
  PVector velocity; 

  float cloudWidth;
  float cloudHeight;
  color cloudColor;

  //constructor - intialization 
  Cloud( float tempPositionX, float tempPositionY, float tempCloudWidth, float tempCloudHeight, color tempCloudColor, float tempCloudVelocityX, float tempCloudVelocityY) {

    position=new PVector (tempPositionX, tempPositionY); 
    velocity=new PVector (tempCloudVelocityX, tempCloudVelocityY); 

    cloudColor= tempCloudColor;
    cloudWidth= tempCloudWidth;
    cloudHeight= tempCloudHeight;
  }
  void move() {
    position.add(velocity);
  }

  void display() {
    fill (cloudColor); 
    ellipse( position.x, position.y, cloudWidth, cloudHeight);
  }
}
void makeNewClouds() {
  for (int i=0; i<clouds.length; i++) {
    clouds[i]=new Cloud( random( 100, 300), random(275, 300), random(50, 400), random(20, 100), color(random(200, 255)), random( -5, 3), random(-5, -3));
  }
}
class Leftpost {

  //variables
  PVector fencePosition;
  PVector fenceVelocity; 

  float fenceWidth;
  float fenceHeight;
  float fencePositionY;
  float colorOpacity; 
  color fenceColor;

  //constructor 
  Leftpost( float tempPositionX, float tempPositionY, float tempFenceWidth, float tempFenceHeight, float tempFenceVelocityX, float tempFenceVelocityY, float tempFenceOpacity) {

    fencePosition= new PVector (tempPositionX, tempPositionY); 
    fenceVelocity= new PVector (tempFenceVelocityX, tempFenceVelocityY); 
    fenceWidth = tempFenceWidth; 
    fenceHeight = tempFenceHeight;
    colorOpacity = tempFenceOpacity;
  }

  void moveLeftPost() {
    fencePosition.add(fenceVelocity);
  }

  void displayLeftPost() {
    colorOpacity = 180;
    colorOpacity = colorOpacity + -(fencePosition).x/15; 
    fill ( 124, 103, 70, colorOpacity);
    fenceWidth = fenceWidth + .3; 
    fenceHeight = fenceHeight + 1 ; 
    fencePositionY = 100;
    fencePositionY = fencePositionY +2; 
    rect( fencePosition.x, fencePositionY, fenceWidth, fenceHeight);
  }
}
class Middlepost {

  //variables
  PVector fencePosition;
  PVector fenceVelocity; 

  float fenceWidth;
  float fenceHeight;
  float fencePositionY;
  color fenceColor;
  float colorOpacity; 

  //constructor 
  Middlepost( float tempPositionX, float tempPositionY, float tempFenceWidth, float tempFenceHeight, float tempFenceVelocityX, float tempFenceVelocityY, float tempFenceOpacity) {

    fencePosition= new PVector (tempPositionX, tempPositionY); 
    fenceVelocity= new PVector (tempFenceVelocityX, tempFenceVelocityY); 
    fenceWidth = tempFenceWidth; 
    fenceHeight = tempFenceHeight;
    colorOpacity = tempFenceOpacity;
  }

  void moveMiddlePost() {
    fencePosition.add(fenceVelocity);
  }

  void displayMiddlePost() {
    fill ( 124, 103, 70, colorOpacity); 
    colorOpacity = 0;
    colorOpacity = colorOpacity + (fencePosition.y)*1.2;
    fenceWidth = fenceWidth + 6;
    rect( fencePosition.x, fencePosition.y, fenceWidth, fenceHeight);
  }
}
class Piggie {
}//end of class

void drawPiggie() {
  //rectMode(CORNERS);

  //pig colour
  fill( 240, 177, 228);

  //ears
  if (mouseX > 200) {
    triangle(mouseX + 10, mouseY - 10, mouseX - 10, mouseY - 10, mouseX + 10, mouseY - 40);
    triangle( mouseX - 15, mouseY - 10, mouseX + 5, mouseY - 10, mouseX - 20, mouseY - 40);
  } //end of if statement
  else {
    triangle(mouseX - 10, mouseY - 10, mouseX + 10, mouseY - 10, mouseX - 10, mouseY - 40);
    triangle( mouseX + 15, mouseY - 10, mouseX - 5, mouseY - 10, mouseX + 20, mouseY - 40);
  }//end of else

  //head 
  ellipse( mouseX, mouseY, 40, 40);

  //snout
  if ( mouseX > 200) {
    rect( mouseX - 30, mouseY - 5, 30, 20, 20);
  } //end of if
  else {
    rect( mouseX + 5, mouseY - 5, 30, 20, 20);
  } //end of else

  //back wing
  fill(255); 
  if (mouseX > 200) {
    triangle(mouseX + 10, mouseY, mouseX + 50, mouseY - 60, mouseX + 60, mouseY - 60);  
    triangle( mouseX + 50, mouseY - 50, mouseX + 80, mouseY - 60, mouseX + 60, mouseY - 60);
    triangle( mouseX + 40, mouseY - 40, mouseX + 70, mouseY - 50, mouseX + 50, mouseY - 50);
    triangle( mouseX + 30, mouseY - 30, mouseX + 60, mouseY - 40, mouseX + 40, mouseY - 40);
  } //end of if
  else {
    triangle(mouseX - 10, mouseY, mouseX - 50, mouseY - 60, mouseX - 60, mouseY - 60);
    triangle( mouseX - 50, mouseY - 50, mouseX - 80, mouseY - 60, mouseX - 60, mouseY - 60);
    triangle( mouseX - 40, mouseY - 40, mouseX - 70, mouseY - 50, mouseX - 50, mouseY - 50);
    triangle( mouseX - 30, mouseY - 30, mouseX - 60, mouseY - 40, mouseX - 40, mouseY - 40);
  } //end of else

  //pig colour
  fill( 240, 177, 228);

  //chest
  if (mouseX > 200) {
    ellipse( mouseX + 30, mouseY - 5, 60, 50);
  } //end of if
  else {
    ellipse( mouseX - 30, mouseY - 5, 60, 50);
  }//end of else

  //front wing
  fill(255); 
  if (mouseX > 200) {
    triangle(mouseX + 5, mouseY - 10, mouseX + 20, mouseY - 60, mouseX + 30, mouseY - 60);  
    triangle( mouseX + 20, mouseY - 50, mouseX + 50, mouseY - 60, mouseX + 20, mouseY - 60);
    triangle( mouseX + 20, mouseY - 40, mouseX + 40, mouseY - 50, mouseX + 20, mouseY - 50);
    triangle( mouseX + 10, mouseY - 30, mouseX + 30, mouseY - 40, mouseX + 20, mouseY - 40);
  } //end of if
  else {
    triangle(mouseX - 5, mouseY - 10, mouseX - 20, mouseY - 60, mouseX - 30, mouseY - 60);  
    triangle( mouseX - 20, mouseY - 50, mouseX - 50, mouseY - 60, mouseX - 20, mouseY - 60);
    triangle( mouseX - 20, mouseY - 40, mouseX - 40, mouseY - 50, mouseX - 20, mouseY - 50);
    triangle( mouseX - 10, mouseY - 30, mouseX - 30, mouseY - 40, mouseX - 20, mouseY - 40);
  } //end of else

  //pig colour
  fill( 240, 177, 228);

  //butt
  if ( mouseX > 200) {
    ellipse( mouseX + 60, mouseY - 5, 80, 60);
  } //end of if
  else {
    ellipse ( mouseX - 60, mouseY - 5, 80, 60);
  } //end of else

  //tail
  if (mouseX > 200) {
    triangle( mouseX + 80, mouseY - 30, mouseX +  90, mouseY - 20, mouseX + 110, mouseY - 50);
  } //end of if
  else {
    triangle( mouseX - 80, mouseY - 30, mouseX -  90, mouseY - 20, mouseX - 110, mouseY - 50);
  } //end of else

  //front legs
  if (mouseX > 200) {
    rect( mouseX + 10, mouseY -5, 10, 40);
    rect( mouseX + 30, mouseY -5, 10, 40);
  } //end of if
  else {
    rect( mouseX - 10, mouseY -5, 10, 40);
    rect( mouseX - 30, mouseY -5, 10, 40);
  } //end of else

  //back legs
  if (mouseX > 200) {
    rect( mouseX + 70, mouseY -7, 10, 50);
    rect( mouseX + 90, mouseY -7, 10, 50);
  } //end of if
  else {
    rect( mouseX - 70, mouseY -7, 10, 50);
    rect( mouseX - 90, mouseY -7, 10, 50);
  } //end of else

  //butt
  fill(0);
  if (mouseX > 200) {
    ellipse( mouseX + 85, mouseY - 10, 3, 3);
  } //end of if
  else {
    ellipse( mouseX - 85, mouseY - 10, 3, 3);
  } //end of else
}//end of drawpig()
//end of routine drawPig()
class Rightpost {

  //variables
  PVector fencePosition;
  PVector fenceVelocity; 

  float fenceWidth;
  float fenceHeight;
  float fencePositionY;
  float colorOpacity;
  color fenceColor;

  //constructor 
  Rightpost( float tempPositionX, float tempPositionY, float tempFenceWidth, float tempFenceHeight, float tempFenceVelocityX, float tempFenceVelocityY, float tempFenceOpacity) {

    fencePosition= new PVector (tempPositionX, tempPositionY); 
    fenceVelocity= new PVector (tempFenceVelocityX, tempFenceVelocityY); 
    fenceWidth = tempFenceWidth; 
    fenceHeight = tempFenceHeight;
    colorOpacity = tempFenceOpacity;
  }

  void moveRightPost() {
    fencePosition.add(fenceVelocity);
  }

  void displayRightPost() {
    colorOpacity = 0;
    colorOpacity = colorOpacity + fencePosition.x*.8; 

    fill ( 124, 103, 70, colorOpacity); 
    fenceWidth = fenceWidth + .3; 
    fenceHeight = fenceHeight + 1; 
    fencePositionY = 100;
    fencePositionY = fencePositionY +2; 
    rect( fencePosition.x, fencePositionY, fenceWidth, fenceHeight);
  }
}
class Scoreboard {
  //didn't end up working :( 
  void scoreCount() {
  }
  void display() { 
    stroke(0); 
    fill(255); 
    for (int i=0; i<maxScore; i++) {
      rect( i*30+10, 5, 1*20+10, 20);
    }
    //current score
    fill(0);
    for (int i=0; i<score; i++) {
      rect(i*20+10, 5, i*20+10, 20); 
      noStroke();
    }
  }
}