How to create a point system


#1

Hello all, I am trying to make a simple interactive game in Processing 3. I currently have a class set up to create bubbles on the screen starting at the bottom and slowly floating to the top. I also have the bubbles disappear upon being clicked by the mouse. I would like to create another class which tracks the amount of bubbles that have been clicked and displays that information on the screen. In addition, if possible, I would like to have the Processing window display “Game Over” if three or more bubbles reach the top of the screen without being clicked. Here is my code:

//Main code:
Bubble b1;
Bubble b2;

void setup() {
  size(500, 500);
  b1 = new Bubble(2, 64);
  b2 = new Bubble(4, 32);
}

void draw() {
  background(255);
  b1.ascend();
  b1.display();
  b1.top();

  b2.ascend();
  b2.display();
  b2.top();
}

void mousePressed() {
  b1.clicked(mouseX, mouseY);
  b2.clicked(mouseX, mouseY);
}
//Bubble class:
class Bubble {
  float x;
  float y;
  float diameter;

  boolean popped = false;

  Bubble(float tempX, float tempD) {
    x = width/tempX;
    y = height;
    diameter = tempD;
  }

  void clicked(float mx, float my) {
    float d = dist(x, y, mx, my);
    if (d < diameter/2) {
      popped = true;
    }
  }

  void ascend() {
    y--;
    x = x + random(-4, 4);
  }

  void display() {
    if (!popped) {
      stroke(0);
      fill(125, 255, 255);
      ellipse(x, y, diameter, diameter);
    }
  }

  void top() {
    if (y < diameter/2) {
      y = diameter/2;
    }
  }
}

Any and all help is appreciated. Thanks in advance!


#2

Points:

I would like to create another class which tracks the amount of bubbles that have been clicked and displays that information on the screen

just before setup declare a variable (no class needed) int points=0;

display it with text(points, 20,20); - see reference

when a bubble is hit just increase points once

Game Over

I would like to have the Processing window display “Game Over” if three or more bubbles reach the top of the screen without being clicked.

similar to points have another counter named failures.

when failures is 3 you show the Game over screen

to achieve this, have a variable state (of the program) (declare it before setup()) that can be 0 (game play) or 1 (show Game Over screen).

In draw() make an if-clause or switch-clause (see reference). Everything you now have belongs into the state 0 part, the Game Over screen in the state 1 part:

if(state==0) {
    ..... // what you now have
} else if(state==1) {
   background(0); 
   fill(255,0,0); 
   text("Game OVER",....
}

Using constants for states

The numbers 0 and 1 look unclear the way I have written it now. To make your code clearer you could name 0 and 1 by assigning them to variables (again before setup()).

int statePlay=0; 
int stateGameOver=1;

Since those are constant (other than state itself they never change) we can declare them as constants using the keyword final:

final int statePlay            = 0; 
final int stateGameOver = 1;

Then we get

if(state==statePlay) {
    ..... // what you now have
} else if(state==stateGameOver) {
   background(0); 
   fill(255,0,0); 
   text("Game OVER",....
}

(it’s a convention to write constants in capitals)

Chrisir


#3

Thank you so much for the help! I’ll go try this out.


#4

I had to correct it a bit, reload it

Remember to hit ctrl-t in processing to get auto-format of your code

:wink:


#5

So I have one small problem. In void top() (which is in my Bubble class), I wrote:

 void top() {
    if (y < diameter/2) {
      y = diameter/2;
      if (popped == false && y == diameter/2) {
        failures = failures + 1;
      }
    }
  }

However, whenever a bubble reaches the top, the counter continuously goes up as long as a bubble is touching the top. What can I change there to make it so that failures only increases by one if a bubble touches the top of the screen?


#6

you can make a new variable isAlive in the class which is initially true:

boolean isAlive = true;

Now on hitting the wall check if it’s alive and only then increase failure. Also set isAlive to false.

I just saw your variable popped is similar to isAlive.

if (popped == false && y== diameter/2 && isAlive==true) {
    failures = failures + 1;
    isAlive=false; // the idea
}

#7

Thank you!!! The isAlive boolean worked perfectly :slight_smile:


#8

Remember to hit ctrl-t in processing to get auto-format

Remark 1

popped is actually similar to isAlive.

Are they the same?

I mean do they serve the same functions? Then you should unify them and get rid of one.

Remark 2

You only have 2 balloons at the moment. You could restart a balloon that is at the top.

In the if-clause in the function top() :

x=random(width-diameter); 
y=height+diameter+random(30); 
isAlive=true; 
popped=false;
diameter=random(18,62); 

Remark 3

Here is a similar thing with many balloons. Instead of restarting them they are managed in an ArrayList and added and removed from it.

Regards, Chrisir


#9

boolean popped is used to determine only whether or not the bubble has been popped. I would prefer to have two separate booleans so that I don’t get as confused.

As for the restart, I will try that out! That was going to be the next thing I worked on :wink: