Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/********************************************************************
 **  Cheyenne Amil    991340231   
 **  Interactive toy  -  Big Fat Croc 
 **  
 **  The croc moves up and down with the arrow keys and eats food
 **  He gets fatter the more food he eats, slows down the fatter he gets
 **  and the score increases the more food hes eaten as well as well
 **
 ** Up and Down keys to move
 ** Click to bite
 *******************************************************************/

//vertical position of croc
int crocY=300;
int armRotate=30;

//how fast the croc will move
int crocSpeed=5;

//foods x and y position
float foodY=0;
float foodX=0;

//food speed changed randomly too
float foodSpeed=2;

//how much food the croc has eaten so far
int score=0;

//if the food has been bit
boolean chomp=false;

//how fat the croc is
int bellySize=0;


void setup () {

  // Set the Canvas size
  size(1000, 750);

  // set Prefered modes for shapes
  rectMode(CORNER);
  ellipseMode(CORNER);
}


/////////////////////////////////////////////
//**MAIN DRAW FUNCTION**
////////////////////////////////////////////

void draw() {

  //  move croc up and down but set parameters in which it cannot pass a certain point
  if (keyPressed) {
    if (keyCode==UP) {        
      crocY=crocY-crocSpeed;
      armRotate=0;
      if (crocY<=0) {
        crocY=0;
      }
    } else if (keyCode==DOWN) {
      crocY=crocY+crocSpeed;
      armRotate=35;
      if (crocY>=550) {
        crocY=550;
      }
    }
  }


  //set background to a sky blue
  background(121, 173, 245);

  //Water
  fill(12, 121, 234);
  rect(0, 100, 1000, 700);

  //Sand
  fill(219, 133, 53);
  rect(0, 700, 1000, 50);

  //call the move food function and draw the food at that position
  moveFood();
  drawFood();


  // If the mouse is pressed close the Crocs Mouth
  if (mousePressed) {

    //if the food is in the crocs jaws when he bites
    if (foodX>=210 && foodX<=260 && foodY>=crocY+70 && foodY<=crocY+140) {

      // move the score up one, grow the belly and slow croc movement depending on score 
      score++;
      bellySize++;   
      slowCroc();

      // fix belly size to go no bigger than 15
      if (bellySize>=25) {
        bellySize=25;
      }
      chomp=true;
    }
    drawMouthClosed(crocY);
  } else
    drawMouthOpen(crocY);

  // Draw Croc body
  drawCroc(crocY);

  //display the score at the top right of the screen
  textSize(20);
  fill(240+score, 153, 12);
  text("Score: ", 10, 50);
  textSize(20+score);
  fill(240+score, 153-(2*score), 12);
  text(score, 85, 50);
}  

//////////////////////////////////////////
//**DRAW THE BODY OF THE CROC**
////////////////////////////////////////////

void drawCroc(int crocY) {

  //Draw a blue-green tail  
  fill(27, 139, 142);
  noStroke();
  triangle(20, 130+crocY, 100, 100+crocY, 100, 130+crocY);

  //Draw Belly that grows with score (caps when score is at 25)
  fill(223, 232, 162);
  ellipse(100, 100+crocY, 100, 35+bellySize);
  fill(115);
  rect(125, 99+crocY, 2, 35+bellySize);
  rect(150, 100+crocY, 2, 35+bellySize);
  rect(175, 99+crocY, 2, 35+bellySize);

  //Draw Body of Croc
  fill(27, 139, 142);
  rect(100, 100+crocY, 105, 30, 0, 0, 5, 0);


  //Draw spikes on crocs back using a loop  
  fill(34, 106, 104);
  for (int i=0; i<=4; i=i+1) {
    triangle(100+(i*20), 100+crocY, 110+(i*20), 92+crocY, 120+(i*20), 100+crocY);
  }   

  //Draw spikes on crocs tail
  triangle(80, 107+crocY, 100, 100+crocY, 87, 97+crocY);
  triangle(60, 115+crocY, 80, 107+crocY, 67, 104+crocY);
  triangle(40, 123+crocY, 60, 115+crocY, 47, 112+crocY);
  triangle(20, 130+crocY, 40, 123+crocY, 27, 119+crocY);

  //draw back leg of croc
  fill(34, 106, 104);
  ellipse(100, 110+crocY, 20, 30);

  //draw the lower part of limb by rotating an ellipse and fingers from an origin
  pushMatrix();
  translate(100, 130+crocY);
  rotate(radians(armRotate));
  ellipse(0, -5, 15, 30);
  fill(225);
  triangle(5, 25, 9, 25, 7, 28);
  triangle(3, 25, -1, 25, 3, 21);
  triangle(11, 25, 15, 25, 11, 21);
  popMatrix();


  //draw front leg of croc
  fill(34, 106, 104);
  ellipse(180, 110+crocY, 20, 30);

  pushMatrix();
  translate(183, 130+crocY);
  rotate(radians(-armRotate));
  ellipse(0, 0, 15, 30);
  fill(225);
  triangle(5, 30, 9, 30, 7, 33);
  triangle(3, 30, -1, 30, 3, 26);
  triangle(11, 30, 15, 30, 11, 26);
  popMatrix();
}


/////////////////////////////////////////
//**DRAW THE MOUTH CLOSED**
/////////////////////////////////////////

void drawMouthClosed(int crocY) {

  // draw lower jaw with rounded bottom right corner
  fill(36, 106, 108);
  rect(200, 115+crocY, 60, 15, 0, 0, 5, 0);

  //draw upper jaw with rounded top right corner
  fill(27, 139, 142);
  rect(200, 100+crocY, 60, 15, 0, 15, 0, 0);

  //draw nostril
  rect(251, 102+crocY, 7, -6, 25, 25, 0, 0);

  //draw the eye bump
  rect(200, 80+crocY, 20, 22, 10, 10, 0, 0);
  fill(255);
  ellipse(208, 82+crocY, 10, 16);
  fill(0);
  ellipse(212, 83+crocY, 6, 14);

  //draw tiny teeth
  for (int i=0; i<=2; i++) {
    fill(255);    
    triangle(205+(i*20), 115+crocY, 210+(i*20), 115+crocY, 208+(i*20), 110+crocY);
    triangle(215+(i*20), 115+crocY, 220+(i*20), 115+crocY, 218+(i*20), 120+crocY);
  } 

  //draw angry Eyebrows
  fill(0);
  triangle(205, 83+crocY, 225, 90+crocY, 210, 78+crocY);
}

/////////////////////////////////////////
//**DRAW THE MOUTH OPENED**
////////////////////////////////////////

void drawMouthOpen(int crocY) {

  // draw lower jaw with rounded bottom right corner
  // rotate the jaw and teeth by setting an origin and rotating the shapes from there
  pushMatrix(); 

  translate(200, 115+crocY);
  rotate(radians(30));

  //draw rect at new origin and round a corner
  fill(36, 106, 108);
  rect(0, 0, 60, 15, 0, 0, 5, 0);

  // for loop to draw the teeth 
  for (int i=0; i<=5; i++) {
    fill(255);
    triangle(10+(i*9), 0, 15+(i*9), 0, 13+(i*9), -5);
  }
  popMatrix(); // end of the bottom jaw rotation


  //draw upper jaw with rounded top right corner

  pushMatrix();
  translate(200, 100+crocY);
  rotate(radians(-30));

  //upper jaw
  fill(27, 139, 142);
  rect(-5, 0, 65, 15, 0, 15, 0, 0);

  //nostril drawing
  rect(51, 2, 7, -6, 25, 25, 0, 0);

  // for loop to draw teeth on the upper jaw
  for (int i=0; i<=5; i++) {
    fill(255);
    triangle(10+(i*8), 15, 15+(i*8), 15, 13+(i*8), 20);
  }
  popMatrix(); 

  //draw the eye
  fill(27, 139, 142);
  rect(200, 80+crocY, 20, 22, 10, 10, 0, 0);
  fill(255);
  ellipse(208, 82+crocY, 10, 16);
  fill(0);
  ellipse(212, 83+crocY, 6, 14);

  //draw Eyebrow
  fill(0);
  triangle(205, 83+crocY, 205, 90+crocY, 210, 78+crocY);
}

///////////////////////////////////////////////////////
//**DRAW THE FOOD AFTER UPDATING ITS POSITION**
//////////////////////////////////////////////////////

void drawFood() {

  //Drawing a Taco,in order is the tomato, lettuce, beef and the shell
  fill(237, 66, 36);
  ellipse(foodX+2, foodY-2, 21, 5);
  fill(29, 193, 47);
  ellipse(foodX-3, foodY-1, 30, 16);
  fill(152, 104, 68);
  ellipse(foodX-5, foodY+5, 34, 10);
  fill(227, 210, 18);
  rect(foodX, foodY, 25, 15, 50, 50, 2, 2);
}

//////////////////////////////////////////////////
//**MOVE FOOD ALONG SCREEN**
//Nicolas Heslers code for moving and refreshing 
//an objects position was used as reference for moving
//the food in this program. I take no credit for this
//code and have only modified his original code
//////////////////////////////////////////////////
void moveFood() {

  // move the food left at a rate of 2-6 pixels a frame

  foodX=foodX-foodSpeed;

  //if the food goes off the screen or gets eaten, move it back to the start
  if (foodX<=-20 || chomp==true) {
    foodX=1050;
    foodY=random(130, 550);
    chomp= false;

    foodSpeed=random(2, 6);
  } else
    chomp=false;
}

////////////////////////////////////////
//**SLOW THE CROC AS SCORE INCREASES**
/////////////////////////////////////

void slowCroc() {


  //crock originally starts moving at 5pixels a frame
  crocSpeed=5;

  //increments the speed down as his score raises
  if (score>=5 && score<=9) {
    crocSpeed=4;
  } 
  if (score>=10 && score<=14) {
    crocSpeed=3;
  } 
  if (score>=15 && score<=19) {
    crocSpeed=2;
  } 

  //floor speed at 1
  if (score>=20) {
    crocSpeed=1;
  }
}