Game Development Reference
In-Depth Information
If this condition is
True
, then the we will remove the baddie data structure from the
baddies list.
Drawing the Window
It isn't enough that our game updates the state of the game world in its memory. Our
program will also have to display the game world to the player. We can do this by drawing
the graphics of the baddies and player's character on the screen. Because the game loop is
executed several times a second, drawing the baddies and player in new positions makes
their movement look smooth and natural. But every element on the screen must be drawn
one at a time by calling the appropriate Pygame function.
167. # Draw the game world on the window.
168. windowSurface.fill(BACKGROUNDCOLOR)
Now that we have updated all the data structures for the baddies and the player's
character, let's draw everything on the screen. First, before we draw anything else on the
Surface
object referred to by
windowSurface
, we want to black out the entire screen
to erase anything drawn on it in a previous iteration through the game loop.
Remember that the
Surface
object in
windowSurface
is the special
Surface
object because it was the one returned by
pygame.display.set_mode()
. This means
that anything drawn on that
Surface
object will appear on the screen, but only after the
pygame.display.update()
function is called.
Drawing the Player's Score
170. # Draw the score and top score.
171. drawText('Score: %s' % (score), font,
windowSurface, 10, 0)
172. drawText('Top Score: %s' % (topScore), font,
windowSurface, 10, 40)
Next we will render the text for score and top score to the top left corner of the window.
The
'Score: %s' % (score)
uses string interpolation to insert the value in the score
variable into the string. This is the same thing as
'Score: ' + str(score)
. We pass
this string, the
Font
object stored in the
font
variable, the
Surface
object on which to
draw the text on, and the X and Y coordinates of where the text should be placed.
Remember that our
drawText()
will handle the call to the
render()
and
blit()
methods.
For the top score, we do the exact same thing. We pass
40
for the Y-coordinate instead
of
0
(like we do for the score) so that the top score text appears beneath the score text.