DEV Community

Cover image for Make a Shooter in Lua/Love2D - Music and Sound Effects
Jens Genberg
Jens Genberg

Posted on

Make a Shooter in Lua/Love2D - Music and Sound Effects

Hello, and welcome to the final part of this tutorial! In the last post, we added some visual effects to the game. It won't really feel complete until we add some audio, though. Read on to find out how.

Loading some files

We will be using the module to play back sound and music files, so the first thing we'll do is load some files into memory. This is very similar to loading image files, except we are using objects of the type "Source" to store the data.

musicTrack ="resources/audio/Mercury.wav", "static")
shootSfx ="resources/audio/Explosion.wav", "static")
enemyDestroySfx ="resources/audio/Shoot.wav", "static")
playerDestroySfx ="resources/audio/Lightening.wav", "static")
Enter fullscreen mode Exit fullscreen mode

We call the newSource() method and provide the paths to our sound files. The second parameter "static" tells Love2D to read the whole file into memory. It can be omitted for larger files, in order to stream them piece by piece. Since the files I'm using are very small, I decided to just load all of the data at startup. We want the music track to loop and luckily there is a function called setLooping() just for that. One of the audio files had a higher volume level than the others, so we call the setVolume() function to bring it in line with the others.

Playing music

Since there will only ever be one instance of the music track playing, handling it won't require much code. All we have to do is call musicTrack:play() in the startGame() method and musicTrack:stop() when the player is hit and the game ends. That's it!

Playing sound effects

We need to add a bit more logic for playing sound effects, so I decided to create a reusable function.

function playSound(sound)
  pitchMod = 0.8 + love.math.random(0, 10)/25
Enter fullscreen mode Exit fullscreen mode

The playSound() function takes a Source object as a parameter. It calls the rewind() function on the Source, which is needed to get Love2D to play the sound effect from the start. This probably happens because the same Source is used for all instances of a sound.

A trick to add some variety to sound effects is to slightly modify the frequency of each sound when it's triggered. We generate a pitchMod value between 0.8 and 1.2 and give it to the setPitch() function. Then we call sound:play() just like when starting the music track.

After that it's just a matter of placing calls to playSound() at appropriate locations in the code.

playerAlive = false
Enter fullscreen mode Exit fullscreen mode

Stop the music and play a loud explosion sound when the player is hit by an enemy.

table.remove(enemies, index)
table.remove(torpedoes, index2)
Enter fullscreen mode Exit fullscreen mode

Play a similar sound when an enemy is hit by a torpedo.

torpedoTimer = torpedoTimerMax
Enter fullscreen mode Exit fullscreen mode

Play a whooshing sound whenever a torpedo is spawned.


This concludes the series on making a shooter using Lua/Love2D! I hope you have found these posts useful. At some point in the future, I will be writing some more advanced tutorials using the same framework. Feel free to contact me at any time with questions and comments.


Latest source
Love2D wiki
Lua reference


  • The music track used for this part was made by SketchyLogic and is hosted over here.
  • The sound effects are by bart and can be found here.

Top comments (0)