Retrieving Events

In the earlier example, we called pygame.event.get() to retrieve all the events and remove them from the queue, which is like opening the door and letting everyone in. This is probably the best way to deal with events, as it ensures we have handled everything before we go on to draw something to the screen but there are other ways to work with the event queue. If you call pygame.event.wait(), Pygame will wait for an event to occur before it returns, which is like waiting by the door until...

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...

Seeing Models in Action

Let's write a class to load a Wavefront OBJ file and render it with OpenGL. I have created a model of a futuristic tank see Figure 11-5 , which was built with AC3D www.inivis.com and exported it as mytank.obj and mytank.mtl. My artistic skills are limited feel free to replace my model with your own 3D object. You can use any 3D modeler software that has the capability to export OBJ files. Figure 11-5. Futuristic tank object in AC3D Figure 11-5. Futuristic tank object in AC3D The class we will...

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...

Perspective Projections

A far more common projection in games and 3D computer graphics in general is the perspective projection, because it takes into account the distance of an object from the viewer. A perspective projection replicates the way that objects farther from the viewer appear smaller than objects close up. Objects rendered with a perspective projection will also appear to narrow toward the horizon, an effect known as foreshortening see Figure 8-5 . Listing 8-6 is a function that projects a 3D coordinate...

Texture Parameters

Pygame Tutorial

OpenGL is very flexible in the way it renders 3D scenes. There are a number of texture parameters that can be set to create visual effects and adjust how textures are used to render polygons. This section covers some commonly used texture parameters the full list is beyond the scope of this book, but you can read the OpenGL documentation online www.opengl.org sdk docs man for all the details. In Listing 11-2 we set two texture parameters, GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAX_FILTER, which...

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...