Hello World Revisited

As I mentioned in Chapter 1, there is a tradition when learning new languages that the first code you write displays the text Hello, World on the screen. Technically we have already done this with a print ' Hello, World ' statement but it is a little disappointing because as game programmers we are interested in creating appealing visuals and a line of text just does not cut it We are going to create a Hello World script with Pygame that opens a graphical window on your desktop and draws an...

Building the Brains

Game Pygame

Each ant is going to have four states in its state machine, which should be enough to simulate ant-like behavior. The first step in defining the state machine is to work out what each state should do, which are the actions for the state see Table 7-1 . Table 7-1. Actions for the Ant States Table 7-1. Actions for the Ant States Walk toward a random point in the world. We also need to define the links that connect states together. These take the form of a condition and the name of the state to...

Windows with No Borders

Generally when you create a Pygame window you will want a standard window with title bars and border. It is possible, though, to create a window that doesn't have these features so that the user will not be able to move or resize the window, or close it via the close button. One instance of such a use is the window used for splash screens. Some games can take a while to load because they contain many image and sound files. If there is nothing visible on the screen while this is happening, the...

Rotational Movement with the Mouse

You have seen that drawing a mouse cursor on the screen is quite straightforward you simply need to get the coordinates of the mouse from a MOUSEMOTION event or directly from the pygame.mouse.get_pos function. Either method is fine if you just want to display a mouse cursor, but mouse movement can also be used to control something other than an absolute position, such as rotating or looking up and down in a 3D game. In this case, we can't use the mouse position directly because the coordinates...

Blending Colors

Something else you may want to do with colors is blend one color gradually into another. Let's say we have a zombie in a horror game that is normally a sickly shade of zombie green but has recently emerged from a lava pit and is currently glowing a bright shade of fireball orange. Over time the zombie will cool down and return to its usual color. But how do we calculate the intermediate colors to make the transition look smooth We can use something called linear interpolation, which is a fancy...

Field of View

Field View Distance

So how do we select a good value for the viewing distance d We could just experiment to find a value that makes the 3D scene look convincing, but we can take the guesswork out of it by calculating d from the field of view fov , which is the angular range of the scene that is visible at one moment. For human beings, the fov is the range from the left eye to the right eye, which is about 180 degrees. Figure 8-7 shows the relationship between fov and viewing distance. When the fov angle increases...