Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Modeling with classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we have the basic structure of our game built and now it's time to start working with the actors and the payers in the game,
0:07 the wizards and the creatures and defining them and their relationships, and that's where this is going to get really interesting.
0:14 We are going to move this stuff around in the end, but let's put it here for now, we are going to have a set of creatures
0:19 let's just take this a list in the beginning and we'll figure out what goes into this list it'll be empty for the moment
0:25 but we'll come up with some creatures to put in there and we are also going to have a wizard and let's call it our hero,
0:29 and we somehow want the thing to represent the wizard. This hero is going to have to have actions and it's to battle the creatures,
0:36 and it's going to have to have data like the remaining hit points or the level or something like that. So let me just write none for a second
0:43 so say this points add nothing until we get the chance to define how we are going to represent the wizards.
0:50 So I could come over here and I could write the code, just in this space, right here. I can create a class that represents the behaviors
0:59 and the data associated with our hero wizard. But I don't want to do that, let's try to organize this a little bit better,
1:05 when I have a lot of pieces involving the creatures and the wizards and so on so let's come over here and make a new Python file
1:12 and we'll call it actors, we'll call it creatures or players or whatever. One of the real powerful ways we can model both behavior and data in Python
1:21 and in many programming languages is with this object oriented programming concept, and we start by creating a blueprint
1:28 from which we will build out instances, of these classes, we'll build out a particular type of wizard,
1:35 maybe we will have a hero wizard and an evil wizard or something to this effect. So the blueprint is called a class, when we define it it's just class,
1:45 remember, we used def for methods, for classes, we just say class and then we just give it a name, notice, in Python most things have lower case names,
1:54 game loop, creatures variable, hero, keywords and so on, but for classes, they are typically cap words style,
2:02 so like Wizard or earlier we worked with Beautiful Soup, where the B and the S were capitalized, right?
2:07 So we are going to define a Wizard, and we'll name it like that and we can just say pass to say we don't want to define
2:15 any data or any behaviors in the beginning. Let's do the same thing for creatures, we'll create a class called creature
2:22 and we'll just say pass for the moment so let's go back here and we'll use those, instead of saying none, that wasn't so amazing,
2:30 I'd like to say Wizard, well, that's not working so well, PyCharm says I have no idea what this means wizard,
2:37 just like any function or variable defined it in another module we have to import it, so let's go at the top and import our wizard.
2:44 Typically, I prefer the type of import that retains the name space so it's very clear where something came from,
2:50 so I can say actors and then down here I could say this I could go over here and say actors. and then you could see my creature and wizard
2:57 and the way we create an instance of a class is we call the initializer just like this.
3:04 Now wizards and creatures are so fundamental to the behavior of this game, that I am going to use a different import,
3:10 I am going to come over here and say from actors import and then we get a list, what do you want to import,
3:18 wizards and let's go and import the creature while we are at it, so if we write it like this then we don't have to use the name space,
3:23 we can just come down here and say wizard, in fact we are not even allowed to use the name space.
3:28 In here let's put some creatures, so we'll create a new creature, what we want to do is initialize this
3:33 with the various specific details of this creatures, so remember we had a toad, he was pretty weak, we had a tiger,
3:40 we had a dragon, we had an evil wizard, we haven't set that part yet so let's just put a bunch of creatures into our list here.
3:46 So now we have a list with five creatures here and we have one particular hero. Now if you wonder why these are gray and have squiggles
3:54 that's just because we are not doing anything with them, yet. Let's go and run this and make sure things are hanging together.
3:59 All right, if we get to this far, things must be going great.

Talk Python's Mastodon Michael Kennedy's Mastodon