 Okay, everybody welcome to space arena the ultimate python total graphics game tutorial part five in this part We'll be working on our player spaceship and making it move like a spaceship So this is going to take a little math little physics and a little bit of luck So let's take a look at what we have so far We go ahead and run it and sometimes things get stuck in genie on the terminal Just hit enter this turns back to black and you are good to go and it works So you can see I've got my spaceship in the center. It's rotating But one problem is before we do anything else is that it tends to Well, we can't tell which way it's facing because it's an equilateral triangle. Let's go ahead and fix that first So I'm gonna go down to my player and this is where one of those cases where the player is rendering different from the other Sprites, so we're gonna have to make a separate render method for the player now if you I hope you watched the last video and if you didn't watch it to the very end You should go back and watch that because I did make a few changes kind of last-minute changes To hopefully clean up the code, but if you stopped early, you might have missed a few of those So that's why your code might look a little different to mine, but just make sure you go back and watch that So what I'm gonna do is I'm gonna go ahead and I'm gonna copy the render method from the sprite class And as we mentioned before the player is a child of the sprite class so I'm gonna go ahead and add that method in there and And What I need to do is I'm gonna use a method of the turtle module in this case the turtle object pen and it is Think what is it? Shape size, I believe yes shape size and you can see here here the arguments I need So self is gonna come from the pen Stretch width so I want it to be a little bit narrower in width I'm gonna keep it 1.0 in height and I don't want a border So you have to remember that the default Shape of an object in the turtle module is 20 by 20 pixels So it'll be 20 pixels high by 10 pixels wide. Let's go ahead and run that and see what happens Okay, so you can see now I can tell which way my spaceship is facing But you also see how these shapes got kind of transformed as well So what I'll need to do is once I'm done with this spaceship is put everything back the way it was So shape size 1.0 1.0 and no border. It's when we go ahead and test that And you can see now just the spaceship has changed Okay, so next up. This is where we want the spaceship to move around like a spaceship so We have to think about the physics of it and how that works and Space you have inertia and all that kind of crazy stuff. So what I'm gonna do is I'm gonna create a new method called Accelerate and it's gonna be for the player and what I'm gonna do here is I'm gonna do a hit. I'm gonna have to do something with thrust and the acceleration and explain that a second self dot thrust plus equals self-dot acceleration Now if I run this I'm not sure if we're gonna get an error or not I'm gonna get an error, but we will get an error later because thrust and acceleration aren't defined So what I'm gonna do is I'm gonna go up to my sprite class And I'm gonna define this thrust equals 0.0. That means the engine is not there's no momentum in this object, it's not moving at all and self-dot acceleration is 0.2 now I just experimented until I found a number that worked with my system and seem to make sense So thrust is the amount of force that the engine is putting out at a certain time Acceleration is the change in that thrust So if you fire the engine for one second, let's say it gives you 0.2 acceleration If you fire it for or give 0.2 thrust you fire it for two seconds It gives you 0.4 then 0.6 0.8. So you have more and more Thrust going into your system now what we need to do with the player is to use that idea Now here's where it gets a little complicated. This is this is where the math really comes in So I'm gonna go up to my update method. So this is what controls How the object behaves okay each tick through the game loop This is called So we need to update the x and y location We need to upset update the heading based on the dx and dy I'm gonna change the order of this around a little bit. I'm gonna add a little bit of code and I'm gonna do self dot heading percent 360 percent equals, excuse me 360 and what that does the this is called the modulus and once my Heading gets to say 370 I don't need it to be 370 It's actually 10 because it's a circle it wraps around and this will wrap it around for you Just trust me on that one. Okay, you can Google the modulus if you're not sure what that happens Now this is where the math gets a little crazy So my dx is Changed By math cosine math that radians self-dot heading times self-dot thrust and Self-dot dy Plus equals math that sign math radians self-dot heading Times self-dot thrust Okay, so this is a bit of mathematics. Let me run that and show you what happens. We're gonna get an error, but Okay, and this is something when you get an error. You got to figure out what the error is So you see line 47 there's line 47 says here's the actual line It says math is not defined. It doesn't know what math means Math is a module that comes with Python, but it's actually not part of the standard Python like itself So we need to import that just like we did the turtle module So I'm gonna go ahead and import math. I'm gonna run this Okay, and Okay, so I I don't get an error, but it's not working because I forgot to do the keyboard binding So we go down to here and I do win dot on key press player dot accelerate And I'm gonna use the up arrow Let's see what happens there So when I push the up arrow Okay, so it's uh Okay, so it's it's flying all over the place because what happens as soon as I push that up arrow It kept accelerating that then the the speed kept going up and up and up and up Okay, so what I got to do is win dot on key press Player I'm gonna call it decelerate even though it's not technically correct physics wise, but I'll just go ahead and use that makes it easy. So when I accelerate So def dot Find decelerate Yeah, actually what I'm doing is turning off the engine in this case because it's gonna keep moving self dot thrust equals zero Okay, so what that means is that the engine is no longer Pushing the spaceship. However, because we're in space. It's gonna keep going because of inertia Let's go ahead and test that again So I'm gonna go ahead and turn Oops something went wrong Yeah, stop and take a look at that and what did I do wrong zero point zero? I'm sure that's correct and Hmm so this is where we got to do a little bit of thinking about what we did and what we did wrong and Why were bad people that our program isn't working? So let me go up to this section. Make sure that's correct Yeah, so plus equals that's really important. I've forgotten that many many times and It's good. And if we go to self that thrust plus equals self that acceleration correct and decelerate self that thrust equals zero. I think I'm pretty happy with that Let me test that again because that's I think that's correct Okay No What did I do wrong? Oh, they have an error. I don't have an error Okey-dokey ah This should be on key released Okay, so it's the accelerating. I was just decelerating the whole time So when I push it down, I want to accelerate when I release I want to decelerate Okay Okay, so you can see now how it moves like a spaceship. So until I Accelerate it keeps going in whatever direction it was going in Okay, et voila there. There you have it. So that's some spaceship physics and That is some pretty cool stuff. So let me just review that real quick so what I had to do to get the spaceship physics to work was Where's the code at I needed to add two new attributes Thrust and acceleration. So thrust is the force of the engine the current force or speed of the engine I guess acceleration is how much it's changing by so if I push it down a little bit It goes up to 0.2 0.4 0.6 until I let go and then thrust goes back down to zero and then We in our update method. So again, we're still changing the heading by our change in angle I just use the modulus to Turn like 370 into 10 because they're the same thing You don't have to do that But I just like to do it that way and then I used math the cosine and radians and All that kind of stuff to separate out the different vectors of dx and dy just real quick There's a couple notes on this radians is a different type of angle measurement So what we're used to is 0 is over to the right 90 is up 180 is to the left 270 is down radians are based on Pi so it's 0 over here. It's 1 half pi Here it's pi over here One and a half pi is down and then back to 2 pi or actually Zero in this case so this function math that radians converts our degrees heading So let's say 90 degrees converts that into which would be half pi like 1.4 something I think or 3.1. Yeah 1.6 something and 1.5 something and then cosine gives us the dx It's just some mathematical thing if you're curious about this stuff look up something called the unit circle And that kind of explains it a little bit better And we're just separating out the dx and the dy and then we're adding that to the current x and y coordinates and then of course we needed a couple of methods here in the player class To control the acceleration and deceleration and even though as I said sports not really deceleration It's just a lack of thrust and then the render method I had to change a little bit because we want that particular shape to be a little bit longer than it is wider And that gives us a bit of a direction. Okay, so yeah, that's a tough one Good luck. Stay tuned for more. Thanks