Pygame Platformer Part 14: Sound and Music
Skills:
Python for Data60%
Key Takeaways
Adds sound and music to a Pygame platformer game using Bfxr and OpenGameArt
Full Transcript
welcome back to game development with pygame this is part 14 of the platformer game and we're going to be adding sound and music alright so we're ready to start talking about sounds so we're gonna need to load our sound files so we're gonna stick a section here in our load data method to load some sounds sound and the first thing we need to do is we're gonna define our sound or sound folder and I'm just gonna use s and D as abbreviation for sound because it's shorter to type and this is gonna be just like we did in our for our image folder right we're just gonna join our working directory with the folder called snd which I've created here in our working folder and that's where I'm putting all our sounds now right now the only sound I have in there is one for jumping so our jump sound we're gonna say PG mixer dot sound and we just point to that sound file which happens to be called jump 33 WAV now obviously there's lots of ways you could go about getting sounds you can find them online in all sorts of different places it's also fun to go and use this little tool here called BFX r @b FX arnett this is a great little retro style sound generator you can just click on the thing you want if you want to power up sound it'll generate some writing power up style sounds picking up a coin and there's that a jump category right so you can get jumpy counter stones and that's where I got my sound that I liked using I played around with this for a while target found one I liked you can spend as much time as you like tweaking all these little sliders and and getting the sound just the way you like it and when you have one sure you export WAV that'll save the file as a WAV file we do not want to click save to disk that'll save it in the BFX our format which pygame can't read so don't forget to export and so that's where i got my jump 33 okay so now it's just a case of taking this jump sound and choosing where to play it now we could just go down here to our event and here when we press the spacebar we could just say also play the jump sound so jump sound up play and that would probably work let's see what it sounds like okay now when i press the spacebar and jump i hear the sound that's pretty good but the problem is that's gonna happen every time i press the spacebar so if i'm trying to jump really fast see how i'm hearing the sound even when i'm in midair and i don't want that i only want the sound when i actually jump off the ground so we need to use our players play a jumping flag to tell whether we should play the sound or not so instead of playing this here i'm going to delete that and over in my player sprite i've already got these conditions set up so this is what makes the jumping happen so this makes sense as the place where we should also play the sound and we only want to play the sound if we're actually starting a jump right way meaning we're not already jumping so we could add it right here into this if clause so this is going to be self dot game jump sound play okay and now that shouldn't work a lot better i mean that should work a lot better for my jump sound playing only when i jump so we get the same effect still with that but now if i'm in midair and i press the spacebar i won't hear the jump again right I only hear the jump when I actually leave the ground okay so that's sound and we'll add some other sounds as we go as we had some more features we can add sounds for them but the other thing that I wanted to talk about in this video was music and while that technically is sound to music is a whole nother side of the coin and in music is really useful to add excitement to add interest to make your game more feel more interesting to play to pull the player into the game and so we want to have some background music playing while we're jumping around and just for fun since we're doing it will also do a different music track that we can use during our game over screen or our Start screen so that means we're gonna want to play some music and there are a couple of things you need to know when you're talking about doing music and especially when it comes to pie game there's one of the things you need to keep in mind is if you go over to the PI game documentation it very specifically here says mp3 support is limited on some systems it can crash the program and so you want to stay away from using mp3 files for your audio or for your music I should say and so they suggest you use OGG OGG is a great format that compresses really nicely makes your music files nice and small and not take up too much space and there are online converters if you've got an mp3 that you can google for and you drop the mp3 on there and it gives you back the same file in OGG format you probably don't want to use wave just because WAV files are uncompressed so a music file is pretty big it's got a lot of sound in it and so a WAV file of a really large or a long piece of music is going to take up a huge amount of space and the trick is that the way music playback works in PI game is it doesn't open the whole file and load it into memory it streams it so it reads it's continually reading it and playing it as it goes and you can only as you see here play play one music stream at once now you can definitely have lots of different sound effects going on but as far as a music stream that's continually playing you can only have one going going at once so we are going to I've got some music that I already loaded that I actually found on open game art if you go on open game art and you browse the music section there is a huge amount of stuff on there that you can choose from all sorts of things for for horror games RPGs fantasy space techno you name it and so feel free to go go explore in there you can find all sorts of really cool stuff most of the artists will put in there on their page what you need to do to use it how you need to credit them or what they're asking you for in return for giving their music out for free so I have gone ahead and I've grabbed a couple of music tracks off of open game art and I'll link to them specifically in the comments below but let's go ahead and and add them and and so first of all I'm gonna go up here to the top and I'm gonna post the credits so I have one it's called happy tune and one that's called yippee and happy tune is what I'm gonna play for my for while we're playing the game and you fee is gonna be our menu screen music so because we can only play one or the plugin can only play one music stream at a time you can't load both of them ahead of time you can know you need to load it because it doesn't actually load it right it just opens it so you need to open that music file when you're ready to play it or in the part of your code where you're ready to play it so for the music while we're playing the game the new the Deaf new is where the is a perfect place to do this is where we're starting everything up and getting ready to run the game so I'm just gonna add here right before run PG mixer music load and we're gonna say this is also in our sound folder and it's happy tune dog okay so that loads the music so now the music is kind of queued up and ready to play so we want to when we run we're gonna start it so when the run loop starts we're gonna play the music so PG mixer music dot play now if you just do dot play it'll play the music it'll play the music file one time through and then end but there's also a loops option that you can do where you can say how many times you want that music to repeat we're gonna put minus one here which means just infinitely repeat so the music will just keep looping and you'll notice when you search for music on open game art especially a lot of them will be labeled as loops so that that way they can seamlessly just loop around and you don't hear any you don't hear an end of the song and it's starting over again you just get a repeat so that will start it playing when the game starts and then we want it to end when the game ends so when the game loop ends we're gonna say PG mixer dot music now we could say stop but instead I'm going to use fade out which fade out we'll let it instead of just suddenly stopping let it just gradually and gracefully fade out and you just put in here how many milliseconds you want to use to fade it so I'm just going to fade out over half a second which just sounds a little nicer than just cutting it off okay so let's run that and see what happens here we are now we have some nice happy music playing in the background while we're doing our jumping already feels a nice so pretty so and now when we die we should hear the music stop there it goes fades out a little bit and then we're on the GAMEOVER screen and now we can add some music to that so we'll go to our show Start screen and we're just going to do the same thing we did here alright so I'm just gonna actually grab this line and in my Start screen I'm gonna paste that and we're gonna use the yippee OGG file we're gonna play it again with loops equals -1 and then when the Start screen ends we're going to fade it out again alright so there we go now we've got a nice little menu screens now music press play and we start playing now on our game over screen we don't have any music but and we could you could do some you know sad music because your your game ended for now for me I'm just going to do the same music on my game over screen because I don't need I don't need a different song there but you could do that you could do that obviously you could found a different song you like maybe a sad you died kind of so sorry your game is over song add that to your game over screen and have that okay alright so that will do it for this video we've added sound effects or I should say one sound effect we've added a sound effect and we're ready to add more whenever we have some other things we want sounds for and we added some nice background music to our game and to our game over screens so I hope you enjoyed the video if you did please press like that would help a lot and if you're not subscribed please do so you can see the next lesson as soon as it comes out and I will see you next time you
Original Description
In this video we add a jumping sound and background music.
Bfxr sound tool: http://bfxr.net/
"Yippee" song by Snabisch: http://opengameart.org/content/yippee-0
"Happy Tune" song by syncopika: http://opengameart.org/content/happy-tune
In each video in this series, we'll add another feature to our platformer until we have a full game experience with graphics, animations, sound, and much more!
Code for this part:
https://github.com/kidscancode/pygame_tutorials/tree/master/platform/part%2014
If you like these videos please consider supporting me on Patreon: https://www.patreon.com/kidscancode
Other helpful links:
* Installing Python: http://kidscancode.org/python-install.html
* Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/
* Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 47 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
▶
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
Rendering Lists and Handling Events
Dev.to · Silas
React Explained: JSX, Components, Virtual DOM & Diffing Algorithm
Dev.to · Saravanan Lakshmanan
The “Wiz” Merger: How Google’s Internal Framework is Rewriting Angular
Medium · JavaScript
Day-3 of Posting blog (Lists in html)
Dev.to · antony stark
🎓
Tutor Explanation
DeepCamp AI