Home > Articles > Programming > Games

This chapter is from the book

Building the Game

The construction of the Space Out game is similar to that of the other games you've developed throughout the book. However, Space Out is slightly more involved simply because it is a more complete game. The next few sections guide you through the development of the game's code and resources.

Writing the Game Code

Although the development of all the previous games in the book began with the header file for the game, Space Out is a little different in that it relies on a custom sprite class. For this reason, the code for the Space Out game begins with the header for this custom sprite class, AlienSprite, which is shown in Listing 21.1.

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 overrides, Update() and AddSprite() (lines 21 and 22). These two methods are critical to providing the AlienSprite class with its own unique functionality separate from the Sprite class. In case you're wondering exactly what this functionality is, let me explain.

If you recall from earlier in the hour, one of the problems in the game engine was that it didn't allow a sprite to create another sprite on its own. You added code to the game engine, including a method called AddSprite() in the Sprite class, to allow for this task to be carried out by sprites. However, the version of the AddSprite() method in the Sprite class doesn't do anything—it's up to derived Sprite classes to create their own sprites. The AlienSprite class is an example of one of these derived classes that overrides the AddSprite() method to do something useful.

Before you get to the AlienSprite::AddSprite() method, however, let's take a quick look at some external global variables that are required of the AlienSprite class:

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 randomly setting the SA_ADDSPRITE sprite action for the aliens.

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 core of the Space Out game is laid out in the SpaceOut.h header file, which is responsible for declaring the global variables used throughout the game (Listing 21.4) .

Listing 21.4 The SpaceOut.h Header File Declares Global Variables That Are Used to Manage the Game

 1: #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 starry background for the game is declared after the bitmaps (line 34), followed by the car sprite that represents the player (line 35). Member variables storing the number of remaining lives, the score, and the difficulty level are then declared (line 37), followed by the familiar game over Boolean variable (line 38).

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 performs initialization tasks such as loading bitmaps, the NewGame() function handles starting a new game once everything else is in place. The AddAlien() function is used to simplify the task of adding alien sprites to the game (line 44). You find out more about how these functions work a little later in the hour.

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 user exits the game. Since you've seen cleanup code for several games already, let's skip the details of how this GameEnd() function works.

The game screen in the Space Out game is painted by the GamePaint() function, which is shown in Listing 21.6.

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:

  • Left arrow—Move the car left

  • Right arrow—Move the car right

  • Space—Fire a missile

  • Enter (Return)—Start a new game (if the game is over)

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 realism to the game.

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 played to indicate that the missile was fired (lines 32 and 33).

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 colliding (Listing 21.9).

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), kills both sprites (lines 16 and 17), and creates a large explosion sprite at the alien's position (lines 20–29). The score is also increased to reward the player for taking out an alien (line 32). Of course, this also means that the difficulty level is recalculated to factor in the new score (line 33).

The other collision detected in the SpriteCollision() function is between an alien missile and the car sprite (lines 37–40). If this collision takes place, a large explosion sound is played (lines 43 and 44) and the missile sprite is killed (lines 47–50). A large explosion sprite is then created at the car's position (lines 53–62). The car sprite is then moved back to its starting position (line 65).

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-related function in the Space Out game is the SpriteDying() function, which is called whenever a sprite is being destroyed. In the case of Space Out, this function is used to create a small explosion sprite any time an alien missile sprite is destroyed. Listing 21.10 shows how this function works.

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 fairly rapidly in any direction, but he bounces off the edges of the game screen (lines 12 and 13). Jelly also bounces off the screen edges (line 17), but his velocity is set differently so that he tends to move much more vertically than Blobbo (line 20). Finally, Timmy moves entirely horizontally (line 27), and is allowed to wrap off the screen from right to left (line 24). The AddAlien() function ends by adding the new alien sprite to the game engine (line 32).

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020