 Okay, welcome to basic animation in Python 3 part 6 and for now. This is the last part So in this part, we're gonna learn how to use a class And objects to make our code reproducible to make it easier to manage and to maintain And this is pretty cool stuff. But like I said at the end of the last video This is a bit more advanced. You may not be ready for this You should know something about classes before coming into this. There's a really good Class tutorial by Socratica. If you look that up, they have a really really good one It's just what I show my students actually. Okay, so let me just show you what we have so far in case you're coming back from a break We have a basically a little animating character here, and I've called it player even though it's actually the enemy in space invaders but those are the gifs I had so We've been using this whole thing the idea of a frame like the current frame that you're on And then frames for animation. So again, we could have invader 3, invader 4, invader 5, whatever This code will work for anything. But what we want to do is we want to create a class And a class is simply a piece of code that defines how an object operates So watch what I do. There's actually quite a bit of advanced stuff here. So I'm going to go ahead I'm going to go create a class I'm going to call it class. I'm going to call it player And I'm going to put here turtle turtle turtle Again, as I mentioned earlier, this is quite a jump up in concepts and coding So I'm saying the player is a turtle. It is a child. It's called a child of the turtle class And then I need a function here called init. This is two underscores not one. I get that question all the time It's two underscores before and two underscores after And then I'm going to put in here self. Self is a little hard to explain But in the case of player, self would be the player. If I make a player two, then self would be player two So with the turtle, I'm going to do a couple things. I want to put here self.pen up I didn't put that in the last one because we didn't move it, but it'll come in handy a little bit later And then I'm going to basically kind of do this stuff here And what I'm going to do is I'm just going to copy that I'm going to cut and paste, actually But I do need to make some changes Okay, so watch the indentation I'm not going to use player here. I'm going to use self So what I'm saying is that this player Has a shape this player has a color this player has frame and this player has frames I did miss one thing actually here, sorry turtle.turtle Dot again two underscores in it underscore underscore self and what that does is it initializes that's what in it means this turtle object And then I'm initializing the player again if you I don't this isn't really about classes This is if you know how to use classes, we're going to use it here So what I want to do here is I'm going to say in my player I'm going to say it's a turtle turtle. I'm going to say it's a player And it's automatically going to be a turtle And then when I create this this method is called It's a special method called init and has two underscores before and two underscores after All this code is going to run. So when I say player It's going to say it's a turtle. It's going to initialize the turtle It's going to player dot pen up player dot shape player dot color player dot frame player dot frames So that self is replaced by player Now The thing about classes is that we want them to be self-contained So basically everything they do should be inside that code. So for that reason, I'm going to copy this animation code And I'm going to put it up here and then I'm going to indent it so it's inside The class Okay, so you see how it's lined up These two functions are lined up these two methods Okay, now because I'm inside a class. I can't use player anymore. I got to use self You'll see why in a few minutes I hope Actually and then the second thing I want to do here is I don't want this to say player animate anymore I'm just going to call this animate I'm going to make it a little bit more general And in this case, it's got to be self dot animate because I want the player to animate itself Okay, and then down here This is going to be out now now that I'm outside the method. It's player dot not underscore dot animate Okay, so I'm going to run that And hopefully that will keep working Error animate takes okay, so when you see this error animate takes zero positional arguments But one was given in this particular case Because I'm inside of a method. I got to put self Okay, again, it's a bit much to explain here, but uh trust me on this one So I hit function f5 in genie And now it's running Okay, so you may be wondering why do we do all that work to convert it in all that work But why do we convert it into a class? So if you remember what I said earlier Um, what if I want to have two players or three players or four players? um In the original code I would have to copy change everything to player two if I want player three But what I can do now this is where it gets really cool. So I can say player two Is a player Okay, that's where this player comes from here and then I'll say player two Go to I'll have it go to zero Let's see 100 Actually, I'll have it go to minus 100. We'll do it keep on the same level. So minus 100 and zero And then I'm going to say player two dot animate Let me put this up here It doesn't really matter but I'll put it up here so it's consistent Okay, now I'm going to go ahead and run that and watch what happens Now I've got two animations going With the same code, which is what's really cool So instead of having to copy and paste all that code change player to player two Because I'm using a class. I'm saying that this player And this player player two and player they are behaving the same way. They have the same methods They could do the same things So then now I can just I basically have unlimited I can go ahead and and do a player three now. Everything does have to have a unique name Uh, I'm gonna put this at 100 zero. I'm gonna head and run it Okay, so this is this is the original player. This is player two and this is player three And you notice that they're animated at slightly different times because they started animating at different times So this gives your your your animation that makes it a little bit a bit more realistic They're not all bouncing at the same time or changing at the same time So, yeah, that's it. That is my basic animation tutorial Again, the use of classes is really really really important So the sooner that you can master this concept or at least become comfortable with it The better And again, there's a lot of stuff in here. There's the list stuff And and the frame using frames and all those sorts of things But I think if you go back and watch it carefully And go through it, you'll you'll get it and if you're having trouble with some of the the coding concepts You definitely want to check out my basic python three Tutorial just because it walks you through all of those things except classes Again, I use look up the socratico one if I remember I'll put the the link down below. Okay, so Yeah, that's it. I hope you enjoy playing around with that again Use gifs you can have as many of these as you like In this list and it will work because the code checks for the length of the list Um, this is yeah, this is pretty cool. It's pretty self-contained and I hopefully it is straightforward for you So thanks for watching. Uh, subscribe if you haven't if you are having trouble Feel free to post in the comments But please note that I need to see the code. I need to know any error messages that you're having Otherwise, I cannot help you. So anyway, keep on coding. Take care