Game Development Reference
In-Depth Information
20 - Dodger
Drawing the Player's Character
174. # Draw the player's rectangle
175. windowSurface.blit(playerImage, playerRect)
Remember that the information about the player is kept in two different variables.
playerImage
is a
Surface
object that contains all the colored pixels that make up the
player's character's image.
playerRect
is a
Rect
object that stores the information
about the size and location of the player's character.
We call the
blit()
method on
windowSurface
and pass
playerImage
and
playerRect
. This draws the player character's image on
windowSurface
at the
appropriate location.
177. # Draw each baddie
178. for b in baddies:
179. windowSurface.blit(b['surface'], b['rect'])
We use a
for
loop here to draw every baddie on the
windowSurface
object.
Remember that each item in the
baddies
list is a dictionary with
'surface'
and
'rect'
keys containing the
Surface
object with the baddie image and the
Rect
object
with the position and size information, respectively.
181. pygame.display.update()
Now that we have finished drawing everything to the
windowSurface
object, we
should draw this surface to the screen with a call to
pygame.display.update()
.
Collision Detection
183. # Check if any of the baddies have hit the
player.
184. if playerHasHitBaddie(playerRect, baddies):
185. if score > topScore:
186. topScore = score # set new top score
187. break
Now let's check if the player has collided with any of the baddies. We already wrote a
function to check for this:
playerHasHitBaddie()
. This function will return
True
if
the player's character has collided with any of the baddies in the
baddies
list. Otherwise,
the function will return
False
.