Game Development Reference
In-Depth Information
Review of the Basic Pygame Data Types
Let's review some of the basic data types used in the Pygame library:
— pygame.Rect - Rect objects represent a rectangular space's location and size.
The location can be determined by the Rect object's topleft attribute (or the
topright , bottomleft , and bottomright attributes). These corner attributes
are a tuple of integers for the X and Y coordinates. The size can be determined by the
width and height attributes, which are integers of how many pixels long or high the
rectangle area is. Rect objects have a colliderect() method to check if they
are intersecting with another Rect object.
— pygame.Surface - Surface objects are areas of colored pixels. Surface
objects represent a rectangular image, while Rect objects only represent a
rectangular space and location. Surface objects have a blit() method that is
used to draw the image on one Surface object onto another Surface object. The
Surface object returned by the pygame.display.set_mode() function is
special because anything drawn on that Surface object will be displayed on the user's
screen.
— Remember that Surface have things drawn on them, but we cannot see this
because it only exists in the computer's memory. We can only see a Surface object
when it is "blitted" (that is, drawn) on the screen. This is just the same as it is with
any other piece of data. If you think about it, you cannot see the string that is stored
in a variable until the variable is printed to the screen.
— pygame.event.Event - The Event data type in the pygame.event module
generates Event objects whenever the user provides keyboard, mouse, or another
kind of input. The pygame.event.get() function returns a list of Event
objects. You can check what type of event the Event object is by checking its type
attribute. QUIT , KEYDOWN , and MOUSEBUTTONUP are examples of some event
types.
— pygame.font.Font - The pygame.font module has the Font data type
which represent the typeface used for text in Pygame. You can create a Font object
by calling the pygame.font.SysFont() constructor function. The arguments to
pass are a string of the font name and an integer of the font size, however it is
common to pass None for the font name to get the default system font. For example,
the common function call to create a Font object is pygame.font.SysFont
(None, 48) .
— pygame.time.Clock - The Clock object in the pygame.time module are
very helpful for keeping our games from running as fast as possible. (This is often
too fast for the player to keep up with the computer, and makes the games not fun.)
The Clock object has a tick() method, which we pass how many frames per
second (fps) we want the game to run at. The higher the fps, the faster the game runs.
Normally we use 40 fps. Notice that the pygame.time module is a different
module than the time module which contains the sleep() function.
Type in the following code and save it to a file named dodger.py . This game also
Search WWH ::




Custom Search