Game Development Reference
In-Depth Information
The
blit()
Method for
Surface
Objects
52. # draw the text onto the surface
53. windowSurface.blit(text, textRect)
The
blit()
method will draw the contents of one
Surface
object onto another
Surface
object. Line 54 will draw the "Hello world!" text (which was drawn on the
Surface
object stored in the
text
variable) and draws it to the
Surface
object stored
in the
windowSurface
variable.
Remember that the
text
object had the "Hello world!" text drawn on it on line 22 by
the
render()
method.
Surface
objects are just stored in the computer's memory (like
any other variable) and not drawn on the screen. The
Surface
object in
windowSurface
is drawn on the screen (when we call the
pygame.display.update()
function on line 56 below) because this was the
Surface
object created by the
pygame.display
.set_mode() function.
The second parameter to
blit()
specifies where on the
windowSurface
surface the
text
surface should be drawn. We will just pass the
Rect
object we got from calling
text.get_rect()
(which was stored in
textRect
on line 23).
The pygame.display.update() Function
55. # draw the window onto the screen
56. pygame.display.update()
In Pygame, nothing is drawn to the screen until the
pygame.display.update()
function is called. This is done because drawing to the screen is a slow operation for the
computer compared to drawing on the
Surface
objects while they are in memory. You do
not want to draw to the screen after each drawing function is called, but only draw the
screen once after all the drawing functions have been called.
You will need to call
pygame.display.update()
each time you want to update
the screen to display the contents of the
Surface
object returned by
pygame.display.set_mode()
. (In this program, that object is the one stored in
windowSurface
.) This will become more important in our next program which covers
animation.
Events and the Game Loop
In our previous games, all of the programs print out everything immediately until they
reach a
input()
function call. At that point, the program stops and waits for the user to