Game Development Reference
In-Depth Information
Functions that have the same name as their data type and create objects or values of this
data type are called constructor functions .
The int() and str() functions are also constructor functions. The int() function
returns an int version of whatever you pass it, whether it is int(5) or int('5') . (The
proper name for strings in Python is str .)
You can always find out what the proper name of a value's data type with the type()
function. For example, try typing the following into the interactive shell:
>>> type('This is a string')
<type 'str'>
>>> type(5)
<type 'int'>
>>> spam = 'Another string'
>>> type(spam)
<type 'str'>
>>> import pygame
>>> pygame.init()
>>> myRect = pygame.Rect(10, 10, 40, 50)
>>> type(myRect)
<type 'pygame.Rect'>
>>> pygame.quit()
(You need to call the pygame.quit() function when you are done with typing
Pygame functions into the interactive shell. Otherwise you may cause Python to crash.)
Notice that the return value from the type() function is not a string, but a value of a data
type called "type"! Try typing this into the interactive shell:
>>> type(type('This is a string'))
<type 'type'>
For the most part, you don't need to know about data types and the type() function
when programming games. But it can be very useful if you need to find out the data type of
the value stored in a variable in your program.
The fill() Method for Surface Objects
27. # draw the white background onto the surface
28. windowSurface.fill(WHITE)
This is the first drawing function call in our program. We want to fill the entire surface
Search WWH ::




Custom Search