# Implementing pausing functionality

I'm working in LWJGL and trying to implement pausing when the user presses the 'P' key. Thus far I have this:

boolean gamePaused = false;
while(!Display.isCloseRequested()){

if(Keyboard.isKeyDown(Keyboard.KEY_P) && !gamePaused)
gamePaused = true;
if(Keyboard.isKeyDown(Keyboard.KEY_P) && gamePaused)
gamePaused = false;

if(gamePaused){
while(true){
System.out.println("Game paused.");
if(Keyboard.isKeyDown(Keyboard.KEY_P)){
gamePaused = false;
break;
}
}
}
//...game logic
}


My problem is that it continues playing even when I press the P key.

• Try adding a timer on the key press so it can't switch between the states so quickly. – Milkboat Mar 8 '12 at 1:33

Look at your if statements. You set paused to true, then the next statement evaluates true (because there's no way to not press p for a short enough time and you just set paused to true), and it sets it to false. Try:

if(Keyboard.isKeyDown(Keyboard.KEY_P) && !gamePaused) {
gamePaused = true;
} else if(Keyboard.isKeyDown(Keyboard.KEY_P) && gamePaused) {
gamePaused = false;
}


EDIT I like Gustavo's suggestion, it's much cleaner and is actually what I do with my booleans:

if(Keyboard.isKeyDown(Keyboard.KEY_P))
gamePaused = !gamePaused;

• Yeah, and also, try to find a way to put these keys strokes as events, not a buffered way. Or just put a timer. And this should work better: if(Keyboard.isKeyDown(Keyboard.KEY_P)) gamePaused = !gamePaused; – Gustavo Maciel Mar 8 '12 at 5:58
• Also do not forget to only disable the logic-code when the game is paused. The rendering and parsing of input should still work (for a RTS f.e., you can pause the game, give commands, and let it run all at once...which is a very nice feature). – Bobby Mar 8 '12 at 8:39

The point made by Byte56 is true (you need an else if or this won't work), but you should also check that the key was not down last frame before doing anything with it. So something like:

if(Keyboard.isKeyDown(Keyboard.KEY_P) && pWasNotDown) {
gamePaused = true;
}


Your inner loop is confusing and makes it all more complicated, too. You should really just say

if(!gamePaused) {
// game logic
}


If the game is paused, the code will just skip over that part to the end of the big while loop. At the end of that loop, set

pWasNotDown = Keyboard.isKeyDown(Keyboard.KEY_P)


You'll come back to the beginning of your while loop, and then the variable will be properly set to ignore isKeyDown the next frame.

This is a problem that comes up with input a lot. There are tons of input events that you only want to occur once per button/key press, and for each of those you'll have to do logic like this. Ideally, you would extract this all into an inputmanager so you can say InputManager.isKeyDown(KEY_P) && !InputManager.wasKeyDown(KEY_P) (or better yet, InputManager.wasJustPressed(KEY_P)). But that's outside the scope of this question, and just something to think about as you continue developing your game.

• This is basically the answer, but to refine it slightly, LWJGL has an keyboard event buffer (meaning you don't have to do the management yourself as Tristan suggests). Please see lwjgl.org/wiki/index.php?title=LWJGL_Basics_2_(Input) under the title "The Event Buffer". – PlayDeezGames Mar 8 '12 at 15:02

I'd like to talk about an alternative that doesn't seem applicable from your example, but is actually very common and should be worth sharing - It's how I have always implement pausing in my games.

Have you ever used a stack based game state management system such as this to divide your game into states or screens? Besides being extremely useful at organizing and transitioning between multiple states (e.g. title screen, options screen, game screen, scores screen) it also makes pausing your game extremely easy - just push a new pause state on top of your current one.

For instance, here's what such a state could look like (in pseudocode):

class PauseState : GameState
{
void Update()
{
if(OnKeyDown("P")) PopState();
}

void void Draw()
{
// Draw fullscreen black quad at 50% opacity
// Draw "Pause" message in the middle of the screen
}
}


And afterwards you could pause your game from any state just by doing:

if(OnKeyDown("P")) PushState(new PauseState());


Just set up your state manager so that it calls Draw on all states on the stack, but only calls Update on the topmost state. That's the most important bit.

I managed to implement a pause funcionality that works perfectly well in my game, based on this tutorial:

1. Create a volatile boolean variable in StartingClass

private volatile boolean isPaused = false;

2. Inside the run() method, use wait() surrounded by a try-catch.

// GAME IS PAUSED

try {
if (isPaused) {
synchronized (this) {
while (isPaused) {
wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}

3. Outside run(), create a new method resumeGame()

public synchronized void resumeGame() {
isPaused = false;
notify();
}

4. Inside Keypressed, define the key to pause and resume. (the p-key in this example).

case KeyEvent.VK_P:

if (isPaused == false) {
isPaused = true;
} else {
resumeGame();
}
break;


use isKeyPressed instead, that way, holding down the "p" button for a very small time won't re-trigger the if statement.

I'm not quite sure how to do this in standard Java, but in LWJGL I made an extremely simple method to check if the game is paused or not. Also, instead of having the gamePaused as a boolean it could be an enum.

 while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
switch(Keyboard.getEventKey()){

case Keyboard.KEY_P:
gamePaused = !gamePaused;
System.out.println(gamePaused);
}
}


I suggest looking up some ways people implemented gamestates into their games, so that way you can have a title screen and a menu (pause screen).

tHere's how I did it:

while (true) {
if (gamePause == false) {
// Game logic here...
}
}


edit: I believe your problem is caused by pause check. You can use this to toggle it false or true:

gamePause = !gamePause;


I use key listener and have no experience with the command you are using for keys but i can say that you don't need to check for the value of the gamePause. Just toggle the gamePause whenever the p key is pressed.

edit 2: You first check for the pause then start the loop. Once the loop starts there is no way for it to check it again. My suggestion is to put the boolean check (if gamePause == false) inside of the loop.

• Adding a bit more context to this answer would be useful. Explaining why this works, where it goes and so on. – MichaelHouse Apr 28 '14 at 14:03
• How are you pausing the game when the P key is pressed without checking for whether the P key is pressed? Magic! – Anko Apr 28 '14 at 15:29
• I believe the problem is in the logic checking the gamePause boolean. I've edited it to clarify. – WVrock May 6 '14 at 4:35