How do I make my score for the game increase by 1 and have it displayed on the bottom corner?

Hi! I am stuggling with a project I have. I have a game whereby the pedestrian has to pass through lanes without hitting the vehicle and once it does, I need for the scores to go up by 1. I am unsure as to how to do the scores part. Does anyone know how to do that?

It is hard to say without seeing your code

To Do

Before setup ()

int score=0;

When the player crossed the street say score++;

To display it:

fill(255,111,3); // or something
text(score, 20, height-25);

It might be the case that one crossing gives you score 60 or so. In this case you need an if clause where you check the crossing (if (playerY<10) or something) and then you increase the score and reset playerY so that the if clause no longer applies: playerY=height-25; or so

This is my coding for displaying the score:
<
// Display score
textSize(25);
textAlign(RIGHT, BOTTOM);
text(“Score:”, width-35, height-10);

I need to use an if statement for the scores. So far I have thought of this:

  • pedY is the y value of the pedestrian.
  • 0 is the top of my window size

< if (pedY < 0); {
} >

How do I implement what you said into this. Is there any other codes you need to help you understand my situation better?

??

Just follow the steps I gave you

New variable score before setup etc.

It’s a to do list for you

No semicolon here!!!

if (pedY < 0) {  //!!!!!
    score++;
    pedY=height-25;
}

And

text("Score: "+score, 20, height-25);

Thank you so much! Do you know why you put a +score in text? I am trying to understand the code before moving on.

1 Like

The plus in this context is just joining the fixed word Score within " and " with the variable score so you get

Score: 3

for example

oh okay thank you heaps for the help! Really helped me out.

1 Like

also so sorry to trouble you again. I’m having trouble with the resetting. When my pedestrain hits the vehicle, the pedestrian is meant to reset back to its original position. I made a function for this and it works. However my “game over” screen dissapears and it just resets. I want my screen to reset after the “game over” screen appears. I cannot use the delay statement for this as well as it is a text. I am thinking it is the positioning of my reset function which is not allowing my “game over” to show. Do you know what might be the case?

It would be best to set a variable showGameOverScreen
When your player hits the screen

Then in draw() evaluate this variable and either show normal game OR the game over screen.

Then

Next step would be to use a function keyPressed() and inside it if(showGameOverScreen&&key==’ ') reset();

Remark

Remember that draw() runs 60 times per second and a function like game over screen doesn’t wait for something or so. It just runs. Therefore you need a distinction in draw() based on showGameOverScreen variable.

When i set a showGameOverScreen as a char variable (char showGameOverScreen) , how do I include the fill, size and text font into it? because although i created a variable, it is not connected to the actual coding i have for the game over screen which is:

<
fill(255,0,0); //red background
rect (0,0,1200, 400); // red background

// Game over text
fill(0);
textSize(40);
text(“GAME OVER”, 500,200);

I’m sorry I’m a little new at processing which is why it is taking me a while to understand what you are saying.

Okay say before setup ()

boolean showGameOverScreen=false;

Set it to true upon collision

showGameOverScreen=true;

In draw say

if(showGameOverScreen) {
  //Display game over screen 
  
fill(255,0,0); //red background
rect (0,0,1200, 400); // red background

// Game over text
fill(0);
textSize(40);
text(“GAME OVER”, 500,200);


} else {

// Game 
What you have in draw() now. The entire code inside draw()

}

when you say the entire code inside draw do you mean everything. So like lane coding, vehicle drawings and all of that?

1 Like

Yes.

Everything that’s not on the game over screen

so when i do copy all of that, do i leave the two copies or get rid of the one that is there currently and have it all under this code?

You MOVE the existing code in the new section

Just make a copy or
use menu tools | archive

and then try it

okay i will cut and paste into new section.

1 Like

You can just paste the new stuff above it and place
} after it