Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
///////////////////////////////////////////////////////////////
//                ** FEED THE PUPPERS**                      //
//                   By Kael Sarmento                        //
//        Click on the Basket to drag a Biscuit to the       //
//                        Pupper.                            //
///////////////////////////////////////////////////////////////

//for easy setting of number of puppers
int numberOfPuppers = 3;
//make a pupper array
Pupper[] myPupper = new Pupper[numberOfPuppers];
Biscuit myBiscuit;

//core used with permission by Khan-Ali, to help with feeding the puppers - implemented by me
KXIcore core;


void setup() {
  size(650, 400);
  //initialize the puppers
  //create array loop for the puppers
  for (int i = 0; i < myPupper.length; i++) {
    myPupper[i] = new Pupper();
  }
  myBiscuit = new Biscuit();
}

void draw() {
  //initilize core used with permission by Khan-Ali
  core = new KXIcore(mouseX, mouseY, mousePressed);

  drawBackground();

  //draw the amount of puppers asked for in the array (numberOfPuppers)
  for (int i = 0; i < myPupper.length; i++) {  
    myPupper[i].drawPupper();
    myPupper[i].movePupper();
    myPupper[i].pupperEat();
  }
  myBiscuit.displayBiscuit();
  myBiscuit.displayBasket();
  myBiscuit.drag();
}

void drawBackground() {
  background(98, 161, 204);
  rectMode(CORNER);
  fill(62, 101, 127);
  //create floor
  rect(0, 300, 650, 100);
  fill(200, 233, 255);
  //create wall boards idk what theyre called i forget
  rect(0, 280, 650, 20);
  rectMode(CENTER);
}
class Biscuit {
  //create variables for creating a biscuit instance and being able to drag/drop the biscuit
  boolean displayed;
  //boolean dragged;
  boolean mouseOver;

  // create biscuit positions
  PVector biscuitPosition = new PVector (0, 0);
  float biscuitSpeed;

  Biscuit() {
  }

  void displayBiscuit () {
    if (displayed) {
      ellipseMode(CENTER);
      fill(102, 44, 18);
      ellipse(biscuitPosition.x, biscuitPosition.y, 25, 25);
      //display the biscuit at mouse coordinates
      biscuitPosition.x = mouseX;
      biscuitPosition.y = mouseY;
    }

    //if (dragged) {
    //  //display the biscuit at mouse coordinates
    //  biscuitPosition.x = mouseX;
    //  biscuitPosition.y = mouseY;
    //}

    //if the biscuit isnt grabbed, but is drawn, make biscuit fall
    /* if (!grabbed && alive) {
     biscuitPosition.y --;
     if (biscuitPosition.y < 350){
     biscuitPosition.y = 350;
     }
     println("god damn" + frameCount); 
     }
     } */
  }
  void displayBasket() {
    rectMode(CORNER);
    fill(255, 140, 123);
    //basket
    rect(550, 290, 70, 80, 10);
    fill(204, 112, 98);
    //inside shadow
    rect(555, 295, 60, 20, 5);
    fill(127, 70, 62);
    //biscuits
    rect(555, 305, 60, 10, 5);
    fill(232, 127, 112);
    //shadow
    rect(555, 320, 60, 50, 5);
  }

  void drag() {
    //if the mouse is over the basket...
    if (mouseY > 290 && mouseY < 370 && mouseX > 550 && mouseX < 620) {
      //println("mouseover is true" + frameCount);
      mouseOver = true; //the mouse is over the basket
    } else {
      mouseOver = false; //if mouse is not over, mouseover is false
    }

    //if mouse is over the basket, and mouse is held..
    if (mouseOver == true && mousePressed == true) {
      //dragged = true;
      displayed = true;
    }
  }
   
   /////////////////////////////////////////////
   // code that would have made the biscuit   //
   // fall. didnt work out, and im sad but    //
   // i wanted to show you all the hard work  //
   // i did                                   //
   /////////////////////////////////////////////
   
  /*
    if (biscuitPosition.y <= 300 && !dragged && displayed) { // check if the biscuit is on the ground
   biscuitPosition.y+=4;
   println("WHY ISNT THE BISCUIT FALLING");
   }
   void falling () {
   if (biscuitPosition.y < 300) {
   isFalling=true;
   if (isFalling==true) {
   biscuitPosition.y += 4;
   } else if (biscuitPosition.y >= 300) {
   isFalling=false;
   }
   }
   }
   */
}
/*
  KXI core functions V1 - FOR PUBLIC
 by: Khan-ali Ibrahim
 
 Functions for common use tasks, work smart not hard people.
 Mainly to handle mouse events but I stuff other stuff I need in there.
 
 This is a class I pass around, you are free to use as long as you credit me.
 
 Don't instantiate/call the function in SETUP, do it in DRAW or else the mouse events won't work
 repeat do (name of object) = new KXIcore(mouseX, mouseY, mousePressed); IN DRAW
 */

class KXIcore {
  //varibles
  float mX, //mouseX
    mY; //mouseY
  boolean mP; //mousePressed

  //Constructor to pass the mouse inputs along
  KXIcore(int x, int y, boolean p) {
    mX = x;
    mY = y;
    mP = p;
  }

  //function that draws an invisible rectangle in CORNERS mode
  //if the mouse is over the invisble box it returns true
  boolean mouseOverCorner(float leftX, float leftY, float rightX, float rightY) {
    if (mX > leftX && mX < rightX && mY > leftY && mY < rightY) {
      return true;
    } else {
      return false;
    }
  }

  //function that draws an invisible rectangle in CORNERS mode
  //if the mouse is over and pressed in the invisble box it returns true
  boolean mouseClickCorner(float leftX, float leftY, float rightX, float rightY) {
    if (mouseOverCorner(leftX, leftY, rightX, rightY) && mP) {
      return true;
    } else {
      return false;
    }
  }

  //function that draws an invisible rectangle in CENTER mode
  //if the mouse is over the invisble box it returns true
  boolean mouseOverCenter(float centerX, float centerY, float sizeX, float sizeY) {
    //finds the sides of the rectangle
    float leftSide = centerX - (sizeX/2), 
      rightSide = centerX + (sizeX/2), 
      topSide = centerY - (sizeY/2), 
      bottomSide = centerY + (sizeY/2);

    if (mX > leftSide && mX < rightSide && mY > topSide && mY < bottomSide) {
      return true;
    } else {
      return false;
    }
  }

  //function that draws an invisible rectangle in CENTER mode
  //if the mouse is over and pressed in the invisble box it returns true
  boolean mouseClickCenter(float centerX, float centerY, float sizeX, float sizeY) {
    if (mouseOverCenter(centerX, centerY, sizeX, sizeY) && mP) {
      return true;
    } else {
      return false;
    }
  }

  //function that when given a two points and one point inbetween them will calculate the percentage
  //between the two points // I used this for my sliders
  float findPercent(int min, int max, int value) {
    //I don't remember the math since I wrote this when I was at night and tired, but it works
    float rangeDistance = max - min;
    float valueDistance = value - min;

    //returns percentage
    return (valueDistance / rangeDistance) * 1;
  }
  
  
  //finds the centerpoint between two points
  float findMidpoint (float pointA, float pointB) {
    //middle school math people, middle school math
    return (pointA + pointB) / 2;
  }
}
class Pupper {

  //Create Pupper Variable Positions (pvectors), for moving along the x-axis/puppy position
  PVector pupperPosition = new PVector(0, 0);
  float pupperSpeed;
  boolean pupperMove = true; 
  boolean pupperFlip = false;

  Pupper() {
    pupperPosition.x = random(-100, 750);
    pupperPosition.y = 300;
    pupperSpeed = random(1, 5); //if spawn on top, make speed random

    //set drawings to center mode
    rectMode(CENTER);
    ellipseMode(CENTER);
    noStroke();
  }  

  void drawPupper() {
    //draw pupper if going right, draw pupper, if going left, draw pupperflipped
    if (pupperFlip) {
      drawPupperFlipped();
    } else {
      fill(255);
      //make back pupper ear
      strokeWeight(15);
      stroke(255);
      line(pupperPosition.x + 40, pupperPosition.y - 15, pupperPosition.x + 50, pupperPosition.y - 10);
      //code some feet for the pupper
      strokeWeight(10);
      line(pupperPosition.x + 25, pupperPosition.y + 25, pupperPosition.x + 25, pupperPosition.y + 33);
      line(pupperPosition.x + 10, pupperPosition.y + 25, pupperPosition.x + 10, pupperPosition.y + 35);
      line(pupperPosition.x - 20, pupperPosition.y + 25, pupperPosition.x - 20, pupperPosition.y + 33);
      line(pupperPosition.x - 35, pupperPosition.y + 25, pupperPosition.x - 35, pupperPosition.y + 30);
      //make pupper tail
      strokeWeight(20);
      line(pupperPosition.x - 40, pupperPosition.y - 10, pupperPosition.x - 50, pupperPosition.y - 20);
      strokeWeight(1);
      noStroke();
      //chubby pupper body
      rect(pupperPosition.x, pupperPosition.y, 100, 70, 90);
      ellipse(pupperPosition.x, pupperPosition.y -15, 96, 50);
      //front pupper ear
      strokeWeight(15);
      stroke(190);
      line(pupperPosition.x - 5, pupperPosition.y - 15, pupperPosition.x - 10, pupperPosition.y - 10);
      stroke(255);
      strokeWeight(13);
      line(pupperPosition.x, pupperPosition.y - 20, pupperPosition.x - 10, pupperPosition.y - 10);
      strokeWeight(1);
      noStroke();
      //pupper eyes
      fill(0);
      ellipse(pupperPosition.x + 40, pupperPosition.y - 10, 10, 10);
      ellipse(pupperPosition.x + 5, pupperPosition.y - 10, 10, 10);
      //puppy mouth
      stroke(0);
      line(pupperPosition.x + 30, pupperPosition.y - 10, pupperPosition.x + 25, pupperPosition.y - 5);
      line(pupperPosition.x + 25, pupperPosition.y - 5, pupperPosition.x + 20, pupperPosition.y - 10);
      noStroke();
    }
  }

  void drawPupperFlipped() {
    // draw the same pupper as above, but flipped so that it can move back and forth properly
    //make pupper ears
    stroke(255);
    strokeWeight(15);
    line(pupperPosition.x - 40, pupperPosition.y - 15, pupperPosition.x - 50, pupperPosition.y - 10);
    //code some feet for the pupper
    strokeWeight(10);
    line(pupperPosition.x - 25, pupperPosition.y + 25, pupperPosition.x - 25, pupperPosition.y + 33);
    line(pupperPosition.x - 10, pupperPosition.y + 25, pupperPosition.x - 10, pupperPosition.y + 35);
    line(pupperPosition.x + 20, pupperPosition.y + 25, pupperPosition.x + 20, pupperPosition.y + 33);
    line(pupperPosition.x + 35, pupperPosition.y + 25, pupperPosition.x + 35, pupperPosition.y + 30);
    //make pupper tail
    strokeWeight(20);
    line(pupperPosition.x + 40, pupperPosition.y - 10, pupperPosition.x + 50, pupperPosition.y - 20);
    strokeWeight(1);
    noStroke();
    //make that pupper bod
    fill(255);
    rect(pupperPosition.x, pupperPosition.y, 100, 70, 90);
    ellipse(pupperPosition.x, pupperPosition.y - 15, 96, 50);
    //front pupper ear
    strokeWeight(15);
    stroke(190);
    line(pupperPosition.x + 5, pupperPosition.y - 15, pupperPosition.x + 10, pupperPosition.y - 10);
    stroke(255);
    strokeWeight(13);
    line(pupperPosition.x, pupperPosition.y - 20, pupperPosition.x + 10, pupperPosition.y - 10);
    strokeWeight(1);
    noStroke();
    //pupper eyes
    fill(0);
    ellipse(pupperPosition.x - 40, pupperPosition.y - 10, 10, 10);
    ellipse(pupperPosition.x - 5, pupperPosition.y - 10, 10, 10);
    //pupper mouth
    stroke(0);
    line(pupperPosition.x - 30, pupperPosition.y - 10, pupperPosition.x - 25, pupperPosition.y - 5);
    line(pupperPosition.x - 25, pupperPosition.y - 5, pupperPosition.x - 20, pupperPosition.y - 10);
    noStroke();
  }

  void movePupper() {
    //if the pupper is moving one way, and hits the width+100, it inverts and moves the other way
    if (pupperMove==true) {
      pupperPosition.x = pupperPosition.x + pupperSpeed;
      if (pupperPosition.x >= width + 100) {
        pupperMove = false;
        pupperFlip = true;
      }
    }
    //same as above, but reversed
    if (pupperMove==false) {
      pupperPosition.x = pupperPosition.x - pupperSpeed;
      if (pupperPosition.x <= width - 750) {
        pupperMove = true;
        pupperFlip = false;
      }
    }
  }

  //if the biscuit is over the pupper in a defined area, the biscuit will dissapear, and yum will be printed
  void pupperEat() { 
    if (core.mouseOverCenter(pupperPosition.x, pupperPosition.y, 50, 35) && myBiscuit.displayed) {
      myBiscuit.displayed = false;
      myBiscuit.biscuitPosition = new PVector(600, 350);
      println("Yum!");
    }

    /////////////////////////////////////////////////
    // Code that was going to be used for making   //
    // the biscuit fall and be eaten, but it wasn't//
    // working so I took it out. Khan let me use   //
    // core class to quickly implement the code    //
    /////////////////////////////////////////////////

    /*
    if (pupperPosition.x - 20 == myBiscuit.biscuitPosition.y - 10 
     || pupperPosition.y - 20 == myBiscuit.biscuitPosition.x - 10){
     eat = true;
     }
     
     if (myBiscuit.biscuitPosition.x > pupperPosition.x - 20 && myBiscuit.biscuitPosition.x > pupperPosition.x + 20
     && myBiscuit.biscuitPosition.y > pupperPosition.x - 20 && myBiscuit.biscuitPosition.y > pupperPosition.x + 20 &&
     myBiscuit.displayed == true && eat == false){
     //eat = true;
     //myBiscuit.biscuitPosition = new PVector(0, 0);
     //myBiscuit.dragged = false;
     //myBiscuit.displayed=false;
     println("fucking yummy " + frameCount);
     }
     if (myBiscuit.biscuitPosition.y == pupperPosition.y && myBiscuit.biscuitPosition.x == pupperPosition.y && !mousePressed){
     println("ugh" + frameCount);
     
     }
     
     //if (myBiscuit.biscuitPosition.x > 100 && myBiscuit.biscuitPosition.y == pupperPosition.y) {
     //  println("uhf " + frameCount);
     //}
     
     //if (eat && myBiscuit.displayed){ //if eat is true, dont display the biscuit
     //  myBiscuit.displayed=false;
     //  eat = false;
     //}
     */
  }
}