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:
The next few sections explore these different types of backgrounds in more detail. Solid BackgroundsA 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.
Image BackgroundsImage 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 BackgroundsAn 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 BackgroundsThe 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.
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.
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. |