Game Development Reference
In-Depth Information
playerStretchedImage
. Stretching an image often distorts it a little. If we keep
restretching a stretched image over and over, the distortions add up quickly. But by
stretching the original image to the new size, we only distort the image once. This is why
we pass
playerImage
as the first argument for
pygame.transform.scale()
.
Draw the Cherries on the Window
121. # draw the food
122. for food in foods:
123. windowSurface.blit(foodImage, food)
In our previous programs, we called the
pygame.draw.rect()
function to draw a
green square for each
Rect
object stored in the
foods
list. However, in this program we
want to draw the cherry sprites instead. We will call the
blit()
method and pass the
Surface
object stored in
foodImage
. (This is the surface that has the image of cherries
drawn on it.)
We only use the
food
variable (which contains each of the
Rect
objects in
foods
on
each iteration through the
for
loop) to tell the
blit()
method where to draw the
foodImage
.
Summary: Games with Graphics and Sounds
This game has added even more advanced graphics and introduced using sound in our
games. The images (called sprites) look much better than the simple drawing primitives
used in our previous programs. The game presented in this chapter also has music playing
in the background while also playing sound effects.
Sprites can be scaled (that is, stretched) to a larger or smaller size. This way we can
display sprites at any size we want. This will come in handy in the game presented in the
next chapter.
Now that we know how to create a GUI window, display sprites and drawing primitives,
collect keyboard and mouse input, play sounds, and implement collision detection, we are
now ready to create a graphical game in Pygame. The next chapter brings all of these
elements together for our most advanced game yet.