How to make text that counts objects caught in a game?

edited November 2016 in Questions about Code

Hi - I have a rain catching game where when you hover over the drops, they are caught by a bucket. I wanted to know how I can make an element that counts the drops caught in the bucket and tells the player on the screen? Like "Drops Caught:" text? Would anyone be able to help me?

Here is my code

Bucket bucket;    // Bucket object
Timer timer;        // Timer object
Drop[] drops;       // An array of drop objects
int totalDrops = 0; // The total amount of drops
PImage mouseCursor; // The mouse cursor image

static final int DURATION = 60*1000 + 1000; // The countdown
int begin;


void setup() {
  size(1000, 800);
  bucket = new Bucket(32); // This creates the bucket
  drops = new Drop[1000];    // This creates 1000 spots in the array
  timer = new Timer(300);    // This creates a timer that goes off every 300 milliseconds
  timer.start();             // This command starts the timer

  mouseCursor = loadImage("bucket.png"); // This loads the bucket image into the mouse cursor

  textAlign(RIGHT, BOTTOM); // This creates the text for the countdown
  textSize(30);

  begin = millis();

}

void draw() {
  background(0);

  // This sets the location of the bucket
  bucket.setLocation(mouseX, mouseY); 

  bucket.display(); 

  // This command keeps the timer in check
  if (timer.isFinished()) {
    // This command creates one drop
    drops[totalDrops] = new Drop();
    totalDrops ++ ;
    // This command makes the drops start over when they hit the end of the array
    if (totalDrops >= drops.length) {
      totalDrops = 0; 
    }
    timer.start();
  }

  // This command displays the drops and makes them move
  for (int i = 0; i < totalDrops; i++ ) {
    drops[i].move();
    drops[i].display();
    if (bucket.intersect(drops[i])) {
      drops[i].caught();
    }
  }

  if(mouseX < 1000) { // This command makes the cursor the bucket image at all times
    cursor(mouseCursor, 0, 0);
  } else {
    cursor(HAND);
  }

  final int rest = DURATION - begin - millis(); // This command starts the countdown
  if (rest <= 0)  noLoop();


  fill(255);
  text("Seconds left: " + nf(round(rest/1e3), 2), 
  width>>0, height>>0); // This places text in the countdown and sets its location
}


class Bucket {
  float r;    // radius
  color col;  // color
  float x, y; // location

  Bucket(float tempR) {
    r = tempR;
    col = color(0);
    x = 0;
    y = 0;
  }

  void setLocation(float mouseX, float mouseY) { // This makes the location of the bucket the mouse cursor's location.
    x = mouseX;
    y = mouseY;
  }

  void display() {
    stroke(0);
    fill(col);
    ellipse(x, y, r*2, r*2);
  }

  // This command recognizes the intersection of a drop and the bucket
  boolean intersect(Drop d) {
    // This calculates the distance of the intersection
    float distance = dist(x, y, d.x, d.y); 

    // This compares the intersected distance to the sum of the radii
    if (distance < r + d.r) { 
      return true;
    } else {
      return false;
    }
  }
}


class Drop {
  float x, y;   
  float speed;  
  color c;
  float r;      

  Drop() {
    r = 8;                   
    x = random(width);       // This makes the drops start falling with a random x location
    y = -r*4;                // This makes the drops start falling above the window
    speed = random(1, 5);    // This allows the drops to fall at random speeds
    c = color(139, 208, 250); 
  }

  // This command makes the drops move down
  void move() {
    y += speed;
  }

  // This command checks if the drops hit the bottom of the window
  boolean reachedBottom() {
    // This allows the drops to go lower than the window
    if (y > height + r*4) { 
      return true;
    } else {
      return false;
    }
  }

  void display() {
    // The drop itself
    fill(c);
    noStroke();
    for (int i = 2; i < r; i++ ) {
      ellipse(x, y + i*4, i*2, i*2);
    }
  }

  // This command sets the drop equal to zero if caught by the bucket
  void caught() {
    speed = 0; 
    // This command sets the location of the caught drop to somewhere way off-screen
    y = -1000;
  }
}



class Timer {

  int savedTime; // This is when the timer is started
  int totalTime; // This is how long the timer should last

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  // This command starts the timer
  void start() {
    // When the timer starts it stores the current time in milliseconds.
    savedTime = millis();
  }

  // The function isFinished() returns true if 5,000 ms have passed. 
  boolean isFinished() { 
    // This command checks how much time has passed
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}
Sign In or Register to comment.