Building the Space Out 2 Game


You now know enough about splash screens to take a stab at adding one to the Space Out game. In doing so, you'll be making it more of a complete game with a professional touch. The new version of the Space Out game with a splash screen is called Space Out 2, and it is the focus of the remainder of this hour . Space Out 2 is very similar to the original Space Out program. In fact, the play of the game doesn't change a bit; all that you're doing is adding a splash screen to spruce up the game a bit.

Writing the Game Code

The first step in adding a splash screen to the Space Out 2 game is to add a couple of global variables to the game that store the bitmap for the splash screen, as well as the mode for the splash screen. Following are these variables :

 Bitmap* _pSplashBitmap; BOOL    _bSplash; 

The first variable, _pSplashBitmap , is pretty straightforward in that it represents the bitmap image for the splash screen. The other global variable, _bSplash , is a Boolean variable that indicates whether the game is in splash screen mode. More specifically , if the _bSplash variable is TRUE , the game is in splash screen mode and the splash screen is displayed; you can't play the game while the splash screen is being displayed. If the _bSplash variable is FALSE , the game plays as normal as if there were no splash screen. So, the idea is to set the _bSplash variable to TRUE at the beginning of the game, and then return it to FALSE after the user starts the game by pressing the Enter key.

As with any global variables, it's important to initialize the splash screen variables. This takes place in the GameStart() function, which is very similar to the previous version. The first change to the GameStart() function is the creation of the splash screen bitmap:

 _pSplashBitmap = new Bitmap(hDC, IDB_SPLASH, _hInstance); 

The only other change is the initialization of the _bSplash global variable, which must be set to TRUE in order to display the splash screen when the game starts:

 _bSplash = TRUE; 

A big part of making the splash screen work properly is disabling parts of the game when the game is in splash screen mode. Listing 22.1 contains the code for the GameActivate() and GameDeactivate() functions, which check the value of the _bSplash variable before resuming or pausing the MIDI background music.

Listing 22.1 The GameActivate() and GameDeactivate() Functions Check the Value of the _bSplash Variable Before Resuming or Pausing the MIDI Background Music
 1: void GameActivate(HWND hWindow)  2: {  3:   if (!_bSplash)  4:     // Resume the background music  5:     _pGame->PlayMIDISong(TEXT(""), FALSE);  6: }  7:  8: void GameDeactivate(HWND hWindow)  9: { 10:   if (!_bSplash) 11:     // Pause the background music 12:     _pGame->PauseMIDISong(); 13: } 

There isn't too much explanation required for these functions because they simply check the _bSplash variable to see if the game is in splash screen mode before resuming or pausing the MIDI background music (lines 3 and 10).

The most important code in the Space Out 2 program in relation to the splash screen is contained in the GamePaint() function, which is where the splash screen is actually drawn. Listing 22.2 contains the code for the GamePaint() function.

Listing 22.2 The GamePaint() Function Draws the Splash Screen Bitmap When the Game Is in Splash Screen Mode
 1: void GamePaint(HDC hDC)  2: {  3:   // Draw the background  4:   _pBackground->Draw(hDC);  5:  6:   // Draw the desert bitmap  7:   _pDesertBitmap->Draw(hDC, 0, 371);  8:  9:   if (_bSplash) 10:   { 11:     // Draw the splash screen image 12:     _pSplashBitmap->Draw(hDC, 142, 100, TRUE); 13:   } 14:   else 15:   { 16:     // Draw the sprites 17:     _pGame->DrawSprites(hDC); 18: 19:     // Draw the score 20:     TCHAR szText[64]; 21:     RECT  rect = { 460, 0, 510, 30 }; 22:     wsprintf(szText, "%d", _iScore); 23:     SetBkMode(hDC, TRANSPARENT); 24:     SetTextColor(hDC, RGB(255, 255, 255)); 25:     DrawText(hDC, szText, -1, &rect, DT_SINGLELINE  DT_RIGHT  26:       DT_VCENTER); 27: 28:     // Draw the number of remaining lives (cars) 29:     for (int i = 0; i < _iNumLives; i++) 30:       _pSmCarBitmap->Draw(hDC, 520 + (_pSmCarBitmap->GetWidth() * i), 31:         10, TRUE); 32: 33:     // Draw the game over message, if necessary 34:     if (_bGameOver) 35:       _pGameOverBitmap->Draw(hDC, 170, 100, TRUE); 36:   } 37: } 

The change to the GamePaint() function involves drawing the splash screen image if the _bSplash variable is TRUE (lines 9 “13). If the _bSplash variable is FALSE , the GamePaint() function draws the sprites, score, and remaining lives, which are only pertinent to a game being played (lines 15 “36). It's important to notice that the starry background and desert bitmap are both drawn regardless of the value of the _bSplash variable (lines 3 “7). This results in the background and desert image being drawn even when the splash screen image is drawn, which is necessary because the splash screen doesn't fill up the entire game screen.

The last coding change in the Space Out 2 game involves the HandleKeys() function, which you know processes key presses for the game. If you recall, the Enter key is used to start a new game if the current game is over. Because the same key is used to leave the splash screen and start a game, it is necessary to change the value of the _bSplash variable when the user presses the Enter key. Listing 22.3 shows the new version of the HandleKeys() function with this change in place.

Listing 22.3 The HandleKeys() Function Changes the Value of the _bSplash Variable if the Game Is Exiting the Splash Screen to Start a New Game
 1: void HandleKeys()  2: {  3:   if (!_bGameOver)  4:   {  5:     // Move the car based upon left/right key presses  6:     POINT ptVelocity = _pCarSprite->GetVelocity();  7:     if (GetAsyncKeyState(VK_LEFT) < 0)  8:     {  9:       // Move left 10:       ptVelocity.x = max(ptVelocity.x - 1, -4); 11:       _pCarSprite->SetVelocity(ptVelocity); 12:     } 13:     else if (GetAsyncKeyState(VK_RIGHT) < 0) 14:     { 15:       // Move right 16:       ptVelocity.x = min(ptVelocity.x + 2, 6); 17:       _pCarSprite->SetVelocity(ptVelocity); 18:     } 19: 20:     // Fire missiles based upon spacebar presses 21:     if ((++_iFireInputDelay > 6) && GetAsyncKeyState(VK_SPACE) < 0) 22:     { 23:       // Create a new missile sprite 24:       RECT  rcBounds = { 0, 0, 600, 450 }; 25:       RECT  rcPos = _pCarSprite->GetPosition(); 26:       Sprite* pSprite = new Sprite(_pMissileBitmap, rcBounds, BA_DIE); 27:       pSprite->SetPosition(rcPos.left + 15, 400); 28:       pSprite->SetVelocity(0, -7); 29:       _pGame->AddSprite(pSprite); 30: 31:       // Play the missile (fire) sound 32:       PlaySound((LPCSTR)IDW_MISSILE, _hInstance, SND_ASYNC  33:         SND_RESOURCE  SND_NOSTOP); 34: 35:       // Reset the input delay 36:       _iFireInputDelay = 0; 37:     } 38:   } 39: 40:   // Start a new game based upon an Enter (Return) key press 41:   if (GetAsyncKeyState(VK_RETURN) < 0) 42:     if (_bSplash) 43:     { 44:       // Start a new game without the splash screen 45:       _bSplash = FALSE; 46:       NewGame(); 47:     } 48:     else if (_bGameOver) 49:     { 50:       // Start a new game 51:       NewGame(); 52:     } 53: } 

Within the code that responds to the Enter (Return) key, you'll find the code that checks to see if the game is leaving splash screen mode. This code first checks to see if the game is in splash screen mode (line 42), and then clears the _bSplash variable and starts a new game if so (lines 45 and 46). If the game is not in splash screen mode, the function checks to see if the game is over (line 48). If the game is over, a new game is started (line 51), which is similar to how the HandleKeys() function worked prior to adding the splash screen code.

Testing the Finished Product

The Space Out 2 game represents one of the easiest tests you'll ever perform on a game. All you really have to do is run the game and make sure that the splash screen appears properly. Of course, you also need to make sure that the splash screen goes away when you press the Enter key to start a new game, but you get the idea. Figure 22.2 shows the splash screen displayed in the Space Out 2 game when the game first starts.

Figure 22.2. The splash screen in the Space Out 2 game presents the game title, copyright information, and information about how to start the game.

graphics/22fig02.jpg

Keep in mind that you are free to get as fancy as you want with the splash screen image for your own games . I chose to keep the Space Out 2 splash screen relatively simple, but you might want to jazz it up to suit your own tastes. The important thing to note about this example is that the splash screen is properly displayed when the game starts.



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net