Music¶
Some background music adds a lot of atmosphere to the game. Including music is not difficult to do. Look around opengameart.org for freely available music in the MP3 format. You can also use an AI to generate some.
To play the music from your game, you need the pygame library. Install it from the command line with:
pip install pygame
Hint
On some systems, you may need to check the PyGame documentation for installation details.
The following code starts the music:
from pygame import mixer
mixer.init()
mixer.music.load("my_music_filename.mp3)
mixer.music.play(loops=-1)
At the end of the program, you may want to stop the music:
mixer.music.stop()
That’s it. Have fun!