How to add score and ending to my game

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:

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.

1 Like

Welcome to the forum!

Chrisir

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

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

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

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

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?

Sure I just let you do the first serv

Then I can look at it

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();
}
}

Does it work?

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

Hmm don’t seems work

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

In case no collision occurs:

firstTime=true;