Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
// C A T B A I T \\
//Object-Oriented Toy by Caitlin Hopwood #9914131163
//Intro to Media Computation Class
//October 22nd, 2016
////////////////////////////////////////////////////
//SOLDIER! YOU CONTROL A LONE FLY BRAVING THE DEPTHS OF A DOMESTIC HOUSEHOLD FULL OF THE LIVING
//BEASTS CALLED CATS. THEY WILL TRY TO SWAT YOU OUT OF THE AIR WITH THEIR PAWS.
//USE THE MOUSE AND AVOID THEM, SOLDIER! OR ELSE YOU WILL FACE AN UNTIMELY DEMISE!
//WE DON'T WANT THAT!
///////////////////////////////////////////////////
int numberOfBottomPaws=3;
int numberOfTopPaws=2;
int numberOfCircles=3;
int score;

BottomPaw[] consecutiveBottomPaws = new BottomPaw[numberOfBottomPaws];
TopPaw[] consecutiveTopPaws = new TopPaw[numberOfTopPaws];
Bg[] consecutiveCircles = new Bg[numberOfCircles];
Fly fly;
Bg bG;

void setup() {
  //set the size of the canvas and shape modes
  size (400, 400);
  ellipseMode(CORNERS);
  rectMode(CORNERS);
  score=0;
  println ("DODGE THE PAWS, SOLDIER! RACK UP THAT SCORE!");

  //initialize game objects
  createConsecutiveCircles();
  createConsecutiveBottomPaws();
  createConsecutiveTopPaws();

  //FLY. THIS IS YOU.
  fly=new Fly();
  //create those pretty background circles!
}
//these are the FOR statements for the arrays to prevent clogging the sketch with a lot of code.
void createConsecutiveCircles() {
  for (int i=0; i<consecutiveCircles.length; i++) {
    consecutiveCircles[i]=new Bg();
  }
}

void createConsecutiveBottomPaws() {
  for (int i=0; i<consecutiveBottomPaws.length; i++) {
    consecutiveBottomPaws[i]=new BottomPaw();
  }
}

void createConsecutiveTopPaws() {
  for (int i=0; i<consecutiveTopPaws.length; i++) {
    consecutiveTopPaws[i]=new TopPaw();
  }
}

void draw() {
  background (#C9EDD9);

  //update objects
  //background!
  for (int i=0; i<consecutiveCircles.length; i++) {
    consecutiveCircles[i].update();
  }
  //FOR statement for displaying bottom paws
  for (int i=0; i<consecutiveBottomPaws.length; i++) {
    consecutiveBottomPaws[i].update();
  }
  //FOR statement for displaying top paws
  for (int i=0; i<consecutiveTopPaws.length; i++) {
    consecutiveTopPaws[i].update();
  }
  //display the background circles!
  for (int i=0; i<consecutiveCircles.length; i++) {
    consecutiveCircles[i].display();
  }
  // update game objects
  //FOR statement for updating bottom paws
  for (int i=0; i<consecutiveBottomPaws.length; i++) {
    consecutiveBottomPaws[i].display();
  }
  for (int i=0; i<consecutiveTopPaws.length; i++) {
    consecutiveTopPaws[i].display();
  }
  //displaying the fly
  fly.display();
  //SCORE!
  textSize(24);
  text("SCORE:"+score, 5, 380);
  //update score
  score+=1;
  //if a pawpad touches the fly [player], reset the game
  // for ( int i = 0; i < 3; i ++) {
  //          if (abs(consecutiveBottomPaws[i].position.x + 5) <=60 && abs(mouseY-consecutiveBottomPaws[i].position.y - 10) < 15){
  //    }}

  for ( int i = 0; i < 2; i ++) {
    //if the paws collide with a certain box around the fly position, the game will reset

    //due to time constraints, this part of the game is a little finnicky, unfortunately
    if (consecutiveTopPaws[i].position.x<fly.position.x+50 && consecutiveTopPaws[i].position.x>fly.position.x-50 && consecutiveTopPaws[i].position.y>fly.position.y-40 &&consecutiveTopPaws[i].position.y<fly.position.y+40) {
      setup();
    }
  }
  for ( int i = 0; i < 3; i ++) {

    if (consecutiveBottomPaws[i].position.x<fly.position.x+50 && consecutiveBottomPaws[i].position.x>fly.position.x-50 && consecutiveBottomPaws[i].position.y>fly.position.y-40 && consecutiveBottomPaws[i].position.y<fly.position.y+40) {
      setup();
    }
  }
}
class Bg {
  //global variables
  PVector position=new PVector();
  PVector velocity=new PVector();
  color circleColor;

  Bg() {
    init();
  }     
  void init() {
    //function for initializing initial position of circles in background
    circleColor=#D7F5E5;
    position.x=random(width, +800);
    position.y=height/2;
    velocity.x=-1;
  }
  void display() {
    noStroke();
    fill(circleColor);
    ellipse(position.x-100, position.y-200, position.x+100, position.y+200);
  }
  void update() {
    //if a circle reaches 100px outside of canvas, reset position
    position.add(velocity);
    if (position.x<-400) {
      init();
    }
  }
}
class BottomPaw {
  //slap those global variables in there
  PVector position=new PVector();
  PVector velocity=new PVector();
  color catColor;
  float pawColor;
  color toeBeanColor;


  BottomPaw() {
    init();
  }
  //initialize paw starting positions
  void init() {

    position.x=random(width, 700);
    position.y=random(200, height);
    velocity.x=-2;
    velocity.y=2;
    catColor=color(random(100, 200), random(50, 150), random(50, 150));
    pawColor=(255);
    toeBeanColor=(#FFBFBF);
  }

  //function for updating the paws w/ movement
  void update() {
    //move paw from right to left
    position.add(velocity);
    //if paw goes off left of canvas by 100, reset position
    if (position.x<-100) {
      init();
    }
    //if the top of the paw reaches a random position between 200 and 250px, reverse Y speed
    if (position.y<=random(200, 250)) {
      velocity.y=2;
    }
    //if the top of the paw reaches a random position between 370 and 400px, reverse Y speed
    if (position.y>=random(370, 400)) {
      velocity.y=-2;
    }
  }
  //function for displaying the paws
  void display () {
    rectMode(CORNERS);
    stroke(catColor);
    strokeWeight(1);
    fill(catColor);
    //have the y position at 300 so it clips past the canvas
    rect(position.x-30, position.y-20, position.x+30, position.y+300);
    fill(pawColor);

    //main paw shape
    noStroke();
    ellipse(position.x-40, position.y-120, position.x+40, position.y);

    //toes
    ellipse(position.x-43, position.y-110, position.x-15, position.y-55);
    ellipse(position.x+43, position.y-110, position.x+15, position.y-55);
    ellipse(position.x-17, position.y-97, position.x, position.y-125);
    ellipse(position.x+17, position.y-97, position.x, position.y-125);

    //TOE BEANS.
    fill (toeBeanColor);
    ellipse(position.x-41, position.y-107, position.x-15, position.y-56);
    ellipse(position.x+41, position.y-107, position.x+15, position.y-56);
    ellipse(position.x-15, position.y-95, position.x-1, position.y-123);
    ellipse(position.x+15, position.y-95, position.x+1, position.y-123);


    //pawpad!
    quad(position.x-25, position.y-25, position.x-15, position.y-68, position.x+15, position.y-68, position.x+25, position.y-25);
  }
}
class Fly {
  //global variables for our lil dude here
  PVector position = new PVector();
  color flyColor;

  Fly() {
    flyColor=#EDF26E;
    position.x=90;
    position.y=mouseY;
    //
  }
  void update() {
  }
  void display() {
    //stinger
    fill(#8A8B77);
    triangle(position.x-26, mouseY-5, position.x-50, pmouseY, position.x-26, mouseY+2);
    //wings
    colorMode(HSB);
    //have the wings flicker between many shades of grey to imitate animation
    fill(0, 0, random(30, 100));
    ellipse(position.x-5, mouseY-5, position.x+2, pmouseY-28);
    ellipse(position.x-10, mouseY-5, position.x-2, pmouseY-18);

    //body
    //switch mode back to RGB
    colorMode(RGB);
    //legs
    triangle(position.x+4, mouseY+7, position.x+9, mouseY+6, position.x+7, mouseY+17);
    triangle(position.x+5, mouseY+14, position.x+7, mouseY+16, position.x, mouseY+16);
    stroke(0);
    line(position.x-20, mouseY+7, position.x-22, mouseY+13);
    line(position.x-23, mouseY+7, position.x-25, mouseY+13);
    line(position.x-26, mouseY+7, position.x-28, mouseY+13);
    //nose!!
    fill(#D6D175);
    noStroke();
    triangle(position.x+15, mouseY+5, position.x+18, mouseY, position.x+28, mouseY+4);
    triangle(position.x+26, mouseY+2, position.x+28, mouseY+4, position.x+33, mouseY);
    //main body
    fill(flyColor);
    stroke(#D8D7BA);
    ellipse(position.x-30, mouseY-10, position.x-10, mouseY+8);
    //head
    ellipse(position.x-15, mouseY-10, position.x+20, mouseY+10);
    //eyes
    fill(255);
    ellipse(position.x+2, mouseY-4, position.x+13, mouseY+5);
    //pupil
    fill(0);
    ellipse(position.x+8, mouseY-3, position.x+13, mouseY+3);
    //brow
    strokeWeight(3);
    stroke(flyColor);
    line(position.x+2, mouseY-5, position.x+12, mouseY-1);
  }
}
class TopPaw {
  //gloooballl varrriables!
  PVector position=new PVector();
  PVector velocity=new PVector();
  color catColor;
  float pawColor;
  color toeBeanColor;


  TopPaw() {
    init();
  }
  //initialize paw starting positions
  void init() {

    position.x=random(width, 700);
    position.y=random(0, 199);
    velocity.x=-2;
    velocity.y=-2;
    catColor=color(random(100, 200), random(50, 150), random(50, 150));
    pawColor=(255);
    toeBeanColor=(#FFBFBF);
  }

  //function for updating the paws w/ movement
  void update() {
    //move paw from right to left
    position.add(velocity);
    //if paw goes off left of canvas by 100, reset position
    if (position.x<-100) {
      init();
    }
    //if the top of the paw reaches a random position between 150 and 200 px, reverse Y speed
    if (position.y>=random(150, 200)) {
      velocity.y=-2;
    }
    //if the top of the paw reaches a random position between 0 and 20px, reverse Y speed
    if (position.y<=random(0, 20)) {
      velocity.y=2;
    }
  }
  //function for displaying the paws
  void display () {
    rectMode(CORNERS);
    stroke(catColor);
    strokeWeight(1);
    fill(catColor);
    //have the y position at 300 so it clips past the canvas
    rect(position.x-30, position.y+20, position.x+30, position.y-300);
    fill(pawColor);

    //main paw shape
    noStroke();
    ellipse(position.x-40, position.y+120, position.x+40, position.y);

    //toes
    ellipse(position.x-43, position.y+110, position.x-15, position.y+55);
    ellipse(position.x+43, position.y+110, position.x+15, position.y+55);
    ellipse(position.x-17, position.y+97, position.x, position.y+125);
    ellipse(position.x+17, position.y+97, position.x, position.y+125);

    //TOE BEANS.
    fill (toeBeanColor);
    ellipse(position.x-41, position.y+107, position.x-15, position.y+56);
    ellipse(position.x+41, position.y+107, position.x+15, position.y+56);
    ellipse(position.x-15, position.y+95, position.x-1, position.y+123);
    ellipse(position.x+15, position.y+95, position.x+1, position.y+123);

    //pawpad!
    quad(position.x-25, position.y+25, position.x-15, position.y+68, position.x+15, position.y+68, position.x+25, position.y+25);
  }
  /* void topDectector(){
   //created to detect if fly hits the paw
   fill(0,0,0,0);
   rect(position.x-40,position.y,position.x+40,position.y);
   }*/
}