Bubble Catcher from Echo

Hi all,

My Bubble Catcher game is based on the example from the book “Learning Processing,” Chapter 10, written by Daniel Shiffman.

BubbleCatcher

Here is the code:

Catcher catcher; // a catcher object
Timer timer; // a timer
Bubble[] bubbles; // an array of bubbles

int totalBubbles = 0;

void setup(){
size(600,600);
smooth();

catcher = new Catcher(40); // catcher’s width 40;
timer = new Timer(500); // a timer that goes off every 0.5 second
bubbles = new Bubble[1000];

timer.start();
}

void draw(){
background(0);
noStroke();
fill(255);
catcher.position(mouseX, mouseY);
catcher.display();

if ( timer.isFinished()){
bubbles[totalBubbles] = new Bubble();
totalBubbles++;
if(totalBubbles >= bubbles.length){
totalBubbles = 0;
}
timer.start();
}

for (int i = 0; i < totalBubbles; i++){
bubbles[i].move();
bubbles[i].display();
if (catcher.intersect(bubbles[i])){
bubbles[i].caught();
}
}
}

class Bubble{

float x,y;
float speed;
float r;
color c;

Bubble(){
r= 8;
x= random(width);
y= -r*2;
speed=random(1,5);
c = color(random(255),random(255),random(255));
}

void move() {
y += speed;
}

boolean reachedBottom() {
if (y > height + r*2){
return true;
}else{
return false;
}
}

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

void caught(){
speed = 0;
y = -700; // set the location off the screen
}
}

class Catcher{
float h;
float x,y;
color c;

Catcher(float tempH){
h=tempH;
x=0;
y=0;
c=color(255);
}

void position(float tempX, float tempY) {
x = tempX;
y = tempY;
}

void display(){
noStroke();
fill(c);
rect(x, y, h, h);
}

boolean intersect (Bubble d){
float distance = dist (x, y, d.x, d.y);
if (distance < h + d.r){
return true;
} else {
return false;
}
}
}

class Timer{

int savedTime;
int totalTime;

Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}

void start(){
savedTime = millis();
}

boolean isFinished() {
int passedTime = millis()-savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}

}

Right now, it is an endless bubble catching game. I tried to┬ácode that when the catcher misses the bubble and the bubble hit the ground, the screen will show the game is over, but I haven’t worked out yet:(

Comments are closed.