 Welcome to Space Arena, the ultimate Python turtle graphics game tutorial part 17 in which we will be making Multiple missiles for our player to shoot So I'm gonna go ahead and show you what we've got here in case you haven't been on for a while And so you see I've got one missile. I can only fire one at a time, which is okay I can live with that, but let's say we get a power up or we want to make more missiles Let's go ahead and give our player the ability to shoot multiple missiles So the first thing we got to do is actually we're gonna create the missiles that the player can use So I'm gonna come all the way down here and this program is getting to be quite large I should refactor it and put it into multiple Files that makes it a little bit more difficult. I think first for some beginners so I'm just gonna go ahead and keep it the way it is and So you can see here I've created a missile object, but what I want to do is I want to create multiple missile objects so I'm gonna go ahead and create missile objects and I'm gonna make a list just like I did with sprites. I'm gonna say missiles Equals and I'm gonna make an empty list. I'm gonna use a little for loop And let's I'm just gonna go ahead and make three to make to start out and then what I'll do is I'll say missiles Dot append Missile now. Don't forget the extra parenthesis over here. You'll get an error it comes up all the time and That will create multiple missiles. So now if I try to run the program I'm gonna get an error because it's not set up for that. Okay, so let's go ahead and probably Go up to the player. Let's go ahead and go up to the player class and In our fire method, this is where it Shoots okay, so you see here. It says missile now as I showed I just I have several missiles so what I gotta do is I gotta check the missiles so for missile in missiles. I want to fire now if I do this Let's not just see what happened. We're probably gonna get an error Yeah, we got that we get an error Sprites append missile line 43. So I made a mistake somewhere line 43 Okay, so you see here where I Append the one missile. So what I do is I go say for missile in missiles Append missile. Okay, that'll add them all. Let's go ahead and try that again Yeah, we're gonna get some errors. So you'll see here now in 582 again It's looking for a missile, but we haven't defined it anymore. It's now a list of missiles So I'm gonna go to line 582. It's from all the way down. I said this is getting to be a longer program and 582 Here we go. So if missile state so I'm gonna go ahead for missile in missiles This is one of the nice things about using a list. It just kind of You know takes care of things for you and there's also another one here Now I could switch this code around a little bit and just break that down to one But let's go ahead and stick with the way it is Missiles, okay, so there's several places that we have to Deal with this missile issue. So if I just can keep testing it and see what happens, you know, it's running It's gonna hit space bar fires Okay, now it's hard to tell but it's actually firing all three missiles at the same time And you can probably see that the collisions aren't working quite right and that's something I have to come back and revisit but I Don't want them to fire all three missiles on top of each other. So here's what I'm gonna do I'm gonna go back to my player Wherever that's at there we go fire and Right now it's firing all of the missiles Okay, so what I'm gonna do is if missile dot state Equals active. That's actually goes ready. I believe was what we used. It's been a while So if the missile is ready Fire the missile Okay, so But what I need to do is I only want to fire one missile So I'm gonna try and put break in here and what break should do if I'm correct is it'll break out of the loop And it will only fire the first missile. Let's go ahead and test that I could be wrong, but so space bar, okay? Okay, two missiles. Okay, so once you're three so I can fire three missiles at a time, which is pretty cool and you see as soon as one disappears I can go ahead and Fire it so we could actually if we wanted to we go ahead do something crazy and we could go ahead and Do something like? Missiles we can make make 30 missiles what the heck why not So I go ahead Now because the they die pretty early they they they disappear early It's hard to get all 30 going, but you can see how basically now I can have almost unlimited missiles Let's go ahead and change that back to three Okay, so what I want to do is to prepare for say the future where we'll have a power-up that will give us You know missile abilities or whatever you want to call it what I'll do is in my fire method What I'm gonna oops in my not missile fire, but in my player fire method what I want to do is First figure out how many missiles I Have okay, so I Want to say numb of missiles Equal zero and then what I'm gonna say I'm gonna get quite a few changes here. So watch what I do here. So numb of missiles Plus equals one okay, so now now this part is gonna be put somewhere else, okay So watch what I do here and watch the indentation So I'm gonna find out how many missiles are ready. So we're either gonna have zero missiles We're gonna have one missile ready We're gonna have Two missiles ready or we're gonna have three missiles ready. That's that's our case. All right, so let's go ahead and deal with that so if Okay, numb of missiles Equals one Then what I can do is I can just go ahead and do this again So that that should put it back to where what? Then what I can do here is I'm gonna go ahead and just copy this first for now Again, usually I code these out ahead of time. I'm kind of doing this live So it might be a little bit clunkier than you're used to seeing So I'm gonna hit to and I might have to change this around Okay, I'm trying to keep this a little bit simple. So no matter what number of missiles I have It's going to let me do this. All right, so go ahead and hit function of five I got an error, of course break outside of loop. Ah, okay I got to do a loop here again for missile in missiles Okay, so F5 So right now there's three ready. So let's see what happens Okay, I can only shoot one missile at a time, which is which is fine Because it's equals three and then I Can only shoot one of those. All right, so that's it breaks right out of that So let me think about how I want to approach this and again, like I said, I usually figure these out ahead of time but I'm just gonna do it here, so Watch what I do here. I'm gonna say print three. I just want to see what's going on Okay, print two So this is kind of just kind of to show you the I'm gonna say that shade the way you might want to Debug your code a little bit without if you don't want to use a debugger, of course So let me hit a space bar. Okay, so you see down here three three three two Okay, okay, but nothing's nothing's really changing. Okay, so what I want to do I'm gonna actually go ahead and probably undo this and say If the number is three what I can do is something like this so for missile in missiles Now I want to fire all of them, right? Now here's the challenge is if I fire them all they're all gonna be on top of each other Okay, so Let's see here. What could I do? Well, I know what I did before so I can I could do something like this directions equals zero negative five and five This could be a tuple this doesn't have to change Yeah, this code isn't quite as lean as I'd like it to be but let's let's do this so I'm gonna say How can I put this? Self dot sorry missile dot that heading plus equals Directions dot pop Okay, and let me see this works and then I'll explain it top object has no attribute pop Okay, this has to be a list my bad There's probably a better way to go about this, but this this I think will work Okay self dot missile that heading plus equals directions dot pop Heading for missile missiles Let me try that again Notice three didn't that's interesting three didn't I took it out print Three I just want to make sure that's the section of code that's operating Print great. This is one of those good days. I have in coding But you know, it's interesting like Just you know for people who are falling at home or beginners. I've been doing this a long time I make a lot of mistakes So it's it's it's not Unusual So let me go ahead and try that yeah, I should be working by don't know why it's not Oh Total working mistake. Okay. I'm breaking out of that For loop that was stupid. Okay, so let's go ahead and run that again. Okay Okay, three. So That's not working. I was pretty sure that was gonna work I'm trying minus 10. See if maybe we just need bigger numbers So that heading function of five. Oh, I know why because the heading is actually changed later No, ah shot. Okay. Got it Self that heading plus directions stop pop Okay, so the heading of the missile Comes from the player. That's right. Okay. Yeah, I haven't done one of these two videos for about two weeks There's a bit of a gap here. So a little rusty myself. Let's try that. Okay. Yeah, that's what I wanted Okay, so now we've got a spreading volley of missiles and they all bounce off the wall separately And we can go ahead and shoot stuff. Oops. That was not what happened Okay, so that's the case where we have three missiles okay But what's interesting like let's go ahead and Yeah, so So you notice there I can only shoot one missile because there was only one missile left So what I'm gonna do is I want to go ahead and just basically copy this because it does work Which which makes me happy And then I'm gonna do the same thing here for two So in the case where we have two missiles and I think I'll put this back to five I don't want that quite a bigger spread for the The missiles so if number of missiles equals two and then What we'll do is We want them to go left and right And I have to say here if missile that state equals active And the more I added this the more I realized this is not particularly efficient code my original version was much nicer actually and So what I'll do here is I'm gonna copy this again. I'll just review this at the end real quick Now here I shouldn't need to do this But we could actually end up in the case where we have ten missiles and we want to just only fire three at a time okay, so so I'd say Yeah, I had to fix that a little bit later. Anyway, so let's just keep it to one two or three So in case we've got two active missiles. We should only fire You know kind of two missiles spread. So let's let's go ahead and test that out by Changing our number of missiles to two Let's go ahead and run it and two and then nothing's happening and we'll see what I did wrong Those two it's not printing to why is that? Okay, let's get rid of this. I don't think we need that Yeah, sorry, like I said, this is really sorry. I'm usually a bit more organized But I think there is a value in watching somebody screw things up and try to fix them. So let's go ahead and Do that now we got an error break is outside of a loop Okay There we go. Let's see if that fixes it. Okay, two is coming up, but nothing's happening. So let's see why Missile and missiles sold out fire now it worked with three, but it's not working with To know why is that? Let's go ahead and make sure the number of missiles is correct. So print Missiles now this should keep printing over and over again Okay, two two two two But I don't see any missiles. Isn't that annoying for missile missiles a missile that stay equals. Oh, it's not active It's ready But it worked there, which is kind of weird. Let's go ahead and test it again There we go. That was stupid. Okay, so now if we got two missiles So it'll fire two missiles at a time, which is kind of cool Let's go ahead and test it with one missile See you got to test all these cases because you God knows As you can see things will happen and range one. So let's go ahead and give it one missile It actually probably what I should have done now that I think about it. Again, this is this is kind of part of the process I suppose Number of missiles plus equals one. Oh, I already did that. So if number of missiles is ready. I already did that Yeah, I think that's it. I think it's working. So let's go ahead and put it back to three because it was kind of cool And then I'll review it and show you what I did there one more time because that was a little confusing and I do apologize for that Plus this video is getting really long so anyway So and I'm sorry for all the as I've been trying to get rid of those but Learn so Let's go back to the start here where What I did was I created a list of missiles now and then Appended them to Appended a certain number of missiles to the list. I Also, it's this point. That's one thing that was changed. I also at in the collisions check to see Not just one missile But for missile and missiles so I check if it's active and then check for the collisions And I had to do that twice once for power-ups and once for the enemies again I could have combined that but I'll leave it the way it is the other thing I had to do was in the game setup Start level excuse me game start level. I had to append all of the missiles to the list of sprites so they would be updated and dealt with properly and then in the player fire method I had to find the number of Active missiles missiles that are red and I should say active number of missiles that are ready to be fired and In a case where there's one missile I Would fire just that that the one missile I should I should say I should actually do the same thing here for Missile and missiles because it might not be the first missile that's available. So we might get a little error there so let's go ahead and take that out and Then now again, I should test this but I'm just gonna leave it like this because I'm tired So if the number of missiles equals two then what I did was I didn't explain the pop thing. So what I did was I have Fight minus five degrees and plus five degrees. I want them to go slightly out at angles And so what directions pop does is it actually takes this value out of the list? pops it off the list and Then I can add that so the first time I add negative five pop pop it again. It adds five So number two and the number of items here has to match now if I had like I want to do a 30 missile Round robin thing which would be pretty cool I would have to have all of these all those plus five, you know five ten fifteen twenty twenty five Or whatever the numbers would work out to now in the case here where I've got three missiles that are ready Yeah, I should qualify that I'm gonna pop out the first one's gonna go straight second one's gonna go a little to the left Third one's go a little bit to the right, but you can't tell that as you're playing the game Go ahead and delete those we don't need them and I think that is it. I'll test it one more time But I think that should do it and I will put this code along with all the other Testing code in the github link which is in the description so if you like what you see subscribe for details or subscribe for updates just to say and Stick around thanks for thanks for watching and keep on coding. Take care