Catching Particles

For the OOP project, I recreated the catching ball game. To make it simple, I made a rectangular bar which is essentially the basket that is suppose to catch the ellipses that are falling from the sky. Each of these balls are made from the Ball class, which has the attributes xPos, yPos, speed, and size. Every time the ball is not caught by the rectangular bar, the number of missed balls increased and the speed of the ball increases. This basically means the game gets harder at each level because the ball is moving at a random(0.1, 0.3) speed faster than before. I had trouble making the balls come down at different times and position, but I fixed it by starting each of the balls’ yPos to be between random(-width, 0), so that the balls don’t show up all at once. Once a ball reaches the bottom of the screen, I reset its x,y position to a new location on the screen, so that it looks like there’s always new balls coming from the sky.

There a 5 levels to this game. If they reach it, they win.

Every time the user catches 10 balls, it turns into a new level.

If the user misses out on 15 balls, they lose (does not reset at each level).

int level = 0;
int totBall = 10;
int caughtBalls = 0;
int missedBalls = 0;
Ball balls[] = new Ball[totBall];
Catcher catcher;

void setup() {
  size(640, 640);
  catcher = new Catcher();
  for (int i = 0; i < totBall; i++) {
    balls[i] = new Ball(random(width - 20), random(-width, 0), 1.2);
  }
}

void draw() {
  background(0);
  fill(255);
  
  if (level < 6 && missedBalls < 15) {
    text("Level: ", 20, 20);
    text(level, 60, 20);
    
    text("Caught:  ", 20, 40);
    text(caughtBalls, 75, 40);
    
    text("Missed: ", 20, 60);
    text(missedBalls, 70, 60);
    
    for (int i = 0; i < totBall; i++) {
      balls[i].run();
      if (balls[i].isTouchingBar(mouseX, height - 20)) {
        caughtBalls++;
      } else if (balls[i].yPos >= height - 10) {
        missedBalls++;
        balls[i].resetIfEnd(height - 10);
      }
    }
    
    if (caughtBalls % totBall == 0 && caughtBalls != 0) {
      level++;
      caughtBalls = 0;
    }
    
    catcher.display();
  } else if (level > 5) {
    text("YOU WIN", width/2 - 20, height/2);
  } else if (missedBalls >= 15) {
    text("GAME OVER", width/2 - 20, height/2);
    text("LEVEL: ", width/2 - 20, height/2 + 20);
    text(level, width/2 + 20, height/2 + 20);
  }
}

 

class Ball {
  float xPos, yPos, spd, size;
  color clr;
  
  Ball(float x, float y, float speed) {
    xPos = x;
    yPos = y;
    spd = speed;
    size = random(5, 20);
    clr = color(random(0, 255), random(0, 255), random(0, 255));
  }
  
  void display() {
    fill(clr);
    ellipse(xPos, yPos, size, size);
  }
  
  void move() {
    yPos += spd;
  }
  
  void resetIfEnd(int y) {
    if (yPos > y) {
      yPos = random(-width, 0);
      xPos = random(width);
      spd = spd + random(0.1, 0.3);
    } 
  }
  
  void run() {
    move();
    resetIfEnd(height);
    display();
  }
  
  boolean isTouchingBar(float barXPos, float barYPos) {
    if (xPos >= barXPos && xPos <= barXPos + 90 &&  
        yPos >= barYPos && yPos <= barYPos + 20) {
      resetIfEnd(height - 20);
      return true;
    }
    return false;
  }
}

 

Leave a Reply