Pygame Tutorial 16 Adding Sounds and background music

alright guys welcome back and hopefully you are having a great day and today we are going to learn about how to add sounds and music to our PI game and currently there are no sounds inside our game so it looks pretty simple but what we are going to do is we are going to add three types of sounds the first sound is the background song that's going to play continuously in the background the second is the shooting of the bullet sound in this case I call it laser dot wave and the third one is explosion dot wave that is going to be produced when our laser bullet hits our enemy and just to show you guys how it sounds I'm gonna play this main dot Python file which I've already changed the code off so that you guys can see what's happening inside our game so I'm just gonna play it and you can see all right so you must have seen there are three sounds first sound continuously goes in the background second is the laser or the bullet sound and the third is when a bullet hits our enemy now how can you get all these three files let me actually make sure that the code is original and the last one that we have used and now how do you get these three dot WAV files so what I want you two guys to do is I want you to go to github.com a3 about space invaders by game and you'll be able to get all the code that I have written till now and along whether you'll be able to get the dot pay files that I have been talking about after downloading these three files or maybe the whole code and you can download them by clicking on this clone or download and then download zip make sure you add these sound files to your project by going to space invader and just pasting them over there now let's get started with adding background music into our game so first thing we need to do is import something known as a mixer from a PI game library and to do that we just write from my game import mixer input this mixer and what does this mixer this is basically a class that helps us handle all kind of music inside our game whether it can be repeating a music loading a music anything that you want so you have to look inside this class whenever you want to do anything with sounds or music and then just below this background we are going to add a background sound so let's right come inside so that you guys can know in future what we are talking about and then we're just gonna use the mixer library by writing mixer dot music dot load pretty easy because you want to load the file and then we are going to play the file and inside this we are just going to write background dot wave which is the name of our file as you can see on the left hand side and then we are just gonna play this sound by writing mixer dot music play play easy and now we want this file to play continuously because right now if we don't add anything inside this dot play this file is gonna play once and then just stop but we want it to play in a loop because this is a game and can go on for long we don't want it to play just once and how do you do that inside this mixer class you just add minus one and is gonna play on Lou so now let's just test it out and see whether it is working or not [Music] as you can hear the background music must be going on but we haven't yet added the bullet sound explosion sound so that must not be there so that's why we are gonna be working on adding the bullets on now but one thing I want you guys to notice is that currently we are using mixer dot music because we want this file to be played continuously in the background so this is kind of music but if something is very short for example our bullet sound then we have to use mixer dot sound so we also going to come below where we have a bullet shooting so how are we going to catch the moment when our bullet is being released so we are going to do that inside our let's go to above let's go to this if bullet state is ready so over here we can release the sound of our bullet going out from our spaceship so because over here we are getting the x-coordinate of the spaceship and then we are shooting the bullet over here so we can somewhere over here at our bullets tongue and how do you do that is also pretty easy just below this we are going to write mixer dot sound so in this case it's not mixer dot music it's mixer add sound and we're gonna first create a variable let's create a variable and call it bullet underscore a sound just to make sure that we know where our sound is and mixer dots on and inside this you have to give the file of the sound so in that's in our case is laser dot wave and then just like we did in the music case we are gonna play it so we're just gonna write but it's sound not state well it's sound dot play and we don't want it to play it inside loop that's why we haven't added -1 so now our bullets is making a sound now we just need to add a sound to our collation and where are we catching collision let me just copy this first and we are gonna go below I just want you to guess first where we are going because this is pretty obvious we are going to this part where if collision we want a sound be played and that sound is of explosion so we are just going to copy and paste this over here because most of the things are going to be same instead of bullet we are going to write explosion sound and then I'm just gonna copy and paste this just below this play and instead of laser dot wave we are going to go with explode dot wave so this this is pretty much it let's actually play our game one more time and see if everything is working properly so let's play this [Music] [Music] all right guys so everything is looking really good so are all of the three sounds are playing obviously feel free to work around and play around and add more sounds maybe remove and add your own sons that's going to be fun in the next video we are going to learn about how to make sure that our game gets over when our enemy hits our spaceship so I'll see you over there

Loading