 Hey, welcome to space arena the ultimate python turtle graphics game tutorial part 4 in this part We're going to be creating a separate player class And you're probably asking it. Why would you do something like that? And the reason we do that is that although the player is a sprite It's gonna have some different behaviors to the enemy. It's gonna have some different behaviors to the power up It's gonna have some different attributes and things like that Okay, so in the case of the player The player is gonna have a heading in a direction and things that the basically the enemies and the power-ups don't really need So we do sorry. So to do that to fix that We're gonna create a separate player class now what we could do is I could just go ahead and copy this and Paste it to make the player class that would work But that's kind of an inefficient way of doing things because we don't want to Delete what's there. We just want to add something to it So watch what I do here. I'm gonna type a class and make a class called player and watch what I put in here Sprite so this is a concept called inheritance and you notice up here, okay, so I got my player class I got my sprite and So just so you know, everything's kind of appearing as I type it What I can do is when I do my constructor In it Self I'm gonna do the same XY shape color that I did before Now I can change these but I'm gonna keep it the same for now Now watch what I do here. This is gonna. It's kind of interesting. I want to type sprite dot in it And then I'm gonna type in here self Zero zero And let's see for the player the shape is gonna be triangle And the color is going to be white Now I could actually go ahead and just I could actually go ahead and do this shape and color Let's do it that way. Keep it easier for you So then down here, I'm gonna go ahead and type player Oops. Okay. Now I'm gonna go ahead and run this and see what happens Notice there's been no change to the behavior. Let's watch that. Let's look take a look at that again So what I've done is I created a player class That is what we call a child Excuse me. You might hear it called be called a subclass as well of the sprite class So what has happened here because I initiated or initialized as you say instantiated the parent class Sprite and sprite And there's a different way to do it here, but I always like to do it this way So it's very clear which with parent classes. I Have said that the player is a sprite So the player automatically inherits, which is the keyword inherits all of this from its parent class But what I can do now is I can actually add things to the player So for example, the player might start out the game with three lives The player is gonna have a score when it starts the game Which is pretty darn cool okay, so right now what this tells you is that all Sprites have this in common, but only the player has lives only the player has a score So what this allows me to do is to add methods to that the player uses But the other sprites don't use so this is a concept again called inheritance where you have a parent class That has the basic attributes and methods the child class inherits those attributes and methods But then adds its own attributes and adds its own methods So I'm gonna go ahead and use that to get our player Moving a little bit better. So I'm gonna go ahead and add an attribute self dot heading and When I start the game, I want it to be facing up Okay, so zero is to the right 90 is to up 180s to the left and 270 is down And I'm also gonna add something called DH, which is the rotational speed Okay, I just let's drop DA instead of DH So that's the change in angle. I should say it's probably a little bit better So oops This is what happens when you talk and type at the same time Now what I want to do Is this is the delta angle So now I need a different update method for the player So I'm gonna go ahead and copy this Because now the player has something different we say self dot heading plus equals self dot DA So the heading is gonna change by the delta of the angle All right, so Let's get a little interesting now. One thing I missed here in my rendering well, it is Pen dot set heading Actually now I'm gonna say what I'm gonna do that there. I'm gonna go ahead and do this down here I'm gonna go ahead and change the render method as well Let's do it that way. I'm gonna say pen dot heading Actually, it's set heading sorry set heading to Self dot heading. I think that's going to work Let's go ahead and see what happens We should see Same thing I think Okay, so it's sorry, but notice how it turned up. It's now facing up a little bit So what we need to do Now this is where it gets a little bit interesting I'm gonna go ahead and Set the players DX and DY to zero Okay, so I'm gonna go ahead and let me just take out these lines. We don't need those Is that what's that's it? Yeah, so I don't need that. I just need to did needed to not do that Down below so we don't want to add any extra code. We don't need now Let's go ahead and rotate our player So what I'm gonna do is I'm gonna add a method I'm gonna call it def rotate self and When I rotate left, I'm gonna say self dot DA equals 10 And I also do Rotate right self say self dot DA Equals negative 10 now notice. This is a little counterintuitive You would think that positive numbers rotate right and negative numbers rotate left. They do not So what I need to do at this point Watch this anyway, why don't we go ahead and just set this to 10 see what happens So we'll just set it to 10 from the start and watch it Okay, that wasn't supposed to happen That's pretty cool Okay, so basically for these we have to set the heading to zero That was cool and set heading to zero because they don't they don't have really have headings Yeah, okay, so now my player is rotating and we don't want that to happen the whole time But that's the gist of it But we don't want the other players the other sprites to rotate because what happens is because we're using the same pen Once something's rotated it stays rotated. So we have to reset it back here Okay, so this is kind of part of the programming process. You test it see what's going on and kind of figure out why it's happening Okay, so I'm gonna go ahead and set that back to zero And now this is where it gets a little bit interesting What I want to do is I want to be able to use the arrow keys to rotate left and to rotate right So what I'm gonna do is I'm gonna go down here and I'm gonna go ahead and do the keyboard bindings And this is specific to the Python turtle module So you have to do win that listen and this sets it up so that listens for the keyboard and Then on key Press So when I press the key, I want it to call player dot rotate Left and The key I'm going to use is the left arrow key. So it's capital L E F T and Then win dot on key press player dot rotate Right, I'm gonna use the right error. Let's test that and see if it works Okay, turning turning actually turning really fast, which is which is fine Okay, so you can see how this is working. I've created a method in the player class Called rotate left and what that does is it sets the da the delta angle to 10 So that means each time step through here. It's gonna rotate 10 now Your computer might be faster than mine. You might find that 10 is way too fast and you might want to set that to 5 Okay, so you'll have to play around the numbers a little bit so that it works as you want it on your computer Okay, so this this might be a better speed actually five, but we can play around with that later once we get the game set up now Probably what you want to have happen is When the player releases the key you want that to You want the rotation to stop so let's go ahead and make a new method called def Stop rotation self and then we'll just say self dot da equals zero And to get that to work what we do is down here I'm gonna go ahead and copy this and set up on key press. I'm gonna use on key release And I got some noise outside and the method is going to be now stop rotation stop rotation and Stop rotation So when I release the air left arrow stop rotation when I release the right arrow stop rotation, let's test that Okay, so it is working. Okay, which is pretty darn cool All right, they want to stop stop that for now The next part requires a little bit of math, but let me just review that real quick And it was a lot but again, this is not a beginner tutorial. This is for intermediate programmers So I expect that you would know some of this already. So we've got a sprite class Which is our parent class of the player class So because this is the parent class and we've initialized it the player class inherits All this stuff from the parent class already So the x-coordinate the y-coordinate the shape color dx and dy It also inherits update and render But you can see here we've actually changed the update because it has I'm gonna put it because the player Has an extra attribute that we need to account for so I need to make my own update method here And also because the heading I Needed to go ahead and Change this now what I could do actually let's go ahead and do that real quick I'm gonna go ahead and change that Is I'm gonna go ahead and give Each sprite a heading so self dot heading equals zero and Then what I can do now is I go self self dot heading There and since the heading is gonna be zero it'll be fine So I'm gonna go ahead and get rid of this render method for now So that way I can cut down on the code a little bit and keep it a little bit simpler But in the update method, I do need to add this Because there is no rotation for these objects now what I could do I could go ahead and do self dot da equals zero and Then if I do that then I can put this up into This and then I can just get rid of this This is actually probably a better way to do it I may end up needing me to add my own methods later, but for now let's go ahead and just do that Let's let's try that So I hope you didn't stop watching it that before I did that. Okay, so I'm testing it. Everything's working exactly as Expected and actually I am gonna need to change that update method later But we'll get to that in a future video. Okay, so again, hopefully this is making some sense to you If it doesn't I would recommend watching this video again If you don't understand how I did what I did here the rest of the program is not gonna make any sense So if you have any questions, you know shoot them down below and I'll try to answer them as best I can Okay, good luck. Stay tuned for more