Hardware Reference
In-Depth Information
Playing sounds using the pygame module
Since the sound effects form the crux of an arcade game, we need to know how to play
sounds using the
pygame
plaform.
1.
We will declare a sound object to play the
beeps.wav
file, available along with
the downloads of this file. The file needs to be copied into the same directory
as the python script that runs the game as follows:
soundObj = pygame.mixer.Sound('beeps.wav')
2.
The sound can be played in a loop by calling the
play()
method as follows:
while True: # main game loop
soundObj.play()
3.
We can stop playing the sound file by calling the
stop()
method.
Building menus using the pygame module
1. We will need a simple menu for our desktop game. We will make use of the
menu
class
(distributed under GNU GPL v3 license) writen by
Scot Barlow
. Let's review
MenuClass-12
60-.html
.)
2. In this menu example, a menu object is created; when a menu opion is selected
using a keyboard, the selected opion is highlighted and printed on a terminal when
the return key is pressed. For example, when the down key is pressed to select
Load
Game
and the return key pressed, the Load Game opion is printed to the terminal.
3.
Let's discuss the parameters that need to be passed as arguments to create
a menu object. According to the documentaion available with the menu class,
a menu object has to be defined with the following parameters:
menu = cMenu(x, y, h_pad, v_pad, orientation, number,
background,buttonList)
The parameters
x
and
y
refer to the location of the origin of the menu
object on the game screen
The parameters
h_pad
and
v_pad
refer to the spacing between the
buttons in the horizontal and vertical directions
The
orientation
parameter refers to the arrangement of the buttons
on the screen, that is,
'horizontal'
or
'vertical'
The
number
parameter refers to the number of buttons that can be
accommodated in a single row (when arranged horizontally) or in a
single column (when arranged vertically)