I would appreciate it if someone could help me with my problem, I am trying to collide my character with the vehicle and then to display a “Game over” screen once it collides. The issue that i am facing is that when it collides the “game over” screen doesn’t remains on the screen but vanishes after a second or so. I have attached the specific part of my program.
Thanks
Not enough context.
It is safer to change a boolean flag gameOver in this if-clause to true.
And in draw() evaluate your flag. Like showing the game over screen when it’s true, OTHERWISE the game (what you have in draw() now presumably)
So do I need to declare a boolean globally and then use it in draw() function? Because at the moment I didn’t use any boolean, if so could you please tell me how to specifically.
Thanks
The draw function loops 60 times per second and overwrites what you did draw previously, the Game over as well.
You could add a noLoop() after the Game Over text, which stops the program and therefore you see the text. If you use your colliding variable and create an if condition at the beginning of draw, which automatically draws the game over screen, if true, then you avoid drawing everything else as well.
Alternatively, I use switch to use different screens. See the example below from a game I made. It is not the full code, just to see, how it works. The screen variable is set 0 at the beginnning to get to the startscreen and later changes based on keys pressed.
switch(screen){
case 0:
startscreen();
break;
case 1:
playscreen();
break;
case 2:
endscreen();
break;
}
if ((key == 'P' || key == 'p') && screen == 0) {
screen = 1;
}
if ((keyCode == RETURN || keyCode == ENTER) && screen == 1) {
if (rounds <= maxround){
endofround();
} else {
screen = 2;
}
This if clause belongs into your draw() function not in the collision function
I tried doing that as well, could you please show me using my example, would highly appreciate it.
Thanks
In draw((
if(colliding ==false) {
your current draw()
}
else {
show game over
}
end of function draw()
Just be aware, that your collision detection might need some work as well. When I did run your code with the updated loop, it immediately switched to the game over screen without me being able to do anything.
Could you please try it and show me how to do it, please I really need to learn this. Would highly appreciate it.
Thanks
show your attempt please
It shows you immediately game over, because your check for collision check is not correct.
What your if function is doing:
If (coordinates within rectangle) { do nothing (because of empty brackets)} else { colliding= true}.
So your function will always switches to collision, when you are not in the rectangle.
The last operation in the if condition is not correct as well.
My advice:
-
collision() function as you had before, but just set the flag
colliding
there. Call collision() function from draw(). -
in draw() evaluate the flag
colliding
and either Play Game (what you had in draw() previously) OR show game over screen
The flag colliding
is a global boolean like
// FLAG
boolean colliding = false;
My apologies if my initial post was not clear. I was too brief.
Warm regards,
Chrisir
In theory, you want to reset the game when you are in game over screen.
So also in keyPressed you can make the distinction with
if(colliding == false) {
to distinguish between Play Screen and Game over Screen.
In the game section keep what you have in keyPressed () now,
in the game over section say colliding = false; and reset the player data etc. (like you have in setup() now)
Remark
you can also consider to let the player start with 3 lives, upon collision do a small reset and say lives--;
and when you are at lives <= 0
the game over screen appears (by saying colliding = true;
).
- collision() function as you had before, but just set the flag
colliding
there. Call collision() function from draw().
This will not work. If you look at the collision function (see below), it will switch to true, when both rectangles are not overlapping. Therefore, he gets a game over with the first frame, when moving the flag evaluation to draw, as he stated. Evaluation should be moved to draw but collision has to be corrected to make it work.
void collision() {
if(colliding ==false) {
if(p1 > x3 && p1 < x3 + x4 && p2 > y3 && p2 < y3 + y3) {
}
else {
colliding = true;
background(0);
fill(255,0,0);
textSize(50);
text(“Game Over”, 50, 100);
}
}
}
It’s running fine here
void collision() {
if(p1 > x3 && p1 < x3 + x4 && p2 > y3 && p2 < y3 + y3) {
colliding = true;
}
}
Remark
not sure about the last part
p2 < y3 + y3
could be
p2 < y3 + x4
or something.
(we should work on the naming, like playerX, carX, playerWidth, carWidth…; there is a rename command in the context menu, when you right click on p2
for example… )
This is my next step, will implement this and hopefully will you how i go.
Thanks
Yes. This will work. I was still referencing the original code.