20. Sound Effects¶
Adding sound to your game isn’t too hard. There are two steps:
Load the sound
Play the sound
For these examples, I’m using a sound file called laser.ogg
that you can
download here. Make sure you save the file in
the same directory as any Python program that tries to use it.
20.1. Loading Sounds¶
The code below creates a new variable called laser_sound
. It calls
arcades load_sound
function. It passes the filename of our sound.
laser_sound = arcade.load_sound("laser.ogg")
For this to work, you need to have a sound downloaded and named laser.ogg
in the same directory as your Python file. The computer will not find the
sound if it is in a different directory.
This loads the sound, but does not play it. We only want to load the sound once. We don’t want to load the sound off the disk every time we play it. It can be kept in memory.
20.2. Playing Sounds¶
The code to play sounds is straight-forward. Just call the play_sound
function, and pass in the variable that we set equal to the sound we loaded:
arcade.play_sound(laser_sound)
Putting the two together, you might think we could do this to play sounds:
import arcade
laser_sound = arcade.load_sound("laser.ogg")
arcade.play_sound(laser_sound)
But that doesn’t work. The program ends before the sound has a chance to play.
The play_sound
button doesn’t pause while the sound plays, it returns
right away and keeps going. This was a similar issue that we had when we opened
a window, and we can solve it the same way:
1 2 3 4 5 6 | import arcade
arcade.open_window(300, 300, "Sound Demo")
laser_sound = arcade.load_sound("laser.ogg")
arcade.play_sound(laser_sound)
arcade.run()
|
For this reason, I recommend loading the sound in
the __init__
method of the class that will play the sound.
20.3. Triggering Sounds¶
We want to play the sound when something happens. So this example loads the
sound once during the __init__
. When the user hits the space bar, that
is when we trigger the sound to play.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import arcade
class MyApplication(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height, "Trigger Sound With Key")
# Load the sound when the application starts
self.laser_sound = arcade.load_sound("laser.ogg")
def on_key_press(self, key, modifiers):
# If the user hits the space bar, play the sound that we loaded
if key == arcade.key.SPACE:
arcade.play_sound(self.laser_sound)
def main():
window = MyApplication(300, 300)
arcade.run()
main()
|
20.4. Finding Sounds¶
Great places to find free sounds to use in your program:
www.freesound.org is ok, but requires a login, which is a pain.
Kenney.nl has a few free sound packs you can download, and several that are cheap.
20.5. Sound File Formats¶
There are several types of sound file formats that you can find sounds in:
.wav
- This is a raw “wave” format. The sound is not compressed at all. You do not need a special library to decompress the sound, but the sound file itself can be rather large..mp3
- MPEG Layer III Audio (mp3) is one of the most popular compressed sound file formats. This file format is what enabled on-line music to become popular. However some of the compression algorithms have patents on them, making it not as suitable for free software..m4a
- Apple’s file format for compressed, but unprotected audio..ogg
- A open-source sound file that uses Ogg-Vorbis for compression. A less popular but patent-free method of sound storage.
Arcade should be able to play files in either the mp3 or ogg format. If you have issues getting it to work, try converting the sound to a raw wav format.
If you need to convert file formats, or trim a sound file, I suggest using the program Audacity.