Pygame modules for interactive programs
Background Music
The pygame.mixer.music
module provides additional sound capabilities. Unlike pygame.Sound
objects, which load the entire clip into memory pygame.mixer.music
, streams files from the disc, only loading what it needs at the current moment. Pygame only supports one music track at a time.
To load background music, you can call pygame.mixer.music.load()
, where the argument is the file name. Note that this is not assigned to a variable but is loaded into the module itself (since it can only support one file). The pygame.mixer.music.play()
function works just like its counterpart in the pygame.Sound
module, but it accepts an extra argument. The first argument is the number of loops (0
to play once, -1
to play indefinitely, or any other value to loop <n>
+ 1 times). The second argument is a start position, which in most file formats is the number of seconds into the file to start playing.
To set up a playlist, use the pygame.mixer.music.queue()
function. queue
accepts a file name just like load
. When the file currently playing finishes, the queued song will automatically be loaded and played. Only one song at a time can be queued.
Listing 8 shows how to overcome the single-item queue. I can use pygame.mixer.music.set_endevent()
(line 15). Its argument is a value between pygame.USEREVENT
and pygame.MAXEVENTS
, just as for the pygame.time.set_timer()
function I discussed earlier. It will cause the provided event ID to appear in the Pygame event queue once the currently playing track has ended (which is checked in line 21). When this event triggers, I can consult my Python list of track file names (lines 8-11) and pygame.mixer.music.queue()
the next track (line 23).
Listing 8
Background Music Playlist
01 import pygame 02 03 pygame.mixer.init() 04 pygame.display.init() 05 06 screen = pygame.display.set_mode ( ( 320 , 240 ) ) 07 08 playlist = list() 09 playlist.append ( "Epoq-Lepidoptera.ogg" ) 10 playlist.append ( "Hydrate-Kenny_Beltrey.ogg" ) 11 playlist.append ( "Mists_of_Time-4T.ogg" ) 12 13 pygame.mixer.music.load ( playlist.pop() ) # Get the first track from the playlist 14 pygame.mixer.music.queue ( playlist.pop() ) # Queue the 2nd song 15 pygame.mixer.music.set_endevent ( pygame.USEREVENT ) # Setup the end track event 16 pygame.mixer.music.play() # Play the music 17 18 running = True 19 while running: 20 for event in pygame.event.get(): 21 if event.type == pygame.USEREVENT: # A track has ended 22 if len ( playlist ) > 0: # If there are more tracks in the queue... 23 pygame.mixer.music.queue ( playlist.pop() ) # Queue the next one in the list
The stop
, set_volume
, and fadeout
functions of the pygame.mixer.music
module all perform like their pygame.Sound
counterparts. Unique functions are rewind
, which starts a track over at the beginning (e.g., when restarting a game level), and set_pos
, which accepts an argument (milliseconds) and sets the current playback position within the music file.
Conclusion
The event/response model of Pygame differs from traditional program flow, but in a game environment or for software with lots of user interaction, it is quite versatile. By adding your own "game mode" variables and status flags to track program state, the logic can become quite complex, but at the same time powerful. Coupled with the built-in sound engine that allows for both foreground and background music playback, the capabilities exist for almost any 2D game you might want to create.
I've also found Pygame useful for several graphical tasks outside of games. Sometimes it's more convenient to use a widget-based toolkit for my main interface and then launch Pygame purely for a "graphics window." In any case, Pygame is an excellent package that provides the flexibility to create many graphical and interactive projects. Now, it's your turn to start programming!
Infos
- "Pygame Modules" by Scott Sumner, Raspberry Pi Geek, issue 03, 2014, pg. 36, http://www.raspberry-pi-geek.com/Archive/2014/03/Graphical-displays-with-Python-and-Pygame
- Pygame: http://www.pygame.org
- Ogg Vorbis music files: http://www.vorbis.com/music/
Buy this article as PDF
Pages: 2
(incl. VAT)