December 31, 2018. Officially the last day of the year. I wanted to end this year by finishing what I started: Creating the last post for how to make a Space Shooter in Pygame and Python. It feels right to wrap things up and start something new and fresh for the new year. There are a number of machine learning and game development projects that I am looking forward to getting started on and sharing with all of you in the next year.

In the final Space Shooter tutorial, we are going to finish two major components: creating a game menu, and adding sound and music to the game.

Cartoon fireworks. (Created using OpenCV.) Going to try something new in the next year.


Adding sound and music

The first task we will talk about is adding sounds and music to our game. Unless we are in the vacuum of space where the large empty spaces between stars and planets provide no molecules for sound waves to travel, then sound provides a much more immersive, realistic, and beautiful experience. The right sounds and music can be used to make a gaming experience more memorable and enjoyable.

Let’s get started by finding the file where all of our sounds are located:

This bit of code locates the path of the sounds folder and is written under where we import the necessary packages at the top of the file.

We must also initialize the sound mixer for Pygame.

Next, in the main( ) game loop, let’s load all of the game sounds and give them variable names so we can call them in our game when we need to.

Let’s look at line 2. Here we load the bullet_sound used by our player ship by locating the sound file name, ‘laser.wav’, and assigning it to a variable name. Pygame has a function that allows for volume adjustment. We use this function in line 3 so the constant sounds of our bullets shooting don’t distract from the other sounds. The other sounds are the enemy bullets sounds, player missile sounds, and the three different types of explosions.

Sounds will be used throughout the game, so to keep this post from getting too complicated I will show an example of how to use the sound. To check out where else to place the sounds, you can try it out for yourself or check out the full code.


Let’s take a look at a complete example for collision detection for bullets and asteroids.

For this instance of collision detection (and others) once we determine that a collision has occurred we need to play not only an animation, but also a sound. After the detection, let’s call the sound we want to use, large_expl for asteroid explosions, and call play( ) and set_volume( ) on the explosion in lines 6 and 7. Every time an asteroid explodes the sound will play.

Using sound in Pygame is really that simple. For other instances where sound is needed, such as bullets or missiles, find the instance where that object is used and follow it by ) in the code.

Game Menu

The second task we are going to tackle is the game menu. Traditionally in older arcades games the menu served as the interface for a player to start the game or learn about the controls. If no one was playing, the game would enter a demo loop to display examples of gameplay, which would be broken if someone moved the controller or pressed a button.

Our Space Shooter will do pretty much the same, sans demo loop. When the player starts the game, we will enter the main game while loop. While the user has not pressed ‘return’ (this is our start button), we will remain in the main game loop and keep checking for either ‘return’ to be pressed or the ‘q’ key to quit the game.

We need to create the menu( ) function and call it in our main game while loop.

There is a lot to unpack here. In lines 3 – 4, the main title music is played, and by using play(-1) the song plays in a continuous loop. The main title and star background image are loaded in lines 6 – 9.

I am excited to finally post this picture. Created using

We then load images that will display the spacebar and arrow key images to the screen in lines 12 – 15. The images are used to show the main game controls to the user before they start the game. These images are all blitted to the screen in lines 17 – 20. Game instructions are also drawn on the screen and yellow-green rectangles are drawn around them to make them stand out from the background.

Finally in lines 34 – 44, we will remain in this small loop on the menu screen until the player presses the ‘enter’ key or quits the game.

“Where and how do we use this menu?” You might ask. Why, in the main while game loop. We first need to create two boolean variables. running is True if we are still in the game loop, and show_menu is True if we are in the menu loop.

Once we push ‘enter’ and break out of the menu loop, a delay is called in line 6 so we don’t jump right into the game. Let’s also fade out of the menu music in line 9 and play a different song when the game starts in line 11 and 12. We then make show_menu equal False.

But how do we go back to the menu if we are out of lives? If player.lives == 0 and the animation for the ship explosion is no longer playing (explosions_ship.alive( )), then we stop the music and return to the main menu in lines 19 – 21.

Final image for the the Spac Fighter tutorial.


Done! That ends the tutorials for the Space Fighter game using Pygame. In this tutorial we added music, sound, and a start menu to the game.

I hope those of you who read this tutorial will use the code to learn from it and make your own games and post them so others can see. I would really like to see what games you have made. Please leave a link in the comments below.

Final Note

It has been quite an adventure writing these posts and in many ways has helped me to realize what I enjoy doing. I would really like to hear back from all of you about your own personal programming adventures in the comments. In the future I am going to start working on more machine learning and computer vision posts. But if there is ever any topics you are thinking about and would like to see here, please let me know.

I hope all of you can continue to follow your goals and dreams in the new year! Happy 2019!


Visit Us
Follow Me

Leave a Reply

Your email address will not be published. Required fields are marked *