XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
上QQ阅读APP看书,第一时间看更新

Sprites and sprite sheets

As far as XNA and the SpriteBatch classes are concerned, a Sprite is a 2D bitmapped image that can be drawn either with or without transparency information to the screen.

Tip

Sprites versus. textures

XNA defines a sprite as a 2D bitmap that is drawn directly to the screen. While these bitmaps are stored in Texture2D objects, the term texture is used when a 2D image is mapped onto a 3D object, providing a visual representation of the surface of the object. In practice, all XNA graphics are actually performed in 3D, with 2D sprites being rendered through special configurations of the XNA rendering engine.

The simple form of the SpriteBatch.Draw() call that you used in Chapter 1, Introducing XNA Game Studio, when drawing squares only needed two parameters: a Texture2D to draw a rectangle indicating where to draw it, and a Color to specify the tint to overlay onto the sprite.

Other overloads of the Draw() method, however, also allow you to specify a rectangle representing the source area within the Texture2D to copy from. If no source rectangle is specified, the entire Texture2D is copied and resized to fit the destination rectangle.

Tip

Overloads

When multiple versions of the same method are declared with either different parameters lists or different return values, each different declaration is called an overload of the method. Overloads allow methods to work with different types of data (for example, when setting a position, you could accept two separate X and Y coordinates or a Vector2 value), or leave out parameters that can then be assigned default values.

By specifying a source rectangle, however, individual pieces can be pulled from a large image. A bitmap with multiple sprites on it that will be drawn this way is called a sprite sheet.

The Tile_Sheet.png file for the Flood Control project is a sprite sheet containing 13 different sprites that will be used to represent the pieces of pipe used in the game. Each image is 40 pixels wide and 40 pixels high, with a one pixel border between each sprite and also around the entire image. When we call SpriteBatch.Draw(), we can limit what gets drawn from our texture to one of these 40x40 squares, allowing a single texture to hold all of the playing piece images that we need for the game:

Sprites and sprite sheets

The Tile_Sheet.png file was created with alpha-based transparency. When it is drawn to the screen, the alpha level of each pixel will be used to merge that pixel with any color that already occupies that location on the screen.

Using this fact, you can create sprites that do not look like they are rectangular. Internally, you will still be drawing rectangles, but visually, the image can be of any shape.

What we really need now to be able to work with the playing pieces is a way to reference an individual piece, knowing not only what to draw to the screen, but what ends of the pipe connect to adjacent squares on the game board.

Tip

Alpha blending

Each pixel in a sprite can be fully opaque, fully transparent, or partially transparent. Fully opaque pixels are drawn directly, while fully transparent pixels are not drawn at all, leaving whatever has already been drawn to that pixel on the screen unchanged. In the 32-bit color mode, each channel of a color (Red, Green, Blue, and Alpha) are represented by 8 bits, meaning that there are 256 different degrees of transparency between fully opaque (255) and fully transparent (0). Partially transparent pixels are combined with the current pixel color at that location to create a mixed color, as if the pixels below were being seen through the new color. As briefly mentioned in Chapter 1, Introducing XNA Game Studio, because XNA uses pre-multiplied alpha, simply creating a transparent color by specifying an Alpha component will not produce the results you might expect. To create a transparent color, create the color as a solid color and then multiply it by the desired alpha value between 0 (fully transparent) and 1 (fully opaque).