# How to create a point system

**URL:** <https://discourse.processing.org/t/how-to-create-a-point-system/9179>\
**Category:** Coding Questions\
**Created:** [March 10, 2019, 8:56pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179 "2019-03-10T20:56:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![deanie](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@deanie](https://discourse.processing.org/u/deanie)\
**Post date:** [March 10, 2019, 8:56pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/1 "2019-03-10T20:56:42Z")

</div>

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:

```auto
//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!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 10, 2019, 9:17pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/2 "2019-03-10T21:17:15Z")

</div>

**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:

```auto
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()`).

```auto
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`:

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

```

Then we get

```auto
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

---

<div class="post-metadata">

**Author:** ![deanie](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@deanie](https://discourse.processing.org/u/deanie)\
**Post date:** [March 10, 2019, 9:18pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/3 "2019-03-10T21:18:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 10, 2019, 9:22pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/4 "2019-03-10T21:22:24Z")

</div>

I had to correct it a bit, reload it

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

😉

---

<div class="post-metadata">

**Author:** ![deanie](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@deanie](https://discourse.processing.org/u/deanie)\
**Post date:** [March 10, 2019, 9:42pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/5 "2019-03-10T21:42:00Z")

</div>

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

```auto
 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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 10, 2019, 9:44pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/6 "2019-03-10T21:44:43Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![deanie](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@deanie](https://discourse.processing.org/u/deanie)\
**Post date:** [March 10, 2019, 9:46pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/7 "2019-03-10T21:46:55Z")

</div>

Thank you!!! The isAlive boolean worked perfectly 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 10, 2019, 9:52pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/8 "2019-03-10T21:52:36Z")

</div>

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()` :

```auto
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.

> [@Newbie - Trying to make a "Taiko"-like game](https://discourse.processing.org/t/newbie-trying-to-make-a-taiko-like-game/9120/3):
>
> That requires a small timer and then the boules.add(... part from above. The position of the ball is randomly, see command random(). A timer looks like this: if(millis() - timeStarted \> 1000){ boules.add(new Boule......... timeStarted=millis(); } Example Example not using your code above… ArrayList\<Ball\> balls = new ArrayList(); int timeStarted=0; void setup() { size(800, 800); noStroke(); timeStarted=-1000; } // func void draw() { background(255); // for-lo…

Regards, Chrisir

---

<div class="post-metadata">

**Author:** ![deanie](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@deanie](https://discourse.processing.org/u/deanie)\
**Post date:** [March 10, 2019, 9:55pm UTC](https://discourse.processing.org/t/how-to-create-a-point-system/9179/9 "2019-03-10T21:55:37Z")

</div>

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 😉
