Python Pygame: Adding Background Music to a Python Game

Featured Img Background Music Pygame

Hey there! Today in this tutorial can we look at adding background music in the window using pygame. So let’s begin!

Step 1: Download the music we want to play

The first thing that we need to do before we move on to the coding section, is downloading some music. You can choose whatever music you want. I went with some simple catchy background music for now.

I have added the background music I am going to use. You can hear it if you want to.

MP3 FORMAT

But the problem is using mp3 format for background music in pygame can get quite buggy and can result in weird squeaky and popping sound instead of the music.

So to prevent that we convert out mp3 format file to OOG format which makes it a better fit for pygame windows. You can find the file format converters online where you just have to upload your music file and the rest is done for free!

The oog file sounds just the same. I have added my wav music file below for you to verify the same.

WAV FORMAT

Step 2: Importing Modules

The first step in every program is to import the necessary modules. The code for the same is shown below.

import pygame
from pygame.locals import *
from pygame import mixer

We will be importing pygame and pygame.locals module for smooth functioning of the window. And we will be importing the mixer module to play the music.

Step 3: Create the basic screen design and functionalities

To create the screen we first initialize the pygame using init function. We will be loading the background image using the image.load function.

Then we will have a running loop that will put the background image and update the screen design. We will also add the QUIT window functionality with the help of event handling.

In the end, we will be using the quit function to destroy the pygame initiated earlier. The code for the same is shown below.

pygame.init()
width = 1000
height = 500
window = pygame.display.set_mode((width,height))
bg_img = pygame.image.load('Images/bg.png')
bg_img = pygame.transform.scale(bg_img,(width,height))

runing = True
while runing:
    window.blit(bg_img,(0,0))
    for event in pygame.event.get():
        if event.type == QUIT:
            runing = False
    pygame.display.update()
pygame.quit()

The output screen of the code above is displayed below.

Output Screen1 Bg Music Pygame
Output Screen1 Bg Music Pygame

Step 4: Initialize Mixer in the program

The next step is to initialize the mixer in the program and load the music using music.load where the path of the music file is passed as a parameter.

After this, we add the function named music.play. But this won’t start the background music. For that, we need to add some more elements in the code which are covered in the later sections.

The code for the same is shown below.

mixer.init()
mixer.music.load('Music File/bensound-summer_wav_music.wav')
mixer.music.play()

Adding Background Music to a Pygame Window – Full Code

The final code is shown below:

import pygame
from pygame.locals import *
from pygame import mixer

pygame.init()
width = 1000
height = 500
window = pygame.display.set_mode((width,height))
bg_img = pygame.image.load('Images/bg.png')
bg_img = pygame.transform.scale(bg_img,(width,height))

mixer.init()
mixer.music.load('Music File/bensound-summer_wav_music.wav')
mixer.music.play()

runing = True
while runing:
    window.blit(bg_img,(0,0))
    for event in pygame.event.get():
        if event.type == QUIT:
            runing = False
    pygame.display.update()
pygame.quit()

The Final Output

The output of the code is displayed in the video below. I have not put a whole over 3 minute video.

You can hear the music playing perfectly!

Conclusion

Congratulations! You successfully learned how to add background music to your screen! You can try out different music sounds and add it to your games build if you have any!

Thank you for reading!