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

6 Answers 6

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

share|improve this answer

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.

share|improve this answer
4  
Adding a bit more context to this answer would be useful. Explaining why this works, where it goes and so on. –  Byte56 Apr 28 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 at 15:29
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Vaughan Hilts Apr 29 at 18:24
    
I believe the problem is in the logic checking the gamePause boolean. I've edited it to clarify. –  WVrock May 6 at 4:35

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

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

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

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

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.