Game Development Reference
In-Depth Information
17 - Graphics and Animation
positions, colors, and directions.
31. blocks = [b1, b2, b3]
On line 31 we put all of these data structures in a list, and store the list in a variable
named
rectangles
.
rectangles
is a list.
rectangles[0]
would be the dictionary data structure in
r1
.
rectangles[0]['color']
would be the
'color'
key in
r1
(which we stored the
value in
RED
in), so the expression
rectangles[0]['color']
would evaluate to
(255, 0, 0)
. In this way we can refer to any of the values in any of the block data
structures by starting with
rectangles
.
Running the Game Loop
33. # run the game loop
34. while True:
Inside the game loop, we want to move all of the blocks around the screen in the
direction that they are going, then bounce the block if they have hit a wall, then draw all of
the blocks to the
windowSurface
surface, and finally call
pygame.display.update()
to draw the surface to the screen. Also, we will call
pygame.event.get()
to check if the
QUIT
event has been generated by the user
closing the window.
The
for
loop to check all of the events in the list returned by
pygame.event.get()
is the same as in our "Hello World!" program, so we will skip its explanation and go on to
line 44.
41. # draw the black background onto the surface
42. windowSurface.fill(BLACK)
Before we draw any of the blocks on the
windowSurface
surface, we want to fill the
entire surface with black so that anything we previously drew on the surface is covered.
Once we have blacked out the entire surface, we can redraw the blocks with the code
below.
Moving Each Block
44. for b in blocks: