Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
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
add comment

4 Answers

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;
share|improve this answer
3  
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
add comment

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.