 Hey, welcome back to space arena the ultimate python total graphs game tutorial part eight where we make our player missile So based on what we've done so far It'll probably come as no surprise that our missile is going to be a child of the sprite class Now it's gonna behave in some ways like a normal sprite like some of the other objects But it's also gonna have some unique properties and we need to account for that. So what I'm gonna do is I'm gonna go ahead and copy just the first part of my player class and I'm gonna go ahead and Make that Into a missile class and you know missiles just the word I chose for it Now one of the things that's different about the missile class and the player class is that the way we have this program The player always starts in the center of the screen, but the missiles not always gonna start in the center So we need to account for that and change that from zero to x and that from zero to y Now something to think about why should you tell it? Let's just go ahead and put this on the screen Let's go ahead and make that real quick. So I'm gonna go ahead and create the missile Object Okay, I'm gonna say missile equals Missile and let's start it at zero comma one hundred just so we can see it I'm gonna make it a circle and I'm gonna make it yellow For now, so I'm gonna go ahead and run that and test it and see what we get Okay, now we don't see anything which is a bad sign usually now you should be asking yourself, okay Why can't I see it? and If it comes back to you, it's because we haven't actually updated it or rendered it in this process yet so if you recall we made this sprites list and And and and we're gonna add that to the list so that it is updated and rendered so let's try it again Okay, and there it is now it's a little bit big And you see it has a health meter which it doesn't really need So we're gonna have to make some changes to the missile class to account for that So the first thing I'm gonna do is I'm gonna go ahead and fix the rendering So what I'm gonna do is I'm just gonna go ahead and copy the render method from the player class and Put that into here So if you recall what's called overriding We're putting a a method with the same name as the parent class method And then this is the method that's used for this child class So what I want to do is I want to make this zero point. Let's go ahead make it zero point two Zero point two could make it zero point one And so that'll make it smaller. So let's go and test that see if it looks a little bit better Yeah, so that's a little bit more like what we're used to what we want We don't want the missile to be bigger as big as the ship, which doesn't make any sense Okay, so that's that's pretty good now The next thing we need to think about is right now. I can see that missile But of course, I shouldn't be seeing the missile because it has we haven't fired it yet So that's one of the differences between this particular object and The other objects, so I'm gonna go ahead and give this sprite give this object. I should say a new Attribute we might actually end up adding this to the main sprite class But for now, let's just put it here and I'm gonna call this self dot state and in this case I'm gonna call it ready and In my mind what ready means is that it is ready to fire. It's not already firing. I can go ahead and use it So I only want to render it So I'm gonna have well, let me say it this way. I'm gonna have two states gonna have ready and active So if self dot state equals active So I only want to render it When it's active Yes, I'm gonna go ahead and hit f5 and let's see if it's rendering. It's not rendering now, which is good because it's not active So now we got to go ahead and make it active Now what I could do is well what I'm going to do actually is I'm gonna make a method here called fire like so like firing the missile but Because we want firing to be controlled by the player. I'm gonna actually put a method in the player Class as well. So watch what I do here So in the player class, I'm gonna go ahead and tape define fire self Now if you think about it when the missile is fired, where does the missile starts from? So I'm gonna say missile Dot fire And it's gonna have to start at the same place the same x-coordinate Same y-coordinate as the player it's gonna have the same heading as the player because the player is going in a certain direction and it's gonna have the same DX and same dy as the Player so hopefully that makes sense So then in my missile class I'm gonna go ahead and put type def fire self I'm gonna put self here and it's gonna be x y heading DX and dy and again these are copied from the player I'm say self dot x equals x self dot y equals y self dot heading equals heading self dot DX equals DX and self dot dy It's do I equals dy Now something to think about I'll tell it. Let's I'm gonna leave that there for now. We'll come back to that Just kind of give you an idea. So if I push the space bar Okay, you see nothing happens because I didn't do the keyboard binding So down here my keyboard bindings. I gotta go win dot on key press Player dot fire And I'm gonna use the space now. Notice. This is a weird quirk This is capitalized. This is capitalized and this is capital left right up down or capitalized for some odd reason space is not If you capitalize it, you'll get an error. Okay, so let's go ahead and test that Okay, I push space nothing happened Which is not what we wanted because I forgot to do what ah I did not change the state of The missile so self dot state equals active So if you remember it won't render unless it's active Let's try that again. Hey, you notice how I'm testing a lot I add a line of code and test it and try and debug it so I hit the space You don't see anything, but it's actually there So if I do that It's moving at the same speed as the player so I don't see it so Which is kind of interesting but it's it's very true to physics though If you had a missile and you let it go you're traveling in space if it didn't have its own acceleration It would just follow the same pattern as its parent would in this case the the spaceship Okay, so what I need to do is When I fire The missile I need to give it a little bit of extra Like juice let's say. All right, so I'm gonna give my missile two more attributes self-dot fuel and I'm just gonna use two lots. Let's use a hundred. See what happens and self dot thrust And I make that Let's make it 4.0 now on your computer Especially if you I'm on a Mac and Macs are really slow So your numbers might need to be higher or actually probably your numbers need to be lower Otherwise everything's just gonna fly way way too fast So when I fire the missile not only do I have to set it to the same direction and heading and everything as the player I also have to give it its own momentum its own acceleration So I'm gonna do that with self Dot DX. This is a copy from where do we put that at somewhere this I'm just gonna go ahead and copy this line actually from the update here in the player class and Or sorry in this break class. Excuse me. So I'm gonna go ahead and just put that So it's going to take its current DX Which is from the player and it's gonna add its own thrust Let's go ahead and run that and see what happens Whoa, that's pretty funky. Okay, and you get this weird kind of I Don't know what you what you want to call that? But it is pretty cool So what's happening is what once we do this It's DX and DUI never really changed the DX and DUI keep Accelerating in that direction. So even though it bounces it bounces back towards the direction that it was initially Fired in okay, so what we do is in our update method We need to well we need to custom customize the update method. So I'm gonna go ahead and Copy the update method from here from the sprite class and Put that into here Okay, so update now I'm gonna go ahead and get rid of this So I no longer need that because the missile is gonna get its You know kind of juice from from that. Okay, so let's just go ahead and test this and make sure nothing else has changed Okay, okay, so you can see it's bouncing and But you see what's interesting is It's following the player's bounce which is kind of interesting Okay, but that's what we kind of expect to happen though to a certain extent Okay, so let me go ahead and oops. I must hit that twice. So we go ahead and Make a new method for this because one of the things you'll see is every time I push the space bar It keeps going. Okay, so I'm gonna have to make a few Changes here. I think at some point, but we'll leave that as it is now update Do I need to update the missile as it's if it's already firing? The answer is no So I say if self dot state Equals active So we only do update if the if it is active Otherwise, we don't need to do that. Let's go ahead and test it and see if it's working Okay, so it's bouncing, which is what we wanted. It's going that way But if you see whether the player is bouncing, you see how that affects the Now you don't want the missile to do that. So that's what we have to fix here in a second So because once it becomes active, then it's active forever So that's why I set up that the variable fuel. So how I did this Was I said self dot fuel Minus equals self dot thrust So the faster it is the quicker it uses up to fuel if Self that fuel is less than or equal to zero And that's when we're going to reset it. So I say self dot reset Okay, so now I now I need to add a reset method for the missile deaf reset self What I'm just gonna do is I'm gonna go ahead and type self dot fuel equals 200 Or what I have was one actually I want to fix that minute anyway, so let's leave that as it is for now I'll talk about why self dot DX equals zero because it's not gonna need to move anymore self dot dy equals zero and Finally self dot state Equals ready. So that'll let us fire it again once it has run out of fuel. Let's go ahead and test that Let's see what happens Okay, I've got an error, which is lovely self is not defined line 204. You can see I do that all the time It's really annoying self and run that Okay, so it's not really going very far. It's not lasting very long Now that'll differ a little bit on each computer some computers it'll last longer some Peters It will last less long I should say Now what's interesting well one thing I wanted to fix is you see how the fuel here I put it as 100 and down here ahead is 200. There's some inconsistency. So what we need to do is Make another variable called max fuel and it's let's make that 200 because that that works on my computer And then what I'm gonna do I'm gonna move this around a little bit and I would say self dot fuel Equals self dot max fuel So what's nice about this the max fuel variable is when we do power-ups We can add range to our missiles in the power-up by setting the max fuel to a higher number So when we reset it, it's gonna be self dot max Okay, so let's go ahead and run that and see if it's doing what we expected. So okay, it's firing. It's bouncing Okay, it's firing now. You notice again. This is I try to do this with realistic physics So I'm gonna go this I'm gonna go this way and bounce and then try and fire So you notice how it's going backwards because the player is going backwards. So you can either fix that by giving your Missile more thrust which would make sense you can make this eight, but of course then it won't last as long Okay, let's go ahead and bounce But notice what's going forward now I don't think you'll be flying around that fast in the game, but you might who knows that might be your thing That's no judging So yeah, it kind of gives you an idea of what you can do and again The idea is that when you start the game the missile doesn't go that far But as you progress and level up your missiles become more powerful You'll be able to fire multiple missiles at a time and just kind of make the game a bit more interesting So let's just review that real real quick. We have created a missile class and it is a Child of the sprite class. We added a few new Variables to it if you knew attributes state thrust max fuel and fuel and then we have a fire method which basically copies the current XY heading and DX and dy of the player and then adds its own thrust to that and Which gives it you know makes it go faster than the player We also have an update method that has an extra check to see if it is active and Each time it is updated. We subtract Some amount of thrust from the amount of fuel and then once that gets to zero We reset it and then we can fire that missile again. Otherwise everything else works the same again the reset method Once it's reset we set the fuel to the max fuel DX and dy to zero and the state to ready notice I didn't do anything with X and Y it doesn't really matter later when we code the collisions and things We'll make sure that it doesn't collide with something. It's not active and then in the render method again We only render it if it is active Okay, and that is that stay tuned for more