Game Development Reference
In-Depth Information
17 - Graphics and Animation
another advanced option called the color depth. You can just pass the value 32.
The
set_caption()
call returns a
pygame.Surface
object (which we will call
Surface
objects for short). Objects are values of a data type that have methods as well as
data. For example, strings are objects in Python because they have data (the string itself)
and methods (such as
lower()
and
split()
). You can store objects in variables just
like any other value. The
Surface
object represents the window and we will include the
windowSurface
variable in all of our calls to drawing functions.
Variables Store References to Objects
You should know that variables never hold objects (including lists and dictionaries), they
only hold references to objects. This is exactly the same way that variables never hold lists
but only hold references to lists. The difference between holding the object and holding a
reference to the object is that if you copy the variable to a second variable, any changes
made to object in one of the variables will also change the object in the other variable. This
is because both variables hold references to the same object because only a copy of the
reference was made, not a copy of the object.
Here is an example with lists (just like in the Hangman chapter). Type the following into
the interactive shell:
>>> x = ['a', 'b', 'c']
>>> y = x
>>> x[2] = 'Hello!'
>>> print(y)
['a', 'b', 'Hello!']
Notice that changing the
x
list has also changed the
y
list, because they both contain
references to the same list.
y
made a copy of the reference in
x
, not a copy of the list.
The same applies to objects. Consider the following code:
>>> import pygame
>>> pygame.init()
>>> windowSurface = pygame.display.set_mode((500,
500), 0, 32)
>>> secondSurface = windowSurface
windowSurface
and
secondSurface
contain references to the same
Surface
object. Any changes made to
windowSurface
will change the same object that
secondSurface
references. The same is true that any changes to
windowSurface
will change
windowSurface
.