Understanding the Types of Game Backgrounds


Now that you have a basic understanding of how backgrounds fit into games , as well as why they are important, I'd like to spend a moment exploring the basic kinds of backgrounds that you'll encounter in game programming. Following are the three primary types of backgrounds:

  • Solid backgrounds

  • Image backgrounds

  • Animated backgrounds

  • Scrolling backgrounds

The next few sections explore these different types of backgrounds in more detail.

Solid Backgrounds

A solid background is a background consisting only of a solid color . This is by far the simplest of all backgrounds, and requires very little code to pull off. Not surprisingly, solid backgrounds are also the dullest of all backgrounds, and aren't used very often in games. Generally speaking, any time you could use a solid background, you could also use an image background and get a much better result. For example, if you were developing a football game, it might make sense to create a solid green background to represent the grass on the field. However, a grassy textured image would look much better in the same context, without a whole lot more development effort. So, although it's important to have the option of using a solid background, it's usually not your best option.

graphics/book.gif

One situation in which you might find a solid background useful is in testing a game. For example, if you have a game that has a complex background image in which the sprites tend to blend in well, you might find it easier to fine-tune the sprites while viewing them against a stark background. By temporarily replacing a background image with a solid background of a contrasting color, you can easily make out what the sprites are doing and fix any problems with them.


Image Backgrounds

Image backgrounds are a step beyond solid backgrounds in that they use a bitmap image to represent the backdrop for a game, as opposed to a solid color. You've already used image backgrounds in the games you've developed throughout the book, so hopefully you can appreciate their usefulness . The primary work involved in using an image background is creating the image itself; from a programming perspective, the Bitmap class in the game engine already handles most of the details of using an image background.

The problem with image bitmaps is that they are static, which means that they don't move or do anything interesting to convey that they are real. For example, the forest image background used in the Fore example from Hour 10, "Making Things Move with Sprites," would have been infinitely more realistic if the trees swayed a little and maybe dropped a leaf every now and then. Of course, such subtleties aren't always easy to add to a game, and you have to carefully weigh adding complexity for the sake of including small details in a game ”sometimes it's worth it, sometimes not. One way to improve on the basic image background is to use an animated background.

Animated Backgrounds

An animated background is a background that somehow changes its appearance over time, which is similar in some ways to an animated sprite. However, an animated background doesn't necessarily have to involve a series of frame images. It's entirely possible to create an animated background whose animation is derived from custom code. The key to making an animated background work is providing some kind of mechanism for updating and drawing the background. Again, this is similar to sprites in that they must be updated periodically to convey a sense of motion.

Another reason why I hesitate to draw too close of a comparison between an animated background and an animated sprite is because backgrounds are usually much larger than sprites. Therefore, using a series of frame images for a background could quickly hog a lot of memory when you consider that backgrounds are typically hundreds of pixels in width and height. It is much more efficient to focus on coming up with interesting and more efficient ways of animating backgrounds. For example, the starry background I mentioned earlier could be created by changing the color of individual pixels in a background image.

Another approach to creating an animated background involves using several smaller images that are capable of moving around on the background. For example, if you wanted to enhance the starry background, you could feasibly add a few planets that are animated on the background by slightly changing their appearance every so often. It's debatable whether you should create these planets as sprites, as opposed to creating them as part of the background, but these are the kinds of decisions you have to make when creating a game.

Scrolling Backgrounds

The final type of background is the scrolling background , which involves an image or set of graphical objects that are capable of being shifted, or scrolled around on the screen. The best way to understand a scrolling background is to picture a background that is much larger than the game screen, which means that the game screen provides a view onto part of the background. To see other parts of the background, you have to scroll the view to another part. Figure 18.1 shows what I mean by a "view on a background."

Figure 18.1. When you use a scrolling background, the game screen provides a view on a portion of the background.

graphics/18fig01.gif

The figure shows how the game screen shows only a portion of a larger background. This type of background is used a lot in adventure games in which you control a character around a large virtual world. As you might have guessed, scrolling backgrounds are considerably more complex to develop than the other types of backgrounds because they involve a lot more game logic. For example, the background must respond to you moving the player, not to mention the fact that sprites have to be moved so that they appear to scroll with the background. Not only that, but scrolling backgrounds often must be capable of wrapping so that you don't encounter a boundary.

graphics/book.gif

An interesting type of scrolling background that is commonly used in 2D games is a parallax scrolling background, which is a background that scrolls at differing rates. Parallax scrolling involves the use of multiple layered background images, such as buildings in the foreground, trees in the mid-ground, and mountains in the background. The idea is to provide the illusion of depth by moving each image at a different speed. So, the mountains move slowest because they are furthest away, while the trees move a little faster, and the buildings move fastest . This simulates the effect of movement we perceive in the real world when passing by objects that are at different distances.


Creating a scrolling background certainly isn't impossible , but it is a formidable challenge when you approach it from the perspective of still learning the ropes of game development. Therefore, you won't be tackling scrolling backgrounds in this book. However, you do learn how to create animated backgrounds, which I think you will find to be plenty interesting for adding some pizzazz to your games.



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