Game Development Reference
In-Depth Information
132. baddieSize = random.randint(BADDIEMINSIZE,
BADDIEMAXSIZE)
133. newBaddie = {'rect': pygame.Rect
(random.randint(0, WINDOWWIDTH-baddieSize), 0 -
baddieSize, baddieSize, baddieSize),
134. 'speed': random.randint
(BADDIEMINSPEED, BADDIEMAXSPEED),
135. 'surface':pygame.transform.scale
(baddieImage, (baddieSize, baddieSize)),
136. }
When the baddieAddCounter reaches the value in ADDNEWBADDIERATE , then the
condition on line 130 is True and it is time to add a new baddie to the top of the screen.
First, the baddieAddCounter counter is reset back to 0 (otherwise, when it keeps
incrementing it will always be greater than ADDNEWBADDIERATE and never equal to it.
This will cause baddies to stop appearing at the top of the screen.)
Line 132 generates a size for the baddie in pixels. The size will be between
BADDIEMINSIZE and BADDIEMAXSIZE , which we have set to 10 and 40 in this
program.
Line 133 is where a new baddie data structure is created. Remember, the data structure for
baddies is simply a dictionary with keys 'rect' , 'speed' , and 'surface' . The
'rect' key holds a reference to a Rect object which stores the location and size of the
baddie. The call to the pygame.Rect() constructor function has four parameters: the X-
coordinate of the top edge of the area, the Y coordinate of the left edge of the area, the width
in pixels, and the height in pixels.
We want the baddie to appear randomly across the top of the window, so we pass
random.randint(0, WINDOWWIDTH-baddieSize) for the X-coordinate of the left
edge. This will evaluate to a random place across the top of the window. The reason we pass
WINDOWWIDTH-baddieSize instead of WINDOWWIDTH is because this value is for the
left edge of the baddie. If the left edge of the baddie is too far on the right side of the screen,
then part of the baddie will be off the edge of the window and not visible.
We want the bottom edge of the baddie to be just above the top edge of the window. The
Y-coordinate of the top edge of the window is 0, so to put the baddie's bottom edge there, we
want to set the top edge to 0 - baddieSize .
The baddie's width and height should be the same (the image is a square), so we will pass
baddieSize for the third and fourth argument.
The rate of speed that the baddie moves down the screen will be set in the 'speed' key,
and is set to a random integer between BADDIEMINSPEED and BADDIEMAXSPEED .
138. baddies.append(newBaddie)
Search WWH ::




Custom Search