Designing an All-Purpose Sprite


As you know by now, the primary purpose of a sprite is to model a graphical object in a game that is capable of moving over time. It takes several pieces of information to manage such an object. The following list reveals the specific properties of a sprite that must be accounted for in the Sprite class:

  • Position

  • Velocity

  • Z-order

  • Bounding rectangle

  • Bounds action

  • Hidden/visible

The most important property of a sprite is its position on the game screen, followed by its velocity. The idea is that the velocity of a sprite will be used in each cycle of the game to change the sprite's position. So, if a sprite has an X velocity of 1 and a Y velocity of -2 , it will move 1 pixel to the right and 2 pixels down the game screen in every game cycle. Obviously, setting higher values for the velocity of a sprite makes it move faster.

Although it's logical to think of a sprite's position in terms of a single coordinate, typically the upper left corner of the sprite, it's actually more useful from a programming perspective to keep track of a rectangular position for the sprite. In other words, the position of a sprite is a rectangle that basically outlines the sprite as it appears on the game screen. This allows you to factor in the width and height of a sprite when you work with its position.

In addition to position and velocity, it is also helpful to assign a Z-order to every sprite in a game. If you recall from the previous hour, Z-order is the depth of a sprite with respect to the screen. If two sprites are sharing the same space on the screen, the sprite with the higher Z-order will appear to be on top of the other sprite. The neat thing about Z-order is that it isn't as difficult to develop as you might think. When you think about it, sprites are naturally drawn on top of each other if you draw them in the proper order. So, establishing the Z-order for a system of sprites is all about ordering the drawing of the sprites so that sprites with a higher Z-order are drawn last. You don't actually learn how to write code to handle Z-order until Hour 11, "Managing a World of Sprites," but you're going to go ahead and build Z-order into the Sprite class in this hour so that it will be ready.

A less obvious sprite property that is very useful is a bounding rectangle , which is a rectangle that determines the area in which a sprite can travel. Generally speaking, the bounding rectangle would be assumed to be the entire game screen, but there are situations in which you might want to limit the movement of a sprite to a smaller area. For example, maybe you've created a bee sprite that you want to see buzzing around a flower. You could easily accomplish this by setting the bounding rectangle for the bee to be a rectangle that encompasses the flower. Taking the bounding rectangle concept a step further, you can also establish bounds actions , which determine how a sprite acts when it encounters a boundary. More specifically , in a billiards game you would probably want the balls to bounce when they hit the pool table boundary. On the other hand, in an Asteroids type game you would probably want the asteroids to wrap around the edges of the game screen boundary. There are four primary bounds actions worth considering for any given sprite: Stop, Wrap, Bounce, and Die. You'll learn about these bounds actions in more detail in a moment, but for now just understand that they determine what happens to a sprite when it encounters a boundary such as the edge of the game screen.

The last sprite property worth considering in the design of a sprite class is the sprite's visibility. Although you could certainly delete a sprite from memory in order to hide it from view, there are situations in which it is better to simply hide a sprite rather than deleting it. As an example, you might want to create a game along the lines of the traditional carnival Whack-A-Mole game in which plastic moles pop-up out of holes and you smash them with a mallet. In this type of game, you would only need to hide the mole sprites after they are hit (clicked with the mouse), as opposed to deleting them from memory and recreating them over and over. For this reason, it is helpful to have a property that determines whether a sprite is hidden.

Although you've learned about several different sprite properties, my goal isn't to explore every property a sprite might ever need; it's just a starting point. In fact, you'll be adding to the Sprite class over the course of the book, so you can think of this as version one of your sprite design. In fact, you will hopefully add to the Sprite class and make your own enhancements after you finish the book.



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