# Need Help for Game Over / Timer

Hello, i’m really new to Processing, i need to make a game over when the balls touch the top or the bottom edge of the screen, another thing to stop the timer when it’s a game over.

My code is really bad ( sorry, it’s only my third time ) :

``````PVector location;  // Location of shape
PVector velocity;  // Velocity of shape
PVector gravity;   // Gravity acts at the shape's acceleration
PImage bg;
int start;

int value = 0;

void setup() {
size(640,360);
location = new PVector(100,100);
velocity = new PVector(1.5,2.1);
gravity = new PVector(0,0.2);
start = millis();

}

void draw() {
background(bg);
stroke(255);
strokeWeight(2);
fill(#E5EED3);
ellipse(location.x,location.y,48,48);
int timer = millis()-start;
text("score : "+timer, 550, 30);

// Add velocity to the location.

// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) {
textAlign(CENTER);
text("\n\n\nGAME OVER\nVotre score : "+timer, 320, 180);
fill(#E5EED3);
textAlign(CENTER);

}

}

void mouseClicked() {
if (value == 0) {
velocity.y = velocity.y * -1.005;
velocity.x = velocity.x * 1.01;
} else {
velocity.y = velocity.y * 1;
}
}
``````

Neat sketch so far. What you need to add to it is the concept of having some sort of state.

Normally, when the player is playing the game, your sketch will be in the “playing the game” state. Let’s call this state #0.

When the player loses, then the game needs to go into a different state. The “you lost” state, or state #1.

What we need is one variable to track which state the game is currently in, and that way we can do different things depending on if the game is currently being played (state #0) or if the game has been lost (state #1).

So, let’s add a variable that tracks the current state:

``````int state;
``````

Now, which state do we start in? The game playing state. So when the sketch first runs, we want to be in state #0. So we know that we want to set the state to 0 in `setup()`:

``````void setup(){
size(640, 360);
state = 0;
// ...
}
``````

Now we want to do different things in draw depending on what the state is. If the state is 0, we want to do the game playing stuff. If the state is 1, we want to display the game over screen:

``````void draw(){
if( state == 0 ){
draw_playing();
} else if( state == 1 ){
draw_gameover();
}
}
``````

Most of what you had before in `draw()` is the stuff that normally happens when we are playing the game. This can now go in `draw_playing()`:

``````void draw_playing(){
background(0);//bg);
stroke(255);
strokeWeight(2);
fill(#E5EED3);
ellipse(location.x, location.y, 48, 48);
int timer = millis()-start;
text("score : "+timer, 550, 30);

// Add velocity to the location.

// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
}
``````

But wait, look at this last conditional block that was in draw() before:

``````  if (location.y > height) {
textAlign(CENTER);
text("\n\n\nGAME OVER\nVotre score : "+timer, 320, 180);
fill(#E5EED3);
textAlign(CENTER);
}
``````

This is a check to see if the game is over! This is a good check, but if the game is over, we want to move into state #1 - the game over state - and then draw the game over screen. here’s the full thing:

``````PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape’s acceleration
PImage bg;
int start;

int value = 0;

int state;
int timer;

void setup() {
size(640, 360);
location = new PVector(100, 100);
velocity = new PVector(1.5, 2.1);
gravity = new PVector(0, 0.2);
start = millis();
state = 0;
}

void draw() {
if ( state == 0 ) {
draw_playing();
} else if ( state == 1 ) {
draw_gameover();
}
}

void draw_playing() {
background(0);//bg);
stroke(255);
strokeWeight(2);
fill(#E5EED3);
ellipse(location.x, location.y, 48, 48);
timer = millis()-start;
text("score : "+timer, 550, 30);

// Add velocity to the location.

// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) { // fell off the bottom.
state = 1; // Go to game over state.
}
// What about a check to see if you hit the top?
// What would you do in that case?
}

void draw_gameover() {
background(128,0,0);
textAlign(CENTER);
text("\n\n\nGAME OVER\nVotre score : "+timer, 320, 180);
fill(#E5EED3);
textAlign(CENTER);
}

void mouseClicked() {
if (value == 0) {
velocity.y = velocity.y * -1.005;
velocity.x = velocity.x * 1.01;
} else {
velocity.y = velocity.y * 1;
}
}
``````

Notice that I made `timer` a global variable, and removed your background image (because i don’t have it). I also added a background to the game over state that is red so you notice it.

1 Like

Wow you are insane thanks you so much, for the timer, it’s now a global variable and i don’t know how to print in in the gamer over, cause when it switch state, the timer seems to back to 0, maybe i will try with another method to count second or count the number of click, for the top of the screen, i tried to make the same with :

``````
if (location.y < height) {
state = 1;

if (location.y > 340) // ( height of the screen )
state = 1
``````

but it doesn’t seems to work. Thank you for your fast answer.

Wait works with :

``````   if (location.y < 0) {
state = 1;
}
``````

Make sure you are pairing up your curly brackets!

``````if (location.y < height) {
state = 1;
if (location.y > 340) // ( height of the screen )
state = 1
// missing closing curly bracket! ('}')
``````

You probably mean this:

``````if (location.y < height) {
state = 1;
}

if (location.y > 340) { // ( height of the screen )
state = 1
}
``````

… Or… do you? make sure these conditions are correct!
(Hint: What is the value of locations.y that puts the ball above the top of the screen?)

This one works :

``````  // Rebond dans les coins
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}

if (location.y < 0) {
state = 1;
}

if (location.y > height) { // Il tombe en dessous
state = 1; // On va au stade numéro 2 (Game Over)
}
}
``````

Did you know anothe way to make a timer and print it in the Game Over screen ? Milis seems to be a bit to complex for me.

Also, you are already displaying the timer in the game over screen. It’s this line:

``````text("\n\n\nGAME OVER\nVotre score : " + timer, 320, 180);
``````

Using `millis()` is a little tricky, but it’s the best way to time things IMHO. If you want to convert to seconds, just divide by 1000…

``int time_seconds = millis() / 1000;``

Ok, the timer work when the game is playing, but always 0 when he is on the game over screen, must I print the value of the timer when the state switch to 1 and just do something like :

``````  textAlign(CENTER);
text("\n\n\nGAME OVER\nVotre score : (command to read the value ), 320, 180);
fill(#E5EED3);
``````

I found the solution, i just put the variable two time ( in draw and draw_playing), thanks now the program works very well :

``````PVector location;  // Location of shape
PVector velocity;  // Velocity of shape
PVector gravity;   // Gravity acts at the shape's acceleration
PImage bg;
int start;
int state;
int timer;
int time_seconds = timer / 1000;
String v = "value";

int value = 0;

void setup() {
size(640,360);
state = 0;
location = new PVector(100,100);
velocity = new PVector(1.5,2.1);
gravity = new PVector(0,0.2);
start = millis();

}

void draw() {
if( state == 0 ){
draw_playing();
} else if( state == 1 ){
draw_gameover();
}
}

void draw_playing(){
background(bg);
stroke(255);
strokeWeight(2);
fill(#E5EED3);
ellipse(location.x, location.y, 48, 48);
timer = millis()-start;
text("score : "+timer, 550, 30);

// Ajout de vitesse
// Ajout de gravité

// Rebond dans les coins
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}

if (location.y < 0) {
// printIn(millis()-start);
state = 1;
}

if (location.y > height) { // Il tombe en dessous
state = 1; // On va au stade numéro 2 (Game Over)
}
}

void draw_gameover() {
background(128,0,0);
textAlign(CENTER);
text("\n\n\nGAME OVER\nVotre score : " +timer, 320, 180);
fill(#E5EED3);
textAlign(CENTER);
}

void mouseClicked() {
if (value == 0) {
velocity.y = velocity.y * -1.005;
velocity.x = velocity.x * 1.07;
} else {
velocity.y = velocity.y * 1;
}
}
``````