Chapter 21. Example Game: Space Out

This hour embarks yet again on the development of another complete game. The game is called Space Out, and it represents a culmination of everything you’ve learned about game programming throughout the book. Although this isn’t the last hour of the book, this is the last complete game you’ll be creating. The remaining hours focus on improving the Space Out game with some interesting features. This hour, however, explores the design and development of the basic Space Out game, which is a vertical space shoot-em-up. The closest arcade comparison I can make to Space Out is Galaga, but the aliens in Space Out don’t move with as intricate of patterns as those in Galaga. Nevertheless, I think you’ll find Space Out to be a fun and entertaining game, both from a programming and a playability perspective.

In this hour, you’ll learn:

  • About the basic premise behind the Space Out game

  • How to design the Space Out game

  • About the nuts and bolts of programming the Space Out game

  • Why testing is still the most fun part of testing a new game

How Does the Game Play?

One of the most classic genres of video games has always been the vertical space shoot-em-up. Space Invaders started it all back in 1978, but many games followed and added their own unique contributions to the genre. One of the most enduring vertical space shooters is Galaga, which you learned about back in the introduction to Hour 9, “A Crash Course in Game Animation.” In Galaga, a relentless sequence of invading aliens fly down from the top of the game screen and attack your ship, which is free to move horizontally across the bottom of the screen. The Space Out game that you develop in this hour is similar in some ways to Galaga, although the theme for the game is a little more whimsical.

In Space Out, you are the driver of a small green car on a trek across the desert. Whether you believe in UFOs, it’s hard to argue that quite a few sightings seem to have occurred in remote desert places such as Roswell, New Mexico. For this reason, your traveler in the game can’t seem to get away from a constant onslaught of alien visitors from above. Unfortunately, the aliens in Space Out are bent on putting an end to our traveler’s trip. The cast of alien characters in the Space Out game are somewhat comical, and add a degree of whimsy to the game. Following are the three kinds of aliens that appear throughout the game:

  • Blobbo the Galactic Ooze

  • Jellybiafra (Jelly for short)

  • Timmy the Space Worm

Granted, these probably aren’t very realistic aliens when it comes to what you might imagine truly encountering in an extra-terrestrial sighting, but this game isn’t about reality. Each of the aliens has its own style of attack, and they each fire different missiles. The idea here isn’t to simulate a realistic alien invasion, but to have some fun with outlandish characters in the context of a vertical shoot-em-up.

Note

How Does the Game Play?

The characters and concept for the Space Out game were created by Rebecca Rose, a computer artist and game designer who has collaborated with me in the past on other game projects.

Designing the Game

Now that you understand the basic idea behind the game, let’s focus on a few details regarding the design of the game. The player’s car can move horizontally across the game screen, which means that its position is confined to the x axis. The player can shoot up vertically—with his Twinkie missiles terminating at the top of the screen, similar to the missiles in the Meteor Defense game from Hour 19, “Example Game: Meteor Defense.”

The aliens in Space Out can move around in any direction and at different velocities. The Blobbo and Jelly aliens bounce off of the edges of the screen, while Timmy is allowed to wrap around and appear on the other side. This is because Timmy has a tendency to fly horizontally across the screen, whereas the others move around a little more randomly. All the aliens fire missiles down toward the player’s car—with the missiles terminating when they strike the car or the ground. The aliens are immune from their own missiles, so they can’t hit each other. This is a good thing for the aliens because they aren’t very careful in terms of how they aim.

Space Out has no discrete levels and no real goal other than surviving. However, the difficulty level of the game does gradually increase as the player progresses through the game. The difficulty of the game increases by adding new aliens at a faster pace. Eventually the player will have his hands full trying to contend with a never-ending army of aliens. That’s the whole fun of shoot-em-ups!

To help you get a feel for how the Space Out game is laid out, take a look at Figure 21.1.

The Space Out game consists of a starry background, a ground desert image, a car, aliens, and missiles that are fired between the car and aliens.

Figure 21.1. The Space Out game consists of a starry background, a ground desert image, a car, aliens, and missiles that are fired between the car and aliens.

The figure shows the background image of the desert, which serves as a backdrop for the car to drive around on. The starry background still appears in the majority of the screen, whereas the background image shows the desert where the car drives around. The background image actually includes a horizontal band of sky that helps blend the desert landscape into the starry background. The car sprite moves around on top of the desert background image. The aliens appear in the sky and move around trying to hit the car by firing various missiles. Of course, the car also fires missiles back at the aliens. The score for the game is displayed in the upper right corner of the game screen, along with the number of remaining lives (cars).

Now that you understand the basics of the game, it’s important to examine the sprites that it requires. Following is a list of the sprites that go into the Space Out game:

  • Car sprite

  • Alien sprites

  • Missile sprites (from the aliens and the car)

  • Explosion sprites

The only type of sprite in this list that I haven’t mentioned is the explosion sprite, which is used to show an alien being destroyed by a missile, as well as the car being destroyed by an alien missile. Two different sizes of explosions are actually used in the Space Out game. The larger explosion is used to show an alien or the car being destroyed, whereas the smaller explosion is used to show a missile exploding. This distinction is important because it shows how missiles cause a smaller explosion when they simply crash into the desert, as opposed to hitting the car.

In addition to the sprites, the Space Out game requires several bitmaps. Following are the bitmap images required of the game:

  • Background desert image

  • Car image

  • Missile image (fired by car)

  • Alien images (Blobbo, Jelly, and Timmy; animated)

  • Missile images (fired by each of the three aliens)

  • Explosion images (animated; small and large)

  • Small car image

  • Game over image

These images flow directly from the design of the game that you’ve covered thus far, so there hopefully shouldn’t be any big surprises here. Perhaps the main thing to point out is that the aliens all rely on animated images to provide them with a more interesting appearance as they move around on the game screen. The only other animated images in the game are the explosion images, which go with the explosion sprites.

The score needs to be maintained throughout the game, as well as the number of remaining lives (cars); the game ends when all three of your cars have been destroyed. The difficulty level of the game is stored away in a global variable, and gradually increases as the player racks up points and progresses through the game. Another important piece of information is the familiar Boolean variable that keeps track of whether the game is over.

The last global game variable required of Space Out is an input delay variable that helps add some restraint to the player’s ability to fire missiles rapidly. Without this input delay variable, it would be too easy to wipe out the aliens by holding down the fire key (Spacebar) and raining missiles on the aliens with little effort. By slowing down the number of missiles that can be fired, the player is forced to be more accurate and evade aliens when they miss. Establishing the appropriate delay is somewhat of a trial-and-error process, and you might decide that the game is more fun with a slightly different value than I used.

To recap, the design of the Space Out game has led us to the following pieces of information that must be managed by the game:

  • The number of lives (cars) remaining

  • The score

  • The difficulty level

  • A Boolean game over variable

  • A delay variable for firing input

Before diving into the code for the Space Out game, you need to add a new feature to the game engine. Although the game engine is a flexible piece of software, you will likely find yourself adding new features in the future to accommodate the special needs of new games. This is a good thing because you get to reuse those features in any game that you create from then on.

Adding One Last Sprite Feature to the Game Engine

The missing feature in the game engine critical to the Space Out game is the capability for a sprite to automatically create another sprite. This might seem like a strange requirement, but think about an alien that is firing a missile at the car in the Space Out game. The missile must be created so that it appears to come from the alien, which means that the missile must know about the alien’s position. Not only that, but the missile must be unique to the alien that fired it because each alien fires a different kind of missile. This presents a significant challenge to the current design of the game engine because there isn’t a good way to automatically create a new sprite based on the properties of another sprite.

A good solution to this problem involves allowing a sprite to create another sprite whenever it needs to. For example, you can allow the alien sprites to create missile sprites themselves, which makes it very easy to position the missile based on the alien’s position—not to mention creating the appropriate missile for each different type of alien. The problem with this approach is that it is impossible to add this functionality to the Sprite class in a generic fashion. In other words, the specifics regarding what kind of sprite needs to be created are unique to each game, and therefore can’t be carried out in the Sprite class. However, the Sprite class can establish the interface that makes it possible.

An important part of this new “add sprite” feature is a new sprite action called SA_ADDSPRITE. The following code shows how the SA_ADDSPRITE sprite action has been added to the existing sprite actions:

typedef WORD        SPRITEACTION;
const SPRITEACTION  SA_NONE      = 0x0000L,
                    SA_KILL      = 0x0001L,
                    SA_ADDSPRITE = 0x0002L;

If you recall, sprite actions are used to signal to the game engine that some particular action must be taken in regard to a sprite. The SA_ADDSPRITE sprite action simply results in a special method being called on the Sprite object to which the action applies. This method is called AddSprite(), and looks like this:

virtual Sprite* AddSprite();

The idea behind the AddSprite() method is that it gets called to allow a sprite to add a new sprite to the game engine. The specifics of the new sprite are entirely dependent on each individual game. In fact, the version of the AddSprite() method in the Sprite class doesn’t do anything, as the following code reveals:

Sprite* Sprite::AddSprite()
{
  return NULL;
}

This code shows how the base Sprite::AddSprite() method doesn’t do anything other than return a NULL sprite pointer, which reveals that the task of adding a sprite via the AddSprite() method is left up to derived sprite classes. So, in order to take advantage of this method, you must derive a sprite class for a particular kind of sprite, and then override the AddSprite() method with a version that actually creates a sprite.

The SA_ADDSPRITE action and AddSprite() method enter the picture in the game engine in the GameEngine::UpdateSprites() method, which must check for the presence of the SA_ADDSPRITE sprite action, and then call the AddSprite() method on the sprite if the action is set. The return value of the sprite’s AddSprite() method is passed along to the game engine’s AddSprite() function, which handles inserting the sprite into the sprite list. This is all that is required of the game engine to support adding a sprite from within another sprite.

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.

Example 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.

Example 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.

Example 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).

Example 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.

Example 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.

Example 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.

Example 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.

Example 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).

Example 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.

Example 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).

Example 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.

Example 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.

Testing the Game

I’ve already said it numerous times that testing a game is the most fun part, and yet again you’ve arrived at the testing phase of a completely new game. Similar to the Meteor Defense game, the Space Out game requires a fair amount of testing simply because a lot of different interactions are taking place among the different sprites in the game. The great thing is that you test a game simply by playing it. Figure 21.2 shows the Space Out game at the beginning, with a single alien firing a few missiles at the car below.

The Space Out game gets started with an alien firing a few missiles at the car below.

Figure 21.2. The Space Out game gets started with an alien firing a few missiles at the car below.

You can move the car left and right using the arrow keys, and then fire back at the alien using the Space key (Spacebar). Shooting an alien results in a small explosion appearing, as shown in Figure 21.3.

A small explosion appears when you successfully shoot an alien.

Figure 21.3. A small explosion appears when you successfully shoot an alien.

Eventually, you’ll venture into dangerous territory and get shot by an alien, which results in a large explosion appearing, as shown in Figure 21.4.

A large explosion appears when the car gets shot by an alien.

Figure 21.4. A large explosion appears when the car gets shot by an alien.

You only have three cars to lose, and the number of remaining cars is shown in the upper-right corner of the game screen next to the score. When you lose all three cars, the game ends, as shown in Figure 21.5.

When you lose all three cars, the game ends and the game over image is displayed.

Figure 21.5. When you lose all three cars, the game ends and the game over image is displayed.

The good news about the game ending in Space Out is that you can immediately start a new one by simply pressing the Enter (Return) key.

Summary

Regardless of whether you are a fan of shoot-em-up space games, I hope you realized the significance of the Space Out game that you designed and built in this hour. In addition to adding yet another complete game to your accomplishments, the Space Out game is important because it represents the most complete game in the book. In other words, it makes the most complete usage of the features you’ve worked so hard adding to the game engine. Not only that, but the Space Out game is a great game for experimenting with your own ideas, simply because it is the kind of game that can be expanded upon in so many different ways. Before you get too crazy modifying the Space Out game, however, sit tight because I have a few modifications of my own to throw at you before I turn you loose in the wild world of game programming.

This hour concludes this part of the book, which was admittedly quite short. The next part of the book picks up right where you left off by showing you some sure-fire techniques to add pizzazz to your games. More specifically, you find out how to add interesting features to the Space Out game such as a splash screen, a demo mode, and a high score list.

Q&A

Q1:

Why does the HandleKeys() function change the velocity of the car sprite in response to the arrow keys, instead of directly changing its position?

A1:

The HandleKeys() function could certainly change the position of the car sprite to get a similar result, but by changing the velocity you get a smoother sense of control in the game. More specifically, the movement of the car isn’t choppy because when it changes direction, it actually decelerates and then accelerates again. This is a minor detail, but one that helps to add a touch of realism to the game.

Q2:

Why doesn’t the Space Out game include a unique derived Sprite class for each kind of alien?

A2:

The game could have been designed to utilize three derived Sprite classes such as BlobboSprite, JellySprite, and TimmySprite, but in reality it just wasn’t necessary to break out the code that much. If the different aliens had required radically different functionality within them, this approach might have made more sense. And if you’re a stickler for adhering to object-oriented design, you could still go back and redesign the game around three alien classes instead of one. However, I usually err on the side of simplicity, and in this case one class does the trick just fine.

Workshop

The Workshop is designed to help you anticipate possible questions, review what you’ve learned, and begin learning how to put your knowledge into practice. The answers to the quiz can be found in Appendix A, “Quiz Answers.”

Quiz

1:

What is the purpose of the SA_ADDSPRITE sprite action?

2:

Why is it necessary to derive a class from the Sprite class in order for it to take advantage of the SA_ADDSPRITE sprite action?

3:

What role does the AlienSprite class play in the Space Out game?

Exercises

  1. Modify the Update() method in the AlienSprite class so that the aliens fly around in different flight patterns.

  2. Create an entirely new alien and add it to the Space Out game. This involves creating an animated bitmap image for the alien, as well as adding appropriate code throughout the Space Out game code, including the AlienSprite class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.255.168