# Game over screen not staying on the screen

**URL:** <https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 18, 2022, 2:06pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416 "2022-04-18T14:06:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bossinnit](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bossinnit](https://discourse.processing.org/u/Bossinnit)\
**Post date:** [April 18, 2022, 2:06pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/1 "2022-04-18T14:06:00Z")

</div>

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  
 ![image_2022-04-19_000536798](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/c/acf257851d31fc331fc6bcc69068f10ce05a5305.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 18, 2022, 3:05pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/2 "2022-04-18T15:05:49Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Bossinnit](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bossinnit](https://discourse.processing.org/u/Bossinnit)\
**Post date:** [April 18, 2022, 8:44pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/3 "2022-04-18T20:44:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 18, 2022, 8:56pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/4 "2022-04-18T20:56:53Z")

</div>

Hello @Bossinnit,

> [@Bossinnit](#):
>
> I need to declare a boolean globally and then use it in draw()

There is an example of this here:

> **[Keyboard Functions / Examples](https://processing.org/examples/keyboardfunctions.html)**
>
> Click on the window to give it focus and press the letter keys to type colors. The keyboard function keyPressed() is called whenever a key is pressed. keyReleased() is another keyboard function that i…

`:)`

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [April 18, 2022, 9:50pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/6 "2022-04-18T21:50:52Z")

</div>

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.

```auto
  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;
    }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 2:58am UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/7 "2022-04-19T02:58:35Z")

</div>

> [@Chrisir](#):
>
> 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)

This if clause belongs into your draw() function not in the collision function

---

<div class="post-metadata">

**Author:** ![Bossinnit](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bossinnit](https://discourse.processing.org/u/Bossinnit)\
**Post date:** [April 19, 2022, 8:42am UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/8 "2022-04-19T08:42:55Z")

</div>

I tried doing that as well, could you please show me using my example, would highly appreciate it.  
Thanks

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 8:56am UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/9 "2022-04-19T08:56:16Z")

</div>

> [@Bossinnit](#):
>
> if(colliding ==false) {

In draw((

if(colliding ==false) {  
your current draw()  
}  
else {  
show game over  
}

end of function draw()

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [April 19, 2022, 11:38am UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/10 "2022-04-19T11:38:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bossinnit](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bossinnit](https://discourse.processing.org/u/Bossinnit)\
**Post date:** [April 19, 2022, 1:30pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/12 "2022-04-19T13:30:54Z")

</div>

Could you please try it and show me how to do it, please I really need to learn this. Would highly appreciate it.  
Thanks

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 2:20pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/13 "2022-04-19T14:20:46Z")

</div>

show **your** attempt please

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [April 19, 2022, 3:29pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/16 "2022-04-19T15:29:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 3:58pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/17 "2022-04-19T15:58:12Z")

</div>

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

```auto
// FLAG 
boolean colliding = false;

```

My apologies if my initial post was not clear. I was too brief.

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 4:13pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/18 "2022-04-19T16:13:59Z")

</div>

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

```auto
  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;`).

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [April 19, 2022, 5:19pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/19 "2022-04-19T17:19:33Z")

</div>

> - 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.

```auto
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);
}
}
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2022, 5:37pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/20 "2022-04-19T17:37:11Z")

</div>

It’s running fine here

```auto
void collision() {
    if(p1 > x3 && p1 < x3 + x4 && p2 > y3 && p2 < y3 + y3) {
       colliding = true; 
    }
}

```

* * *

**Remark**

not sure about the last part

```auto
p2 < y3 + y3

```

could be

```auto
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… )

---

<div class="post-metadata">

**Author:** ![Bossinnit](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bossinnit](https://discourse.processing.org/u/Bossinnit)\
**Post date:** [April 19, 2022, 7:23pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/22 "2022-04-19T19:23:39Z")

</div>

This is my next step, will implement this and hopefully will you how i go.  
Thanks

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [April 19, 2022, 7:39pm UTC](https://discourse.processing.org/t/game-over-screen-not-staying-on-the-screen/36416/23 "2022-04-19T19:39:16Z")

</div>

Yes. This will work. I was still referencing the original code. 👍
