Building the Battle Office 2 Program Example


You're now sitting there with a perfectly good game engine with a shiny new feature, so you're no doubt anxious to see how it works in a practical game. Fortunately, it's not too difficult to find a good application for animated sprites . I mentioned earlier in the book that the Battle Office game had a deficiency in that the guys moving across the hallway near the top of the game screen appear to slide, as opposed to run. It would only look as if they were running if their legs were moving as they moved across the game screen. This kind of visual effect is only possible through frame animation, which you now have the capability of carrying out within the game engine you've now grown to love. Okay, love might be too strong a word, but hopefully you've at least grown to appreciate what it can do.

The next couple of sections focus on what it takes to animate the two moving guys in the Battle Office game using frame-animated sprites. Because you've already built the animation logic into the game engine, modifying the Battle Office game takes very little effort. You'll call the new version of the game with the animated guys Battle Office 2.

Writing the Program Code

I mentioned earlier this hour that the only thing required from a programming perspective to add animated sprites to a game is setting the number of frames and the frame delay for each sprite. Of course, you'll also need to create a suitable bitmap image containing the frames for the sprite, but that doesn't directly impact the programming side of things. In the case of the Battle Office 2 game, the only two sprites being altered for frame animation are the two moving guy sprites. Both sprites use four frame images, which means that they each need to be set to use four frames. Listing 17.7 contains the sprite creation code for the first moving guy sprite.

graphics/clock.gif

If you set the number of frames for a sprite to a number greater than one but you don't change the sprite's bitmap image, you definitely will get strange results. This has to do with the fact that the sprite automatically assumes that the individual frame images are arranged horizontally across the sprite image.


Listing 17.7 The Sprite Creation Code for the First Moving Guy Establishes the Number of Frames for the Animated Sprite
 1: _pGuySprites[3] = new Sprite(_pGuyBitmaps[3], rcBounds, BA_WRAP);  2: _pGuySprites[3]->SetNumFrames(4);  3: _pGuySprites[3]->SetPosition(500, 10);  4: _pGuySprites[3]->SetVelocity(-3, 0);  5: _pGuySprites[3]->SetZOrder(1);  6: _pGuySprites[3]->SetHidden(TRUE);  7: _pGame->AddSprite(_pGuySprites[3]); 

This code shows how easy it is to change a normal sprite into an animated sprite. A single call to the SetNumFrames() method is all it takes to inform the sprite that it is to use four frames of animation (line 2). It's important to note that the frame delay is not being set for this sprite, which means that it is cycling through the frames at maximum speed.

The second guy sprite is a little different in that it opts for a frame delay, as shown in Listing 17.8.

Listing 17.8 The Sprite Creation Code for the Second Moving Guy Establishes the Number of Frames and the Frame Delay for the Animated Sprite
 1: _pGuySprites[4] = new Sprite(_pGuyBitmaps[4], rcBounds, BA_WRAP);  2: _pGuySprites[4]->SetNumFrames(4);  3: _pGuySprites[4]->SetFrameDelay(5);  4: _pGuySprites[4]->SetPosition(260, 60);  5: _pGuySprites[4]->SetVelocity(5, 0);  6: _pGuySprites[4]->SetZOrder(1);  7: _pGuySprites[4]->SetHidden(TRUE);  8: _pGame->AddSprite(_pGuySprites[4]); 

This code not only sets the number of frames for the sprite (line 2), but it also sets the frame delay for the sprite (line 3). Setting the frame delay to 5 , as in this code, means that the sprite's animation frame will be updated in every fifth game cycle. In other words, the second guy sprite is animating at one-fifth the speed of the first sprite.

Testing the Finished Product

Testing the animated sprites in the Battle Office 2 game is not a very involved process, as you might imagine. Figure 17.3 shows the two guys moving across the hallway, and although it's difficult to see on the static page of a book, I promise you that they're kicking their legs like crazy!

Figure 17.3. The Battle Office 2 game shows off the new animated sprite features in the game engine.

graphics/17fig03.jpg

Granted, the new animated sprites in the Battle Office 2 game don't really impact the play of the game very much. However, it's still hard to argue that a little more playful realism can never hurt a game. In fact, anything you can do to make a game more interesting will ultimately add to the satisfaction of game players, and ultimately the success of the game.



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

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