Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
// good eats by jessica peng 
 
/* six round creatures bounce around the screen. 
   click the mouse to drop a food pellet. 
   the more you feed them, the more they grow! */

/* references:

  - format of the for loops used in creature and backdrop arrays based on car array 
example from 9.7 in learning processing textbook 

  - bounce function of creatures based on example from natureofcode chapter 1: vectors

  - PVector help from Nic

*/

Creature[] creatures= new Creature[6];
Food speck; 
Backdrop b; 

void setup() {
  
  size(600, 500); 
  rectMode(CENTER);  
  noStroke(); 
  
  b = new Backdrop(); 
  speck = new Food();
  
  // array of creatures with random starting positions and speeds
  
  for (int i = 0; i < creatures.length; i++) {
    creatures[i] = new Creature(random(0, 600), random(0, 500), random(0.5, 5));
  }
  
}

void draw() { 

  // update
  
  speck.update(); 

  for (int i = 0; i < creatures.length; i++) {
    creatures[i].update();
  }


  // display
  
  b.display(); 
  
  for (int i = 0; i < creatures.length; i++) {
    creatures[i].display();
  }

  if (!speck.eaten) {
    speck.display();
  }
}
//gradient background

class Backdrop {  

  void display() {

    background(113, 232, 228);

    fill(255, 0, 0); 
    text("click to feed the creatures!", 225, 200);
    
    for (int i = 0; i < 9; i++) {
     fill(238, 192, 92, 10 + i*30);
     rect(300, 75 + i*50, 600, 50); 
    }
    
  }
}
class Creature { //<>//

  PVector position;
  PVector velocity;

  PVector randomDirection; 
  PVector directionToFood;
  float speed;

  int faceSize; 
  int eyeSize; 
  color eyeColor;


  // constructor - starting values for position, speed, size, and color
  
  Creature(float positionX, float positionY, float creatureSpeed) {
    position = new PVector(positionX, positionY);
    speed = creatureSpeed;

    faceSize = 20;
    eyeSize = 5;
    eyeColor = color(255, 255, 255);

    randomDirection= new PVector(random(0, 600), random(0, 500));
  }

  void update() {

    // when there's no food onscreen
    if (speck.eaten) {   
      
      // creatures bounce off edges
      
      if ((position.x > width) || (position.x < 0)) {
        randomDirection.x = randomDirection.x * -1;
      }

      if ((position.y > height) || (position.y < 0)) {
        randomDirection.y = randomDirection.y * -1;
      }
      // creatures move in a random direction
      directionToFood=randomDirection;
    } 
    
    // when there IS food on the screen
    else {
      
      // so creatures can locate food position 
      PVector foodPosition = new PVector(speck.foodPositionX, speck.foodPositionY);
      
      // creatures move toward food 
      directionToFood = PVector.sub(foodPosition, position);

      /* if distance b/n food and creature is small enough
      i.e. (creature is touching the food)
      then food is eaten */

      if (abs(speck.foodPositionX - position.x) < 2 && (abs(speck.foodPositionY - position.y) < 5)) {
        speck.eaten = true;
        
        // eye color changes 
        eyeColor = color(255, 150, 0); 
        
        // creature size will increase with food intake, up to 50
        if (faceSize < 50) {
          faceSize = faceSize + 1;
        }
      }
    }
    
    directionToFood.normalize(); 
    velocity = PVector.mult(directionToFood, speed); 
    position.add(velocity);
    
  }

  void display() {
    
    // creature face
    fill(0); 
    ellipse(position.x, position.y, faceSize, faceSize);
    
    //creature eyes 
    fill(eyeColor);
    ellipse(position.x + 5, position.y, eyeSize, eyeSize);
    ellipse(position.x - 5, position.y, eyeSize, eyeSize);
  }
}
class Food {

  float foodPositionX; 
  float foodPositionY; 
  boolean eaten; 


  // constructor - set eaten to true as default so program starts with no food on screen
  Food() {
    eaten = true;
  }

  void update() {

    // food appears at coordinates where mouse is pressed  
    
    if (mousePressed == true) {  
      foodPositionX = mouseX;
      foodPositionY = mouseY;
      eaten = false;
    }
  }

  void display() {

    // if food is eaten, set opacity to 0 - food appears to disappear
    
    if (eaten) {
      fill(255, 0);
      rect(foodPositionX, foodPositionY, 5, 10, 20);
    } else {
      fill(255); 
      rect(foodPositionX, foodPositionY, 5, 10, 20);
    }
  }
}