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.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);
// 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);
//bg = loadImage("bg.png");
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.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);
// 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.