Game Development Reference
In-Depth Information
53. gameOverSound = pygame.mixer.Sound('gameover.wav')
54. pygame.mixer.music.load('background.mid')
Next we want to create the Sound objects and also set up the background music. The
background music will constantly be playing during the game, but Sound objects will only
be played when we specifically want them to. In this case, the Sound object will be played
when the player loses the game.
You can use any .wav or .mid file for this game. You can download these sound files from
this topic's website at the URL http://inventwithpython.com/resources. Or you can use your
own sound files for this game, as long as they have the filenames of gameover.wav and
background.mid . (Or you can change the strings used on lines 53 and 54 to match the
filenames.)
The pygame.mixer.Sound() constructor function creates a new Sound object and
stores a reference to this object in the gameOverSound variable. In your own games, you
can create as many Sound objects as you like, each with a different sound file that it will
play.
The pygame.mixer.music.load() function loads a sound file to play for the
background music. This function does not create any objects, and only one sound file can be
loaded at a time.
56. # set up images
57. playerImage = pygame.image.load('player.png')
58. playerRect = playerImage.get_rect()
59. baddieImage = pygame.image.load('baddie.png')
Next we will load the image files that used for the player's character and the baddies on
the screen. The image for the character is stored in player.png and the image for the baddies
is stored in baddie.png . All the baddies look the same, so we only need one image file for
them. You can download these images from the topic's website at the URL
http://inventwithpython.com/resources.
Display the Start Screen
When the game first starts, we want to display the name of the game on the screen. We
also want to instruct the player that they can start the game by pushing any key. This screen
appears so that the player has time to get ready to start playing after running the program.
Also, before each game starts, we want to reset the value of the top score back to 0 .
61. # show the "Start" screen
62. drawText('Dodger', font, windowSurface, (WINDOWWIDTH / 3),
(WINDOWHEIGHT / 3))
63. drawText('Press a key to start.', font, windowSurface,
Search WWH ::




Custom Search