Game Development Reference
In-Depth Information
If the player's character has hit a baddie, then we check if the player's current score is
greater than the top score. If it is, we set the new top score to be the player's current score.
Either way, we break out of the game loop. The program's execution will jump down to line
191.
189. mainClock.tick(FPS)
To keep the computer from running through the game loop as fast as possible (which
would be much too fast for the player to keep up with), we call
mainClock.tick()
to
pause for a brief amount of time. The pause will be long enough to ensure that about
40
(the value we stored inside the
FPS
variable) iterations through the game loop occur each
second.
The Game Over Screen
191. # Stop the game and show the "Game Over" screen.
192. pygame.mixer.music.stop()
193. gameOverSound.play()
When the player loses, we want to stop playing the background music and play the
"game over" sound effect. We call the
stop()
function in the
pygame.mixer.music
module to stop the background music. Then we call the
play()
method on the
Sound
object stored in
gameOverSound
.
195. drawText('GAME OVER', font, windowSurface,
(WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
196. drawText('Press a key to play again.', font,
windowSurface, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3)
+ 50)
197. pygame.display.update()
198. waitForPlayerToPressKey()
Now we want to display text on the window to tell the player that the game is over, and
they should press a key to start playing a new game. The two calls to our
drawText()
function will draw this text to the
windowSurface
object, and the call to
pygame.display.update()
will draw this
Surface
object to the screen.
After displaying this text, we want the game to stop until the player presses a key, so we
call our
waitForPlayerToPressKey()
function.
200. gameOverSound.stop()