简体   繁体   中英

Why isn't my game pausing? (Windows C++ Keyboard Input Statemachine)

I'm writing a game in C++ using the Windows API which has a Splash Screen at the start, before gameplay begins, and can be paused.

I store the state of the game in an enum, game_state {PAUSED, PLAYING, SPLASHSCREEN}, and rely on Keyboard input to control the game.

The game was working properly, switching between paused and playing, but when I tried to add a splashscreen to begin the game on, the pause functionality stopped working, and I'm not sure why...

if(Keyboard.GetKey(VK_RETURN) && game_state == SPLASHSCREEN)  
{                                                               
    game_state = PLAYING;                                       
    Keyboard.SetKey(VK_RETURN, false);                          
}                                                             


if(Keyboard.GetKey(VK_RETURN))               
{                                               
    if(game_state == PAUSED)                    
    {                                           
        game_state = PLAYING;               
    }                                           
    else                                        
    {                                           
        game_state = PAUSED;                    
    }                                           
    Keyboard.SetKey(VK_RETURN, false);          
}                                            

//If Paused, go to Pause Screen 
if(game_state == PAUSED)
{
    pauseScreen();
}

//If Splash Screen, go to Splash Screen 
if(game_state == SPLASHSCREEN)
{
    splashScreen(); 
}

//If not paused, do game processing
if(game_state == PLAYING)
{
    gamePlay();
}

GetKey() returns true if the key is held down. game_state is an enum global containing the current state of the game. SetKey() sets the specified key as down (true) or up (false)

Oh, and all splashScreen() pauseScreen() and gamePlay() do are display sprites representing each state (at the moment)

SetKey

void Keyboard::SetKey(WPARAM key, bool key_down)
{
    if(key_down)
    {
        m_keys[key] = true;
    }
    else
    {
        m_keys[key] = false;
    }
}

GetKey

bool Keyboard::GetKey(WPARAM key)
{
    if(m_keys[key])
    {
        m_keys[key] = false;
        return true;
    }
    else
    {
        return false;
    }
}

Remove m_keys[key] = false; from the Keyboard::GetKey method. As it is being set to false in the first check, it prevents the next check from seeing that it was pressed.

Calling GetKey() sets the key as released - since it checks to see if the key is pressed and the state is splashscreen before checking anything else - the key will always be released when checking it again.

Alter GetKey or alter the way the code is written.

My best guess is that your splash screen has the focus and it will take over the message loop, then you don't get the key event. Just a guess, can't really know without seeing the window creation/registration code of your splash and main windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM