Sign in to follow this  
PaulJSimmons

Pausing my Game

Recommended Posts

Hi,ive created a 2d space shooter, i have a few things left to add to my game one being a pause.

I have added the following code which seems to pause the screen, but the game still runs, i.e. when i unpause the enemies have been moving. I believe this to be because the enemies movements are calculated by gametime. Any suggestions as to what I need to add to fix this problem.
[code]

void Pause()
{
while (1)
{
for (int i = 0; i < 255; i++)
{
if (GetAsyncKeyState(i))
return;
Paused = false;
}
}
}

ErrorType GameMain()
// Called repeatedly - the game loop
{
if(pTheInputs->KeyPressed(DIK_L))
{
Paused = true;
Pause();
}

gTheGameTimer.mark();
// validation routine - should be able to alt-tab out of program
// (Done in game main just to show that it needs to be done - better to do in MyDrawEngine. really)
if(pTheDrawEngine->Validate()==FAILURE) return FAILURE;

RECT phelirect = phelicopter->GetCollRect();
if( pEnemies->CheckCollision(phelirect)==true)
{
phelicopter->HandleCollision();
}

RECT pbullrect = pBullets->GetRectForBullet(10);
if(pEnemies->CheckCollision(pbullrect)==true)
{
pBullets->HandleCollision();
}

if (Paused == false)
{
pStars->Draw();
pNebSpace->DrawNeb();
phelicopter->Draw();
phelicopter->Move();
pEnemies->Draw();
pEnemies->Move();
pExplosion->Draw();
pExplosion->Move();
pBullets->Draw();
pBullets->Move();
pTheDrawEngine->Flip();
pTheDrawEngine->ClearBackBuffer();
}
return SUCCESS;
}

[/code]

Share this post


Link to post
Share on other sites
The for loop with 255 for get asynckeystate is pointless, just keep the if in the while(1) and it will be fine, no need to loop 255 times as the while loop will do it already.
But, to fix it the easy way is to make your gameloop a switch with 2 sections, one section for when its paused and the other updates the rest of the game. This way you know for certain when the game is updated and when the pause state is updated.

Share this post


Link to post
Share on other sites
Implementing pause with a while(1) is a very bad idea, even if it works its ugly as hell, if you ask me there should never in your code be a while(constant value) its just bad.

what you should do is simply not update logic and animation if the pause flag is on, rendering should still go on, some pause screens are animated to avoid monitor burn, the important thing is that you not process anything other than the key that disables the pause, If I'm not mistaken you are still performing some collision checking while in pause, its pointless, and the game time mark, does that send time messages to your game's entities? that would be the root of the problem.

Share this post


Link to post
Share on other sites
Yes Nexuskill the game timer does send messages to allow my entities to move at the same speed on different machines, the whole while business was me trying to stop it from blitting the objects, i'm fairly new to C++ so i was just trying different things that came to mind.


Do you have any ideas how i can stop them moving because although the game is paused the game timer is still ticking over which is how my enemy objects are still moving.( well this is what i assume anyways)

Share this post


Link to post
Share on other sites
Well, the game timer has the responsibility of processing passage of time, even when paused, it should still do that, specially if we consider an animated pause screen. But you are exceeding its responsibilities by having it propagate the resulting time delta, you should do that in a game logic implementation that allows you to control who you inform about time and when, thus allowing you to not update entities while in pause

You should have a Game class that has an update/draw loop, in the update you first process time, then you take the results of that process and send it only to the entities that should know about it in the current application context

Share this post


Link to post
Share on other sites
What I do is simply have an isPaused boolean. Then, all game updates and rendering are inside of an if statement, So:

[code]
if ( !isPaused ) {

updateEntities();
renderEntities();
}
[/code]

Then your state machine can just control that boolean. So when the pause state is entered, isPaused is set true, and then no entities will update. remember though you dont want ALL game logic to not respond, only gameplay. So you shouldnt have stuff like input from the mouse and keyboard depend on this, since you need to be able to get out of the paused state. Simple and works well for me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this