Every computer program ever written is about processing data. As a result, when you design your game program structure, you must specify what data the program processes and what processing operations it performs. This two-step process tells you how to write your program. To specify the data your program processes, you design the program's objects. Specifying the operations the program performs involves designing the member functions for all of the objects. Designing Game ObjectsSoftware objects were introduced in chapter 3, "Introducing Object-Oriented Programming." In general, figuring out what objects a game needs is pretty straightforward. Essentially, you create objects for the things you see on the screen. In fact, writing games is one of the best ways to learn object-oriented design because you can generally see the objects you're designing. Figure 7.1 shows a screen shot of the game Ping, which we'll write later in this chapter. Ping provides good practice for learning game software design. Figure 7.1. Ping requires objects for the ball and the paddle.
The Ping game shown in this figure is a simple simulation of ping-pong or table tennis. Players use the paddles at the left and right edges of the screen to hit the ball back and forth. As you can see by looking at this screenshot, it's very intuitive that the game requires at least two types of objects in the software: one to represent the ball and another to represent the paddles. Note You'll find the files for Ping on the CD in the folder \Source\Chapter07\Prog_07_01. There's a compiled executable version of Ping that you can run in the \Source\Chapter07\Bin folder. After you figure out what objects a game needs, you must design the objects themselves. Specifically, you design your objects identifying the important characteristics or attributes of the object. For example, the ball in Figure 7.1 would require an object that has a position, a height, and width, and a vector describing the ball's movement, and a bounding rectangle. chapter 4, "Introducing the LlamaWorks2D Game Engine," demonstrated the use of such an object by introducing the llamaworks2d::sprite class. Obviously there is a lot more to designing objects than I've explained in these few paragraphs. In fact, entire books have been written about this topic. However, this short introduction should be enough to get you started. After you finish this book, it's a good idea to invest in a book on object-oriented design. You'll find a few such books in the Suggested Reading list on the CD. Designing Game TasksAlthough each game you write is unique, nearly every game performs the same set of basic tasks, which are listed below. After you've written code for these tasks for a couple of games, you will become very proficient at it. As a result, you'll be able to rapidly move into the tasks that are unique to the game you're working on. Factoid Game programmers have various terms for the message loop. They may call it the update loop, the rendering loop, the main game loop, or the game loop.
The example game Ping will demonstrate how to handle all these tasks. |