Game Development Reference
In-Depth Information
Chapter 19
Sprite Sheets
19.1 Introduction
In this chapter, we will start building the first elements of the
Penguin Pairs
game.
This game is quite a bit more complicated than the previous games. You can already
see this by looking at the number of game assets that this game uses. Just like we
did in the
Jewel Jam
game, we are going to use images containing several different
sprites. This is actually a technique used in many games. In the
Jewel Jam
game,
we used this to store a strip of jewels in a single sprite. However, using strips is
not always a good idea. Especially when an image contains many different sprites
in a strip, the strip may become too long for the graphics hardware to handle. This
can be solved by storing sprites in a
sheet
instead, consisting of multiple rows and
columns. For an example of such a sprite sheet, see Fig.
19.1
.
19.2 Overview of the Example Project
In order to test the loading and displaying of a sheet of sprites, we have created a
simple example project called
PenguinPairs1
. In this example project, we are going
to show a background, and then draw a sprite from the spritesheet on top of the
background. Using the left and right arrows keys, you can then select which sprite
from the sheet should be shown.
To get started more easily, we copy a few classes from the previous game. First,
we are going to need the
GameObject
class and the
GameObjectList
class. We are also
going to need the
AssetManager
class for loading the sprites, and the
InputHelper
class
for dealing with input. We will then write a new
SpriteGameObject
class, which will
have the capability for reading sheets of sprites.
The
PenguinPairs
class inherits from the
Game
class, and we do all our initializa-
tion (creating a graphics device and a sprite batch, and more) in there. We add a
member variable
gameWorld
of type
GameObjectList
. This variable is initialized in the