# How to add score and ending to my game

**URL:** https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235
**Category:** Coding Questions
**Created:** [August 7, 2019, 3:46pm UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235 "2019-08-07T15:46:28Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 7, 2019, 3:46pm UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/1 "2019-08-07T15:46:28Z")

</div>

Hi all, I’m new on this forum. I’ve writing my first game to processing and I’m trying to add score and ending to my game. I’ve just add some lines of code but don’t work. Someone can help me?  
Here the code:

```auto
float x = 360;
float y = random(0,360);
float x2 = 360;
float y2 = random(0,360);
float x3 = 360;
float y3 = random(0,255);
PFont f;
int score = 0;
int lives = 5;
boolean lost = false;
void setup() {
  size(400,400);
  background(255);
  f = createFont("Times New Roman",25,true);
}
void randomrect() {
  fill(0);
  rect(x,y,40,40);
  x = x - 2;
  if(x<-40) {
    x = 360;
    y = random(0,360);
  }
}
void randomrect2() {
  fill(0);
  rect(x2,y2,40,40);
  x2 = x2 - 2;
  if(x2<-40) {
    x2 = 360;
    y2 = random(0,360);
  }
}
void randomrect3() {
  fill(0);
  rect(x3,y3,40,40);
  x3 = x3 - 2;
  if(x3<-40) {
    x3 = 360;
    y3 = random(0,360);
  }
}
 for(int count = 0; count <= 5 ; count = count+1) {
void gameover(float squarex, float squarey) {
    if(((mouseY>squarey) && (mouseY<squarey+40) && (squarex <= 80) && (squarex >= 0)) || ((mouseY+40>squarey) && (mouseY+40<squarey+40) && (squarex <= 80) && (squarex >= 0))) {
      lives- -;
    } 
    else {
      if((lives<=0) && (count>=0)) {
      textFont (f,25);
      fill(255,0,0);
      text("GAME OVER",140,160);
      noLoop(); 
      } 
    } 
  } 
 }
void draw() {
  println(x,y,x2,y2);
  background(255);
  noStroke();
  fill(200,0,0);
  rect(40,mouseY,40,40);
  text("score=" + score,340,20);
  text("lives=" + lives, 0,20);
  score++;
gameover(x,y);
gameover(x2,y2);
gameover(x3,y3);
  randomrect();
  if(frameCount>60) {
  randomrect2();
  }
if (frameCount>120) {
  randomrect3();
}
if (lives<=0) {     
  lost = true;     
  noLoop();     
  textSize(20);     
  text("Click to Restart", 125,100);     
  textSize(13); 
}
}

```

Thanks for the replies.

---

<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: [August 7, 2019, 8:25pm UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/2 "2019-08-07T20:25:03Z")

</div>

Welcome to the forum!

Chrisir

---

<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: [August 7, 2019, 8:27pm UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/3 "2019-08-07T20:27:55Z")

</div>

this line was outside of any function - this is not allowed:

`for (int count = 0; count <= 5; count = count+1) {`

---

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 8, 2019, 8:40am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/4 "2019-08-08T08:40:27Z")

</div>

Yes, I’ve see it after and i fix the function for inside the function void

void gameover(float squarex, float squarey) {  
for(int count = 0; count \<= 5 ; count = count+1) {  
if(((mouseY\>squarey) && (mouseY\<squarey+40) && (squarex \<= 80) && (squarex \>= 0)) || ((mouseY+40\>squarey) && (mouseY+40\<squarey+40) && (squarex \<= 80) && (squarex \>= 0))) {  
lives–;  
}

but I don’t know to work it because the game when finish don’t restart again

---

<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: [August 8, 2019, 8:59am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/5 "2019-08-08T08:59:13Z")

</div>

I don’t follow your logic there.

I think you don’t need a for loop.

Instead you want to increase a counter when a collision occurs.

The thing is that you count each collision multiple times since the condition is still true. Try something like using a boolean.

before setup boolean firstTime = true; // indicator whether we counted this collision already

when a collision occurs count it only when firstTime == true; set firstTime to false then.

When no collision occurs anymore say firstTime = true; to prepare for the next collision

---

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 8, 2019, 9:35am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/6 "2019-08-08T09:35:46Z")

</div>

Sorry for the misunderstanding, this is the first time I turn to language as processing. I’m trying to write what you told me, but if you can help me, could you write the code?

---

<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: [August 8, 2019, 9:55am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/7 "2019-08-08T09:55:05Z")

</div>

Sure I just let you do the first serv

Then I can look at it

---

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 8, 2019, 10:05am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/8 "2019-08-08T10:05:57Z")

</div>

```auto
float x = 360;
float y = random(0,360);
float x2 = 360;
float y2 = random(0,360);
float x3 = 360;
float y3 = random(0,255);
PFont f;
int score = 0;
boolean firstTime=true;
void setup() {
  size(400,400);
  background(255);
  f = createFont("Times New Roman",25,true);
}
void randomrect() {
  fill(0);
  rect(x,y,40,40);
  x = x - 2;
  if(x<-40) {
    x = 360;
    y = random(0,360);
  }
}
void randomrect2() {
  fill(0);
  rect(x2,y2,40,40);
  x2 = x2 - 2;
  if(x2<-40) {
    x2 = 360;
    y2 = random(0,360);
  }
}
void randomrect3() {
  fill(0);
  rect(x3,y3,40,40);
  x3 = x3 - 2;
  if(x3<-40) {
    x3 = 360;
    y3 = random(0,360);
  }
}
void gameover(float squarex, float squarey) {
    if(((mouseY>squarey) && (mouseY<squarey+40) && (squarex <= 80) && (squarex >= 0)) || ((mouseY+40>squarey) && (mouseY+40<squarey+40) && (squarex <= 80) && (squarex >= 0))) {
      textFont (f,25);
      fill(255,0,0);
      text("GAME OVER",140,160);
      noLoop();    
    }
}
void draw() {
  println(x,y,x2,y2);
  background(255);
  noStroke();
  fill(200,0,0);
  rect(40,mouseY,40,40);
  text(" score = "+score,300,20);
  if(firstTime==true) {
    firstTime=false;
    score++;
  }
   else {
     firstTime=true;
   } 
gameover(x,y);
gameover(x2,y2);
gameover(x3,y3);
  randomrect();
  if(frameCount>60) {
  randomrect2();
}
if (frameCount>120) {
  randomrect3();
}
}

```

---

<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: [August 8, 2019, 10:10am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/9 "2019-08-08T10:10:33Z")

</div>

Does it work?

I have the feeling Ou didn’t implement what I said…

---

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 8, 2019, 10:13am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/10 "2019-08-08T10:13:45Z")

</div>

Hmm don’t seems work

---

<div class="post-metadata">

### Author: ![Mike92](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mike92](https://discourse.processing.org/u/Mike92)
#### Post date: [August 8, 2019, 10:44am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/11 "2019-08-08T10:44:26Z")

</div>

```auto
float x = 360;
float y = random(0,360);
float x2 = 360;
float y2 = random(0,360);
float x3 = 360;
float y3 = random(0,255);
PFont f;
int score = 0;
int lives = 5;
boolean firstTime=true;
void setup() {
  size(400,400);
  background(255);
  f = createFont("Times New Roman",25,true);
}
void randomrect() {
  fill(0);
  rect(x,y,40,40);
  x = x - 2;
  if(x<-40) {
    x = 360;
    y = random(0,360);
  }
}
void randomrect2() {
  fill(0);
  rect(x2,y2,40,40);
  x2 = x2 - 2;
  if(x2<-40) {
    x2 = 360;
    y2 = random(0,360);
  }
}
void randomrect3() {
  fill(0);
  rect(x3,y3,40,40);
  x3 = x3 - 2;
  if(x3<-40) {
    x3 = 360;
    y3 = random(0,360);
  }
}
void gameover(float squarex, float squarey) {
        for(int i=0; i<=5; i++) {
    if(firstTime==true) {
    if(((mouseY>squarey) && (mouseY<squarey+40) && (squarex <= 80) && (squarex >= 0)) || ((mouseY+40>squarey) && (mouseY+40<squarey+40) && (squarex <= 80) && (squarex >= 0))) {
      lives--;
      firstTime=false;
    } 
      else {
     if(lives<=0) {
      textFont (f,25);
      fill(255,0,0);
      text("GAME OVER",140,160);
      noLoop(); 
     } 
      }  
    } 
         } 
}
void draw() {
  println(x,y,x2,y2);
  background(255);
  noStroke();
  fill(200,0,0);
  rect(40,mouseY,40,40);
  text(" score = "+score,300,20);
  text(" lives = "+lives,0,20);
   
gameover(x,y);
gameover(x2,y2);
gameover(x3,y3);
  randomrect();
  if(frameCount>60) {
  randomrect2();
}
if (frameCount>120) {
  randomrect3();
}
}

```

I’ve tried this but the lives count down only first time the rect touch the other rect

---

<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: [August 8, 2019, 10:47am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/12 "2019-08-08T10:47:43Z")

</div>

In case no collision occurs:

firstTime=true;

---

<div class="post-metadata">

### Author: ![megaankara34](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/megaankara34/32/10997_2.png) [@megaankara34](https://discourse.processing.org/u/megaankara34)
#### Post date: [November 3, 2020, 12:22am UTC](https://discourse.processing.org/t/how-to-add-score-and-ending-to-my-game/13235/13 "2020-11-03T00:22:38Z")

</div>

Mira. era fácil, ya quedó

```auto
float x = 360;
float y = random(0, 360);
float x2 = 360;
float y2 = random(0, 360);
float x3 = 360;
float y3 = random(0, 255);
PFont f;
int score = 0;
boolean firstTime=true;
void setup() {
  size(400, 400);
  background(255);
  f = createFont("Times New Roman", 25, true);
}
void randomrect() {
  fill(0);
  rect(x, y, 40, 40);
  x = x - 2;
  if (x<-40) {
    x = 360;
    y = random(0, 360);
    score++;
  }
}
void randomrect2() {
  fill(0);
  rect(x2, y2, 40, 40);
  x2 = x2 - 2;
  if (x2<-40) {
    x2 = 360;
    y2 = random(0, 360);
    score++;
  }
}
void randomrect3() {
  fill(0);
  rect(x3, y3, 40, 40);
  x3 = x3 - 2;
  if (x3<-40) {
    x3 = 360;
    y3 = random(0, 360);
    score++;
  }
}
void gameover(float squarex, float squarey) {
  if (((mouseY>squarey) && (mouseY<squarey+40) && (squarex <= 80) && (squarex >= 0)) || ((mouseY+40>squarey) && (mouseY+40<squarey+40) && (squarex <= 80) && (squarex >= 0))) {
    textFont (f, 25);
    fill(255, 0, 0);
    text("GAME OVER", 140, 160);
    noLoop();
  }
}
void draw() {
  println(x, y, x2, y2);
  background(255);
  noStroke();
  fill(200, 0, 0);
  rect(40, mouseY, 40, 40);
  text(" score = "+score, 300, 20);
  if (firstTime==true) {
    firstTime=false;
  } else {
    firstTime=true;
  } 
  gameover(x, y);
  gameover(x2, y2);
  gameover(x3, y3);
  randomrect();
  if (frameCount>60) {
    randomrect2();
  }
  if (frameCount>120) {
    randomrect3();
  }
}

```
