But first, the basics...
Playing a song once:
Playing a song infinitely:
The number being passed in is the number of times to repeat the song. 0 will play it once.
Calling play without a number is like calling it with 0.
Queuing a Song:If you want a song to start playing immediately after a song is finished, then you can use there's a queue method.
Stopping a Song:
The stop function will also nullify any entries in the queue.
Doing Something When a Song Ends:The times that you really need a queue are rare. Typically you'll just want to play the same song over again until you change it. But suppose you want to play a selection of 4 or 5 songs in sequence over and over again. Or even play randomly from a list of songs forever. At this point it's better to implement your own logic and use the handy set_endevent function.
In part 1, I showed you how to pump the event queue. When going through the events, you check the event.type field and see if it's pygame.QUIT or pygame.KEYDOWN, etc. These type values are just integers. When you call the set_endevent function, it expects a number as input. Its value will be used in the event.type field when the song nautrally ends. Confused? Here's some code...
The USEREVENT + 1 is to ensure that the number assigned to SONG_END isn't inadvertently equal to any other predefined event. Like pygame.VIDEORESIZE or something. USEREVENT has the highest value in the enum.
Shuffle and Repeat:If, for example, you wanted to play randomly from a list of 5 songs, one could create a list of the songs as a global:
Add a flag indicating which song is currently playing:
And write a function that chooses a different song randomly that gets called each time the SONG_END event is fired:
Or if you want them to play in the same sequence each time:
SoundsThe music API is very centralized. However sounds require the creation of sound objects that you have to hold on to. Much like images. Sounds have a simple .play() method that will start playing the sound.
Because you can make the mistake of storing sound instances redundantly, I suggest creating a sound library much like the image library from part 2.
There are many more features but this is really all you need to do 95% of what most games will require of you.
Next up: Drawing Geometric Shapes