Sign in to follow this  
raptorstrike

game over sequence

Recommended Posts

ok i want my program to display a game over message and pause until the F2(new game) key is pressed but when the statement is fuffiled nothing happens it just keeps going on its way heres what i got (i dont really need anyone critiquing my code unless it pertains to my problem i cause this code hasnt been edited or optimized or whatever you want to call it) thx. [smile]
#include "breakout.h"
#include <string>
    

BOOL GameInitialize(HINSTANCE hInstance)
{
    _pGame = new GameEngine(hInstance , TEXT("breakout"), TEXT("breakout"), IDI_UFO, IDI_UFO_SM,500, 650);
        if (_pGame == NULL)
        {
            return FALSE;
        }; 
    _pGame->SetFrameRate(40);
    
        _hInstance =hInstance;
        return TRUE;
}
void GameStart(HWND hWindow)
{   RECT BallBounds =  { 0 , 0 , _pGame->GetWidth() , _pGame->GetHeight()};
    Offscreenbitmap = CreateCompatibleBitmap(GetDC(hWindow),_pGame->GetWidth(), _pGame->GetHeight());
    OffscreenDC = CreateCompatibleDC(GetDC(hWindow));
    RECT BlockBounds = { 0,0,_pGame->GetWidth(), _pGame->GetHeight()};
    for(int j = 0; j<30; j++)
    {
        blockbits[j] = new Bitmap(hDC, IDB_BLOCK , _hInstance);
    };    
    for(int i = 0; i<30; i++)
    {
        blocks[i] = new Sprite(blockbits[i] , BlockBounds , SA_NONE);
        _pGame->AddSprite(blocks[i]);    
    };
    for (int l = 0; l <30; l++)
    { 
        blocks[l]->SetPosition(rand()%500, rand()%450);
        for(int s=0; s < 30; s++)
          if( blocks[l]->TestCollision(blocks[s]) == TRUE)
          {
              if (blocks[s] != blocks[l])
               blocks[l]->SetPosition(rand()%500, rand()%450);
          };
                      
    };    
        
    RECT PaddleBounds = { 0 , 0 , _pGame->GetWidth() , _pGame->GetHeight()};
    srand(GetTickCount());
    HDC hDC = GetDC(hWindow);
    BackGround = new Bitmap(hDC, IDB_BACKGROUND , _hInstance);
    ballbit = new Bitmap(hDC , IDB_BALL , _hInstance);
    paddlebit = new Bitmap(hDC , IDB_PADDLE , _hInstance);
    ball = new Sprite( ballbit , BallBounds , BA_BOUNCE);
    paddle = new Sprite (paddlebit , PaddleBounds , BA_WRAP);
    ball->SetVelocity(0,5);
    paddle->SetVelocity(PaddleSpeed,0);
    paddle->SetPosition(250,600);
    ball->SetPosition(250,400);
    _pGame->AddSprite(ball);
    _pGame->AddSprite(paddle);
    
}  
void GameEnd()
{
    _pGame->CleanupSprites();
    //_pGame->CloseMIDIPlayer();
    delete _pGame;
}
void GameActivate (HWND hWindow)
{
    
}   
void GameDeactivate  (HWND hWindow)
{
    
}  
void GamePaint( HDC hDC)
{
   // TCHAR szGameover[64];
      
    TCHAR szText[64];
    RECT rect = { 600, 10, 301 };
    wsprintf(szText, "%d", score);
    BackGround->Draw(hDC, 0 , 0 , TRUE);
    _pGame->DrawSprites(hDC);
   switch (ballnum)
    {
        case 0:
            {
                DrawText(hDC, gameover, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 

                  totalblocks = 0; 
                    ball->SetPosition(250,400);
                    score = 0 ;
                    //ball->SetVelocity(0,5);
                    pause = true;
                    _pGame->SetSleep(pause);
                    if (GetAsyncKeyState(VK_F2) < 0)
                    pause = false;   
            };
    };        
    switch (totalblocks)
    {
        case  0: 
        {
        
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
		bool IsNotSet=true;                
		while (1)
		{
			bool Failed=false;
	        	for(int s=0; s < 30; s++)
	                {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              	if (blocks[s] != blocks[l])
                              	{
                               	blocks[l]->SetPosition(rand()%500, rand()%450);
				                               	Failed=true;
                                };   	
                             }
                        }
			if (!Failed) break;
		}
             }      
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }
             
                      
       }
       totalblocks=29; 
  
   case -1:
        {
        
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
		bool IsNotSet=true;                
		while (1)
		{
			bool Failed=false;
	        	for(int s=0; s < 30; s++)
	                {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              	if (blocks[s] != blocks[l])
                              	{
                               	blocks[l]->SetPosition(rand()%500, rand()%450);
				                               	Failed=true;
                                };   	
                             }
                        }
			if (!Failed) break;
		}
             }      
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }
             
                      
       }
       totalblocks=29; 
   }  

    wsprintf(szText, "%d", score);
     DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
   
  
}
void GameCycle ()
{
    if (ballnum == 0)
    GAMEOVER = 1; 
    if(ballposition < 0 +12)
    {
        direct = down;
    };    
        
          
    if(GetAsyncKeyState(VK_F2) < 0 )
    {
    totalblocks = 0; 
    ball->SetPosition(250,400);
    score = 0 ;
    ball->SetVelocity(0,5);
    ballnum = 3;
    };    
     InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
    _pGame->UpdateSprites();
    HWND hWIndow =_pGame->GetWindow();
    HDC hDC = GetDC(hWindow);
    GamePaint(OffscreenDC);
    BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame-> GetHeight(), OffscreenDC, 0, 0, SRCCOPY);
    ReleaseDC(hWindow, hDC);
    if(PaddleSpeed != 0 && PaddleSpeed < 0)
    PaddleSpeed++;
    if(PaddleSpeed != 0 && PaddleSpeed > 0)
    PaddleSpeed--;
    paddle->SetVelocity(PaddleSpeed,0);
    ballposition +=direct;
    if(ballposition > 650 - 12)
    {
    ball->SetPosition(250,400);
    ballposition = 400;
    direct = down;
    ball->SetVelocity(0,5);
    }; 
    if (pause == true)
    {
        _pGame->SetSleep(pause);
    };
    //_pGame->UpdateSprites();     
    
   
}
void HandleKeys()
{
    if(GetAsyncKeyState(VK_LEFT) < 0)
    {
        PaddleSpeed = -7;
        paddle->SetVelocity(PaddleSpeed, 0);
    };  
    if(GetAsyncKeyState(VK_RIGHT) < 0)
    {
        PaddleSpeed = 7;
        paddle->SetVelocity(PaddleSpeed, 0);
    };      
    
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{}
void MouseButtonUp(int x, int y, BOOL bLeft)
{}
void MouseMove(int x, int y)
{} 
BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
{
  if(pSpriteHitter->GetBitmap() == blockbits[1] && pSpriteHittee->GetBitmap() == blockbits[1])
  {
      pSpriteHitter->SetPosition(rand()% 500, rand()%500);
  };    
  if(pSpriteHitter->GetBitmap() == ballbit && pSpriteHittee->GetBitmap() == paddlebit)
  {
      if(PaddleSpeed != 0)
      {
      ball->SetVelocity(-PaddleSpeed,-5);
      direct = up;
      }    
      else ball->SetVelocity(0,-5); 
      direct = up;
  }
  for(int i = 0; i < 30; i++)
  {
      if(pSpriteHitter->GetBitmap() == ballbit && pSpriteHittee->GetBitmap() == blockbits[i] && pSpriteHittee->IsHidden() != TRUE)
      {
      
           ball->SetVelocity(0,5);
           pSpriteHittee->SetHidden(TRUE);
           score += 5;
           totalblocks--;
           direct = down;
      };    
  }; 
  return false;

}              

HEADER
Sprite* ball;
Bitmap* ballbit;
Sprite* paddle;
Bitmap* paddlebit;
GameEngine* _pGame;
HINSTANCE _hInstance;
short int PaddleSpeed;
Bitmap* BackGround;
Sprite * blocks[30];
Bitmap * blockbits[30];
HDC OffscreenDC;
HDC hDC;
HWND hWindow;
HBITMAP Offscreenbitmap;
int score = 0;
short int totalblocks = 29;
 
float down = 5;
int ballposition = 400;
int up = -5;
int ballnum = 3;
int direct = down;
int GAMEOVER = 0;
char gameover[64] = "GAME OVER";
bool pause = false;

Share this post


Link to post
Share on other sites
In my Tetris game I did something like this:

enum GAMESTATE
{
Playing = 0,
BlinkLine = 1
Paused = 2,
GameOver = 3
};


Then, when I reached the point where it went game over, I did this:

m_GameState = GameOver;


For me you had to use the menu before you could restart playing, but in this case, you can do it like this:

switch (m_GameState)
{
case Playing:
NormalChecks();
break;
case Paused:
if (KeyPressed(P))
Pause(FALSE);
break;

case GameOver:
if (KeyPressed(F2))
NewGame();
break;
}


Toolmaker

Share this post


Link to post
Share on other sites
Using switch statements is a way to go, but I personally prefer function pointers to regulate my games. Basically I have a some code like:
-------------------

int (*Game_Stage) ();// <--this is a function pointer
int Game_Init();
int Game_Main();
int Game_Over();
...
int main()
{
Game_Stage = Game_Init;
while(Game_Stage() );//keeps running the current game stage

return 0;
}
...
//its time to go to the game over stage
Game_Stage = Game_Over;//next time we enter main(), Game_Over will be run until the Game_Stage is switched
...
//user press that F2 key, well lets go back to the main game
if( F2 )
Game_Stage = Game_Main;//now, keep running the main game



Function pointers Look complex, but they are actually pretty easy. This way, you don't need to add an extra case to a switch statement for every new stage.

Share this post


Link to post
Share on other sites
Quote:
void GameCycle ()
{
if (ballnum == 0)
GAMEOVER = 1;
if(ballposition < 0 +12)
{
direct = down;
};

and so forth...

There's your problem - even after you set the GAMEOVER = 1 it's still going through all your update procedures.

So even after you lose, its still going. You should put exit the function if the game is no longer going, but still render to the screen. That way, you get a "freeze frame".

And even though you didn't ask for advice... I'd suggest the same thing as Toolmaker - a gamestate flag. It makes life easier.


Mushu - trying to help those he doesn't know, with things he doesn't know.
Why won't he just go away? An question the universe may never have an answer to...

Share this post


Link to post
Share on other sites
sence this is still an active thread and i dont feel like cluttering up the board how come when i call

switch (VIEW)
{
case 1:
{
if (show != false)
{
while(GetAsyncKeyState(VK_ESCAPE) >= 0 && GAMEOVER != TRUE)
{
wsprintf(HISCORE, "%d", hiscore);
DrawText(hDC, HISCORE , -1, &rect5, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
DrawText(hDC, szHighScore, -1, &rect6, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
if(GetAsyncKeyState(VK_SPACE) < 1)
{
show = false;
break;
}
};
}
};
};

it wont show at all but when i take show = false ou of the if state ment then it works i dont get it though because show only = false if i press the space key right or am i going about this wrong

[ i plan on putting flags for the gamestate in my next game but this one is too close to competion to try and rework the entire source]

Share this post


Link to post
Share on other sites
Well, haven´t really used GetAsyncKeyState() rather than recently tested it and for me it looks like the return value will be 0, if the key haven´t been pressed and that would mean that this statement will be always true:

if(GetAsyncKeyState(VK_SPACE) < 1)
{
show = false;
break;
}

and that could be the reason why you never will see the stuff when you have "show = false" line included (in this case, the stuff inside the while-loop will be run only ones).

And I could imagine that the reason why without the line (show = false) you will see the stuff is that the code you showed will be looped through every frame and because you won´t do any changes that would prevent the while loop from working, the code will be ran every frame.

So, possibly by changing the if statement to something like this could lead to better result:

if(GetAsyncKeyState(VK_SPACE) != 0)
{
show = false;
break;
}

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