Game Development Reference
In-Depth Information
drawn on it. This is the Surface object that will be blitted to the Surface object
returned by pygame.display.set_mode() and drawn on the screen.
Next, we want to reset the player's score to 0 .
73. playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT -
50)
The starting location of the player will be in the center of the screen and 50 pixels up
from the bottom. The tuple that we set the topleft attribute to will change the location of
the playerRect object. The first item in the tuple is the X-coordinate of the left edge.
The second item in the tuple is the Y coordinate of the top edge.
74. moveLeft = moveRight = moveUp = moveDown = False
75. reverseCheat = slowCheat = False
76. baddieAddCounter = 0
Also at the start of the game, we want to have the movement variables moveLeft ,
moveRight , moveUp , and moveDown set to False . The reverseCheat and
slowCheat variables will be set to True only when the player enables these cheats by
holding down the "z" and "x" keys, respectively.
The baddieAddCounter variable is used for a counter to tell the program when to
add a new baddie at the top of the screen. The value in baddieAddCounter will be
incremented by one each time the game loop iterates. When the baddieAddCounter
counter is equal to the value in ADDNEWBADDIERATE , then the baddieAddCounter
counter is reset back to 0 and a new baddie is added to the top of the screen.
77. pygame.mixer.music.play(-1, 0.0)
At the start of the game, we want the background music to begin playing. We can do this
with a call to pygame.mixer.music.play() . The first argument is the number of
times the music should repeat itself. -1 is a special value that tells Pygame we want the
music to repeat endlessly. The second argument is a float that says how many seconds into
the music we want it to start playing. Passing 0.0 means we want to play the music
starting from the beginning of the music file. (Passing 2.0 , for example, would have
started the music two seconds into the music file.)
The Game Loop
The game loop contains the code that is executed while the game is being played. The
game loop constantly updates the state of the game world by changing the position of the
player and baddies, handling events generated by Pygame, and drawing the state of the
Search WWH ::




Custom Search