Jump to content
  • Advertisement
Sign in to follow this  
GuitarPlayer0912

Pausing games using keyboard input

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just recently developed a small game and am working on the second edition of it. I can't seem to figure out how to pause the game though or if I can, I can't get it to resume. If anyone has any ideas for how to pause and unpause a game, reply to this. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Generally how I do this is the entire game is driven by an Update function that takes in an elapsed time. If I want to pause the game, change the game speed, etc I just modify the elapsed time that I pass to the update function. This is a simple architecture that makes these kinds of changes easy to make.

Share this post


Link to post
Share on other sites
Quote:
Original post by GuitarPlayer0912
thanks guys, I got it to work using pg up and pg down. But for some reason, ALT works when I didn't program it to, does anybody know why?


can you show us your code to recieve keyboard input?

Share this post


Link to post
Share on other sites
void HandleKeys()
{
if (!_bGameOver && !_bSplash)
{
// Move the tank based upon left/right key presses
POINT ptVelocity = _pTankSprite->GetVelocity();
if (GetAsyncKeyState(VK_LEFT) < 0)
{
// Move left
ptVelocity.x = max(-_iMAXSPEED - 2, -6);
_pTankSprite->SetVelocity(ptVelocity);
}
else if (GetAsyncKeyState(VK_RIGHT) < 0)
{
// Move right
ptVelocity.x = min(_iMAXSPEED + 2, 6);
_pTankSprite->SetVelocity(ptVelocity);
}
if (GetAsyncKeyState(VK_DOWN) < 0)
{
// Stop
ptVelocity.x = (0, 0);
_pTankSprite->SetVelocity(ptVelocity);
}

if (_bAmmoLeft)
// Fire missles
if ((++_iFireInputDelay > 6) && GetAsyncKeyState(VK_SPACE) < 0)
{
if (_iScore >= 0)
{
// Create a new missile sprite
RECT rcBounds = { 0, 0, 850, 600 };
RECT rcPos = _pTankSprite->GetPosition();
Sprite* pSprite = new Sprite(_pHumanMissle, rcBounds, BA_DIE);
pSprite->SetPosition(rcPos.left + 29, 510);
pSprite->SetVelocity(0, -10);
_pGame->AddSprite(pSprite);
_iAmmo -= 1;

// Play the missile (fire) sound
PlaySound((LPCSTR)IDW_MISSLE, _hInstance, SND_ASYNC |
SND_RESOURCE);
}
if (_iScore >= 2000)
{
RECT rcBounds = { 0, 0, 850, 600 };
RECT rcPos = _pTankSprite->GetPosition();
Sprite* pSprite = new Sprite(_pMortarLeft, rcBounds, BA_DIE);
pSprite->SetVelocity(-5, -10);
_pGame->AddSprite(pSprite);
_iAmmo -= 1;
}
if (_iScore >= 1500)
{
RECT rcBounds = { 0, 0, 850, 600 };
RECT rcPos = _pTankSprite->GetPosition();
Sprite* pSprite = new Sprite(_pMortarRight, rcBounds, BA_DIE);
pSprite->SetVelocity(5, -10);
_pGame->AddSprite(pSprite);
_iAmmo -= 1;
}
if (_iScore >= 3500)
{
RECT rcBounds = { 0, 0, 850, 600 };
RECT rcPos = _pTankSprite->GetPosition();
Sprite* pSprite = new Sprite(_pHumanLaserL, rcBounds, BA_DIE);
pSprite->SetPosition(rcPos.left + 15, 529);
pSprite->SetVelocity(0, -10);
_pGame->AddSprite(pSprite);

// Play the laser sound
PlaySound((LPCSTR)IDW_LASER, _hInstance, SND_ASYNC |
SND_RESOURCE);
}
if (_iScore >= 3500)
{
RECT rcBounds = { 0, 0, 850, 600 };
RECT rcPos = _pTankSprite->GetPosition();
Sprite* pSprite = new Sprite(_pHumanLaserL, rcBounds, BA_DIE);
pSprite->SetPosition(rcPos.left + 51, 529);
pSprite->SetVelocity(0, -10);
_pGame->AddSprite(pSprite);

// Play the laser sound
PlaySound((LPCSTR)IDW_LASER, _hInstance, SND_ASYNC |
SND_RESOURCE);
}

// Reset the input delay
_iFireInputDelay = 0;

if (_iAmmo == 0)
{
_bAmmoLeft = FALSE;
}
}
}
if (GetAsyncKeyState(VK_ESCAPE) < 0)
{
PostQuitMessage(0);
}
if (GetAsyncKeyState(VK_NEXT) < 0)
{
_bPaused = TRUE;
_pGame->PauseMIDISong();
}
else if (GetAsyncKeyState(VK_PRIOR) < 0)
{
_bPaused = FALSE;
_pGame->PlayMIDISong(TEXT(""), FALSE);
}

if (GetAsyncKeyState(VK_0) < 0)
{
_pGame->CloseMIDIPlayer();
}
else if (GetAsyncKeyState(VK_1) < 0)
{
_pGame->CloseMIDIPlayer();
_pGame->PlayMIDISong(TEXT("feelthebeat_darude_nightbird.mid"), TRUE);
}
else if (GetAsyncKeyState(VK_2) < 0)
{
_pGame->CloseMIDIPlayer();
_pGame->PlayMIDISong(TEXT("darude_sandstorm.mid"), TRUE);
}

// Start a new game based upon an Enter (Return) key press
if (GetAsyncKeyState(VK_RETURN) < 0)
if (_bSplash)
{
_bSplash = FALSE;
if (!_bHelpScreen)
{
NewGame();
}
}
else if (_bGameOver)
{
NewGame();
}
else if (_bHelpScreen)
{
_bHelpScreen = FALSE;
_bSplash = FALSE;
NewGame();
}

if (_bSplash && GetAsyncKeyState(VK_HOME) < 0)
{
_bHelpScreen = TRUE;
}
else
{
_bHelpScreen = FALSE;
}
}

that's my code for keyboard input. Don't worry about the _b...whatever, those are just boolean values to tell if the game is over, splash screen is shown, etc.

Share this post


Link to post
Share on other sites
Some of the keys you have #defined or made constants or whatever may not have the correct values. Check to make sure that the alt key doesn't have the same value as page up or page down. Assuming you have defined them yourself, that is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!