Building the Game
The construction of the Space Out game is similar to that of the other
Writing the Game Code
Although the development of all the previous games in the book
Listing 21.1 The AlienSprite.h Header File Declares the AlienSprite Class, Which Is Derived from Sprite
1: #pragma once
2:
3: //-----------------------------------------------------------------
4: // Include Files
5: //-----------------------------------------------------------------
6: #include <windows.h>
7: #include "Sprite.h"
8:
9: //-----------------------------------------------------------------
10: // AlienSprite Class
11: //-----------------------------------------------------------------
12: class AlienSprite : public Sprite
13: {
14: public:
15: // Constructor(s)/Destructor
16: AlienSprite(Bitmap* pBitmap, RECT& rcBounds,
17: BOUNDSACTION baBoundsAction = BA_STOP);
18: virtual ~AlienSprite();
19:
20: // General Methods
21: virtual SPRITEACTION Update();
22: virtual Sprite* AddSprite();
23: };
The
AlienSprite
class is not very complex at all, as the listing hopefully reveals. It's important to notice that
AlienSprite
derives from
Sprite
(line 12), and declares a constructor and destructor (lines 16 “18). More importantly, however, are the two methods in the
Sprite
class that
AlienSprite
If you recall from earlier in the
Before you get to the
AlienSprite::AddSprite()
method, however, let's take a quick look at some external global
extern Bitmap* _pBlobboBitmap; extern Bitmap* _pBMissileBitmap; extern Bitmap* _pJellyBitmap; extern Bitmap* _pJMissileBitmap; extern Bitmap* _pTimmyBitmap; extern Bitmap* _pTMissileBitmap; extern int _iDifficulty; These global variables are part of the main Space Out game code, and are included in the file SpaceOut.h, which you see in a moment. They must be declared externally in the AlienSprite code because the code references the global variables. Listing 21.2 contains the code for the AlienSprite::AddSprite() method, which makes use of the external global variables to determine what kind of missile sprite to add. Listing 21.2 The AlienSprite::AddSprite() Method Adds a Missile Sprite Based on the Alien That Is Firing It
1: Sprite* AlienSprite::AddSprite()
2: {
3: // Create a new missile sprite
4: RECT rcBounds = { 0, 0, 640, 410 };
5: RECT rcPos = GetPosition();
6: Sprite* pSprite = NULL;
7: if (GetBitmap() == _pBlobboBitmap)
8: {
9: // Blobbo missile
10: pSprite = new Sprite(_pBMissileBitmap, rcBounds, BA_DIE);
11: pSprite->SetVelocity(0, 7);
12: }
13: else if (GetBitmap() == _pJellyBitmap)
14: {
15: // Jelly missile
16: pSprite = new Sprite(_pJMissileBitmap, rcBounds, BA_DIE);
17: pSprite->SetVelocity(0, 5);
18: }
19: else
20: {
21: // Timmy missile
22: pSprite = new Sprite(_pTMissileBitmap, rcBounds, BA_DIE);
23: pSprite->SetVelocity(0, 3);
24: }
25:
26: // Set the missile sprite's position and return it
27: pSprite->SetPosition(rcPos.left + (GetWidth() / 2), rcPos.bottom);
28: return pSprite;
29: }
The purpose of this AddSprite() method is to allow an alien sprite to create a missile sprite. The important thing to notice in the AddSprite() method is that it fires a different kind of missile for each different kind of alien. To determine what kind of alien is firing the missile, the method checks the bitmap image for the sprite (lines 7, 13, and 19). A missile sprite is then created based on which alien is firing the missile (lines 10, 11, 16, 17, 22, and 23). Finally, the new missile sprite's position is set so that it appears just below the alien (line 27), and the sprite is returned from the method so that it can be added to the sprite list in the game engine (line 28).
You might have noticed earlier in the
AlienSprite
class that the
Update()
method is also overridden. Listing 21.3 contains the code for the
AlienSprite::Update()
method, which handles
Listing 21.3 The AlienSprite::Update() Method Randomly Sets the SA_SPRITEACTION Sprite Action so That the Aliens Fire Missiles
1: SPRITEACTION AlienSprite::Update()
2: {
3: // Call the base sprite Update() method
4: SPRITEACTION saSpriteAction;
5: saSpriteAction = Sprite::Update();
6:
7: // See if the alien should fire a missile
8: if ((rand() % (_iDifficulty / 2)) == 0)
9: saSpriteAction = SA_ADDSPRITE;
10:
11: return saSpriteAction;
12: }
The most important line of code in this method is line 5, which calls the base class Update() method so that the sprite is properly updated. The method then uses the _iDifficulty global variable as the basis for randomly setting the SA_SPRITEACTION sprite action (lines 8 and 9), which results in the alien firing a missile. This works because the SA_SPRITEACTION sprite action causes the AlienSprite::AddSprite() method to get called, which creates a new missile sprite and adds it to the game engine.
That wraps up the code for the
AlienSprite
class, which is a crucial component of the Space Out game. The
Listing 21.4 The SpaceOut.h Header File Declares Global Variables That Are Used to Manage the Game1: #pragma once 2: 3: //----------------------------------------------------------------- 4: // Include Files 5: //----------------------------------------------------------------- 6: #include <windows.h> 7: #include "Resource.h" 8: #include "GameEngine.h" 9: #include "Bitmap.h" 10: #include "Sprite.h" 11: #include "Background.h" 12: #include "AlienSprite.h" 13: 14: //----------------------------------------------------------------- 15: // Global Variables 16: //----------------------------------------------------------------- 17: HINSTANCE _hInstance; 18: GameEngine* _pGame; 19: HDC _hOffscreenDC; 20: HBITMAP _hOffscreenBitmap; 21: Bitmap* _pDesertBitmap; 22: Bitmap* _pCarBitmap; 23: Bitmap* _pSmCarBitmap; 24: Bitmap* _pMissileBitmap; 25: Bitmap* _pBlobboBitmap; 26: Bitmap* _pBMissileBitmap; 27: Bitmap* _pJellyBitmap; 28: Bitmap* _pJMissileBitmap; 29: Bitmap* _pTimmyBitmap; 30: Bitmap* _pTMissileBitmap; 31: Bitmap* _pSmExplosionBitmap; 32: Bitmap* _pLgExplosionBitmap; 33: Bitmap* _pGameOverBitmap; 34: StarryBackground* _pBackground; 35: Sprite* _pCarSprite; 36: int _iFireInputDelay; 37: int _iNumLives, _iScore, _iDifficulty; 38: BOOL _bGameOver; 39: 40: //----------------------------------------------------------------- 41: // Function Declarations 42: //----------------------------------------------------------------- 43: void NewGame(); 44: void AddAlien();
The listing shows that the global variables for the Space Out game largely consist of the different bitmaps used throughout the game. The
Similar to the Meteor Defense game in Hour 19, the Space Out game also relies on a couple of support functions. The first of these,
NewGame()
, is important because it is used to set up and start a new game (line 43). Unlike the
GameStart()
function, which
The primary setup code for the game takes place in the familiar GameStart() function, which is shown in Listing 21.5. Listing 21.5 The GameStart() Function Initializes the Bitmaps and Background for the Game, and Calls the NewGame() Function
1: void GameStart(HWND hWindow)
2: {
3: // Seed the random number generator
4: srand(GetTickCount());
5:
6: // Create the offscreen device context and bitmap
7: _hOffscreenDC = CreateCompatibleDC(GetDC(hWindow));
8: _hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow),
9: _pGame->GetWidth(), _pGame->GetHeight());
10: SelectObject(_hOffscreenDC, _hOffscreenBitmap);
11:
12: // Create and load the bitmaps
13: HDC hDC = GetDC(hWindow);
14: _pDesertBitmap = new Bitmap(hDC, IDB_DESERT, _hInstance);
15: _pCarBitmap = new Bitmap(hDC, IDB_CAR, _hInstance);
16: _pSmCarBitmap = new Bitmap(hDC, IDB_SMCAR, _hInstance);
17: _pMissileBitmap = new Bitmap(hDC, IDB_MISSILE, _hInstance);
18: _pBlobboBitmap = new Bitmap(hDC, IDB_BLOBBO, _hInstance);
19: _pBMissileBitmap = new Bitmap(hDC, IDB_BMISSILE, _hInstance);
20: _pJellyBitmap = new Bitmap(hDC, IDB_JELLY, _hInstance);
21: _pJMissileBitmap = new Bitmap(hDC, IDB_JMISSILE, _hInstance);
22: _pTimmyBitmap = new Bitmap(hDC, IDB_TIMMY, _hInstance);
23: _pTMissileBitmap = new Bitmap(hDC, IDB_TMISSILE, _hInstance);
24: _pSmExplosionBitmap = new Bitmap(hDC, IDB_SMEXPLOSION, _hInstance);
25: _pLgExplosionBitmap = new Bitmap(hDC, IDB_LGEXPLOSION, _hInstance);
26: _pGameOverBitmap = new Bitmap(hDC, IDB_GAMEOVER, _hInstance);
27:
28: // Create the starry background
29: _pBackground = new StarryBackground(600, 450);
30:
31: // Play the background music
32: _pGame->PlayMIDISong(TEXT("Music.mid"));
33:
34: // Start the game
35: NewGame();
36: }
After the
GameStart()
function finishes loading the bitmaps for the game (lines 14 “26) and creating the starry background (line 29), it starts playing the background music (line 32). The function then finishes up by calling the
NewGame()
function to start a new game (line 35). The
GameEnd()
function cleans up after the
GameStart()
function, and is called whenever the
The game screen in the Space Out game is
Listing 21.6 The GamePaint() Function Draws the Background, the Desert Ground Bitmap, the Sprites, the Score, and the Game Over Message
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: // Draw the sprites
10: _pGame->DrawSprites(hDC);
11:
12: // Draw the score
13: TCHAR szText[64];
14: RECT rect = { 460, 0, 510, 30 };
15: wsprintf(szText, "%d", _iScore);
16: SetBkMode(hDC, TRANSPARENT);
17: SetTextColor(hDC, RGB(255, 255, 255));
18: DrawText(hDC, szText, -1, &rect, DT_SINGLELINE DT_RIGHT DT_VCENTER);
19:
20: // Draw the number of remaining lives (cars)
21: for (int i = 0; i < _iNumLives; i++)
22: _pSmCarBitmap->Draw(hDC, 520 + (_pSmCarBitmap->GetWidth() * i),
23: 10, TRUE);
24:
25: // Draw the game over message, if necessary
26: if (_bGameOver)
27: _pGameOverBitmap->Draw(hDC, 190, 149, TRUE);
28: }
The GamePaint() function takes care of drawing all graphics for the Space Out game. The function begins by drawing the starry background (line 4), followed by the desert ground image (line 7). The sprites are then drawn (line 10), followed by the score (lines 13 “18). The number of remaining lives, which are represented by small car images, are drawn in the upper-right corner of the screen just right of the score (lines 21 “23). Finally, the function finishes up by drawing the game over image, if necessary (lines 21 “27). The GameCycle() function works hand-in-hand with the GamePaint() function to update the game's sprites and reflect the changes onscreen. Listing 21.7 shows the code for the GameCycle() function. Listing 21.7 The GameCycle() Function Randomly Adds New Aliens to the Game
1: void GameCycle()
2: {
3: if (!_bGameOver)
4: {
5: // Randomly add aliens
6: if ((rand() % _iDifficulty) == 0)
7: AddAlien();
8:
9: // Update the background
10: _pBackground->Update();
11:
12: // Update the sprites
13: _pGame->UpdateSprites();
14:
15: // Obtain a device context for repainting the game
16: HWND hWindow = _pGame->GetWindow();
17: HDC hDC = GetDC(hWindow);
18:
19: // Paint the game to the offscreen device context
20: GamePaint(_hOffscreenDC);
21:
22: // Blit the offscreen bitmap to the game screen
23: BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame->GetHeight(),
24: _hOffscreenDC, 0, 0, SRCCOPY);
25:
26: // Cleanup
27: ReleaseDC(hWindow, hDC);
28: }
29: }
Aside from the standard GameCycle() code that you're already accustomed to seeing, this function doesn't add much additional code. The new code involves randomly adding new aliens, which is accomplished by calling the AddAlien() function after using the difficulty level to randomly determine if an alien should be added (lines 6 “7). With the alien sprites squared away, you still have to contend with how the user is going to control the car sprite. This is accomplished via a keyboard interface using the HandleKeys() function, which is shown in Listing 21.8. Listing 21.8 The HandleKeys() Function Allows the User to Control the Car Sprite Using Keys on the Keyboard
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 (_bGameOver && (GetAsyncKeyState(VK_RETURN) < 0))
42: // Start a new game
43: NewGame();
44: }
The HandleKeys() function looks to see if any of four keys are being pressed. Following are the meanings of these keys in the context of the Space Out game:
By knowing the meanings of these keys, the code in the
HandleKeys()
function hopefully makes a bit more sense. The function begins by making sure that the game isn't over (line 3), and then proceeds to check on the status of each of the three keys that have relevance to the game play; the fourth key (Enter) only applies to a game that is over. If the left arrow key is pressed, the
HandleKeys()
function alters the car sprite's velocity so that it moves more to the left (lines 10 and 11). On the other hand, if the right arrow key is pressed, the car sprite's velocity is set so that it moves more to the right (lines 16 and 17). One interesting thing to note about this code is that the car is capable of moving faster to the right than it is to the left, which is because the car is aiming to the right. In other words, it can't go as fast in reverse as it can moving forward, which adds a little
Firing missiles is initiated by the user pressing the Space key (Spacebar), but it only takes place if the fire input delay has been triggered (line 21). The net effect of the fire input delay is to slow down the firing of missiles so that the player can't go crazy with a barrage of missiles; the game would be too easy if you could fire at that rate. To actually fire a missile, a missile sprite is created and set to a position just above the car sprite, which makes the missile appear to originate from the car (lines 24 “29). A sound effect is also
The last section of code in the HandleKeys() function starts a new game in response to the user pressing the Enter (Return) key. The _bGameOver variable is checked to make sure that the game is over (line 41), and the NewGame() function is called to start the new game (line 43).
Another important function in the Space Out game is the
SpriteCollision()
function, which is called in response to sprites
Listing 21.9 The SpriteCollision() Function Responds to Collisions Between Missiles, Aliens, and the Car Sprite
1: BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
2: {
3: // See if a player missile and an alien have collided
4: Bitmap* pHitter = pSpriteHitter->GetBitmap();
5: Bitmap* pHittee = pSpriteHittee->GetBitmap();
6: if ((pHitter == _pMissileBitmap && (pHittee == _pBlobboBitmap
7: pHittee == _pJellyBitmap pHittee == _pTimmyBitmap))
8: (pHittee == _pMissileBitmap && (pHitter == _pBlobboBitmap
9: pHitter == _pJellyBitmap pHitter == _pTimmyBitmap)))
10: {
11: // Play the small explosion sound
12: PlaySound((LPCSTR)IDW_LGEXPLODE, _hInstance, SND_ASYNC
13: SND_RESOURCE);
14:
15: // Kill both sprites
16: pSpriteHitter->Kill();
17: pSpriteHittee->Kill();
18:
19: // Create a large explosion sprite at the alien's position
20: RECT rcBounds = { 0, 0, 600, 450 };
21: RECT rcPos;
22: if (pHitter == _pMissileBitmap)
23: rcPos = pSpriteHittee->GetPosition();
24: else
25: rcPos = pSpriteHitter->GetPosition();
26: Sprite* pSprite = new Sprite(_pLgExplosionBitmap, rcBounds);
27: pSprite->SetNumFrames(8, TRUE);
28: pSprite->SetPosition(rcPos.left, rcPos.top);
29: _pGame->AddSprite(pSprite);
30:
31: // Update the score
32: _iScore += 25;
33: _iDifficulty = max(80 - (_iScore / 20), 20);
34: }
35:
36: // See if an alien missile has collided with the car
37: if ((pHitter == _pCarBitmap && (pHittee == _pBMissileBitmap
38: pHittee == _pJMissileBitmap pHittee == _pTMissileBitmap))
39: (pHittee == _pCarBitmap && (pHitter == _pBMissileBitmap
40: pHitter == _pJMissileBitmap pHitter == _pTMissileBitmap)))
41: {
42: // Play the large explosion sound
43: PlaySound((LPCSTR)IDW_LGEXPLODE, _hInstance, SND_ASYNC
44: SND_RESOURCE);
45:
46: // Kill the missile sprite
47: if (pHitter == _pCarBitmap)
48: pSpriteHittee->Kill();
49: else
50: pSpriteHitter->Kill();
51:
52: // Create a large explosion sprite at the car's position
53: RECT rcBounds = { 0, 0, 600, 480 };
54: RECT rcPos;
55: if (pHitter == _pCarBitmap)
56: rcPos = pSpriteHitter->GetPosition();
57: else
58: rcPos = pSpriteHittee->GetPosition();
59: Sprite* pSprite = new Sprite(_pLgExplosionBitmap, rcBounds);
60: pSprite->SetNumFrames(8, TRUE);
61: pSprite->SetPosition(rcPos.left, rcPos.top);
62: _pGame->AddSprite(pSprite);
63:
64: // Move the car back to the start
65: _pCarSprite->SetPosition(300, 405);
66:
67: // See if the game is over
68: if (--_iNumLives == 0)
69: {
70: // Play the game over sound
71: PlaySound((LPCSTR)IDW_GAMEOVER, _hInstance, SND_ASYNC
72: SND_RESOURCE);
73: _bGameOver = TRUE;
74: }
75: }
76:
77: return FALSE;
78: }
The
SpriteCollision()
function is undoubtedly the heftiest function in the Space Out game, and for good reason: The collisions between the sprites in the game completely control the play of the game. The function begins by checking for a collision between a player missile and an alien (lines 6 “9). If the collision occurred, the
SpriteCollision()
function plays a small explosion sound (lines 12 and 13),
The other collision
The last section of the SpriteCollision() function checks the number of lives to see if the game is over (line 68). If so, a game over sound is played and the _bGameOver variable is set to TRUE (lines 71 “73).
Another important sprite-
Listing 21.10 The SpriteDying() Function Creates a Small Explosion Whenever an Alien Missile Sprite Is Destroyed
1: void SpriteDying(Sprite* pSprite)
2: {
3: // See if an alien missile sprite is dying
4: if (pSprite->GetBitmap() == _pBMissileBitmap
5: pSprite->GetBitmap() == _pJMissileBitmap
6: pSprite->GetBitmap() == _pTMissileBitmap)
7: {
8: // Play the small explosion sound
9: PlaySound((LPCSTR)IDW_SMEXPLODE, _hInstance, SND_ASYNC
10: SND_RESOURCE SND_NOSTOP);
11:
12: // Create a small explosion sprite at the missile's position
13: RECT rcBounds = { 0, 0, 600, 450 };
14: RECT rcPos = pSprite->GetPosition();
15: Sprite* pSprite = new Sprite(_pSmExplosionBitmap, rcBounds);
16: pSprite->SetNumFrames(8, TRUE);
17: pSprite->SetPosition(rcPos.left, rcPos.top);
18: _pGame->AddSprite(pSprite);
19: }
20: }
The function begins by checking to see if the dying sprite is an alien missile (lines 4 “6). If so, a small explosion sound is played (lines 9 and 10), and a small explosion sprite is created (lines 13 “18). The last two functions in the Space Out game are support functions that are completely unique to the game. The first one is NewGame() , which performs the steps necessary to start a new game (Listing 21.11). Listing 21.11 The NewGame() Function Gets Everything Ready for a New Game
1: void NewGame()
2: {
3: // Clear the sprites
4: _pGame->CleanupSprites();
5:
6: // Create the car sprite
7: RECT rcBounds = { 0, 0, 600, 450 };
8: _pCarSprite = new Sprite(_pCarBitmap, rcBounds, BA_WRAP);
9: _pCarSprite->SetPosition(300, 405);
10: _pGame->AddSprite(_pCarSprite);
11:
12: // Initialize the game variables
13: _iFireInputDelay = 0;
14: _iScore = 0;
15: _iNumLives = 3;
16: _iDifficulty = 80;
17: _bGameOver = FALSE;
18:
19: // Play the background music
20: _pGame->PlayMIDISong();
21: }
The NewGame() function begins by clearing the sprite list (line 4), which is necessary because you aren't certain what sprites have been left over from the previous game. The car sprite is then created (lines 7 “10), and the global game variables are set (lines 13 “17). The function then finishes by starting the background music (line 20). The AddAlien() function is shown in Listing 21.12, and its job is to add a new alien to the game at a random location. Listing 21.12 The AddAlien() Function Adds a New Alien at a Random Position
1: void AddAlien()
2: {
3: // Create a new random alien sprite
4: RECT rcBounds = { 0, 0, 600, 410 };
5: AlienSprite* pSprite;
6: switch(rand() % 3)
7: {
8: case 0:
9: // Blobbo
10: pSprite = new AlienSprite(_pBlobboBitmap, rcBounds, BA_BOUNCE);
11: pSprite->SetNumFrames(8);
12: pSprite->SetPosition(((rand() % 2) == 0) ? 0 : 600, rand() % 370);
13: pSprite->SetVelocity((rand() % 7) - 2, (rand() % 7) - 2);
14: break;
15: case 1:
16: // Jelly
17: pSprite = new AlienSprite(_pJellyBitmap, rcBounds, BA_BOUNCE);
18: pSprite->SetNumFrames(8);
19: pSprite->SetPosition(rand() % 600, rand() % 370);
20: pSprite->SetVelocity((rand() % 5) - 2, (rand() % 5) + 3);
21: break;
22: case 2:
23: // Timmy
24: pSprite = new AlienSprite(_pTimmyBitmap, rcBounds, BA_WRAP);
25: pSprite->SetNumFrames(8);
26: pSprite->SetPosition(rand() % 600, rand() % 370);
27: pSprite->SetVelocity((rand() % 7) + 3, 0);
28: break;
29: }
30:
31: // Add the alien sprite
32: _pGame->AddSprite(pSprite);
33: }
The
AddAlien()
function adds a new alien to the game, which can be one of three types: Blobbo, Jelly, or Timmy. The code for the creation of each type of alien is similar, but each alien has slightly different characteristics. For example, Blobbo is capable of moving around
You're probably relieved to find out that this wraps up the code for the Space Out game, which means that you're ready to put the resources together and take the game for a test spin. |