#100DaysOfCode in Python Transcripts
Chapter: Days 13-15: Text-based games (and classes)
Lecture: Demo: Building our classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, so here's our general program. Let's put that aside for a minute, and go work on the various creatures that we're going to model.
0:07 And I'll call this actors to kind of say, here's where I'm going to put a creature, the wizard, and so on.
0:12 So what we need to do is create what we talked about, something called a class, and this is going to be the blueprint of a creature.
0:18 So, the way you create a class in Python is really straightforward. You say class, and then you say the name of it.
0:24 Okay, we're going to model everything at the lowest level as a creature, and use a colon to define the block that is the class, like you do
0:32 all the various blocks in Python. Now, there's a couple of things which we can do to get started, but, commonly, you want to store
0:40 pieces of data and behaviors with the creature. So, for the data part we're going to use what's called an initializer.
0:46 So we'll say def init, and there's this dunder init, in fact if I say double underscore, these methods are called dunder because it's
0:53 double underscores on both ends. These are all part of what makes up classes, and they all have special meaning.
1:00 The one that we care about is this init. This is very common. So down here, we might want to have a name for it.
1:07 Equals, let's call it, toad, and it might have a level. And the level of the toad is 1, or something like that. Now, this is not really helpful
1:16 because every creature is going to be a toad of level one. So what we can do is, we can when we create them say, this particular creature is a tiger,
1:24 this creature is a bat, and so on, and so we could pass in the name here, and we could pass in the level. So we're going to go like this.
1:32 So, here we've defined this blueprint. What is a creature? A creature is anything that can be created and given explicitly a name and a level.
1:39 We could go farther and give those types, as you can in Python 3, but we're not going to do that right now.
1:45 The other thing, this is the data part we have so far, the other thing is going to be some kind of behaviors around it.
1:53 So, a creature, can, let's suppose that one creature can be defensive against some kind of attack. So we could say it like this.
2:03 So the creature's going to roll a defensive roll. And now, you might want this to be somewhat based on the creature's level.
2:11 So let's create some sort of randomness. Let's say roll equals, well I'll go over here and we'll say random. Now this comes from the standard library
2:19 so we have to import it here, put like that at the top. Then we can say randint(), and you give a lower bound,
2:27 of let's say 1 to 12, and if you want to know whether this goes from 1 to 12, 1 to 11, and things like that, we can say view quick documentation.
2:38 And what does it say? It returns a number such that A, the number that comes back is less than or equal to the upper and lower bound.
2:46 Perfect. So this is our, let's say, 12-sided die. And then we'll return roll times self.level. Anytime you want to refer to your own items,
2:56 your own values, you have to say self dot. So to get back to this, we say self.level. Alright, so this is going to be some kind of defense here.
3:06 Now let's go and create our various creatures in the game. There's this little to do here. We had a couple of things, we had a bat, we had a toad,
3:15 we had a tiger, we had a wizard, an evil one, and so on. So we'll type creature and of course
3:21 we have to import that at the top here, just like any type. We go like this and if I ask Python to show me the parameters, you'll see it takes a name.
3:31 So let's call this a bat, and this is a level 5 bat. I think we had a toad which is a level 1. And we had a tiger which was level 12, let's say.
3:42 And we had a dragon, which is a level 50. And for now, I'm going to put it this way, evil wizard was a 1000. I think that's what it said.
3:54 Okay, so if we just run this and we could print out our creatures, we need to make sure that we're running the main.
4:03 Remember, up here, at the very top, we wrote this, but at the bottom we have to do our little main.
4:08 Like this, to say are we running this script as a program? Or are we just importing it? If this is true, we're running it
4:14 as a program, then we want to invoke our main method. So let's go over here and say run, and there's some stuff that
4:21 went a little bit crazy somewhere along the way because we didn't finish it. But here, you'll see that we've created a creature object here, and a
4:29 creature object there, and so on. Our little creature part worked, but then the code that we sort of commented out below, isn't quite done.
4:39 So this is close, but remember I said you might want to have special features. So let's just focus on the dragon for a minute.
4:46 You might want to have special features that take into account the scaliness, the fire-breathingness,
4:50 have a different mechanism for defense, and so on. So let's say we're going to have a class called dragon.
4:56 And the dragon also is going to have a defensive roll, like this, and maybe the dragon also wants to have this information. Woops, copy that.
5:06 So the dragon is going to go like this, and then it's going to copy it, and this seems really, really tedious. It is and I'll fix it in just a second.
5:13 You'll see that we can model this differently. But when we create a dragon, it will have this. And let's also say that it has
5:19 a scaliness factor and breathes fire. Alright, so we want to store these here, so I'll say, "self dot", that equals that.
5:35 In fact, in PyCharm you can hit alt enter and it will even write that whole bit for you because it knows that this is what you should do.
5:41 But you probably see a lot of duplication here, and here. And if I want to change something about this, like add a feature to all creatures,
5:49 well I'll have to go in and add it everywhere, like this. So, we'll be able to something a little bit better here in just a second.
5:55 But let's go ahead and write this. Let's take that and let's say we're going to multiply that times the self.scaliness, let's go that equals that.
6:08 We'll say if self.breathes_fire, then value equals value times 2. So it's even worse, stronger if it breathes fire. Okay, great, so there's that.
6:21 Now, this is not really super helpful. Any change we make over here, we would kind of like to copy them over there, and really, we would like
6:32 to treat the dragon as just a specialization of a creature. Remember, this is a relationship. So we can model that by saying this dragon is a creature,
6:43 like this, alright? And when we say that, what it lets us do is actually take on all the attributes here. Notice this is giving us a little warning,
6:53 it says, "you need to say super dot", and in pass the name and the level. And this basically says we're going to run
7:00 this bit of code, and we don't actually need this stuff here, we're just going to store let it pass on through, then we're just going to store
7:07 the things that are special about the dragon. Similarly, this right here, we could say the role is actually whatever the regular creature does.
7:21 And then we could say, okay we're going to do that same thing, and we're going to factor in the scaliness and the firebreathing, here.
7:29 You can see right here that PyCharm is saying that you're actually getting this detail here from your creature class that you're driving from.
7:37 Okay, so this is all well and good. Let's do one more thing. Let's have a wizard. Now the wizard is also going to be a creature,
7:50 and the wizard actually has no special items or a passing to it, so we can just leave this init thing off. We're just going to add one behavior
7:58 to the wizard, which is going to be attack. So he's going to have a creature he's going to attack, and it's going to return True or False.
8:05 So it'll go something like this. Now notice, both the wizard can get this defensive role, or offensively, but it's fine,
8:15 and the creature also knows how to get a defensive role. And we'll just compare those, we'll say return,
8:20 my role is greater than or equal to their role. Alright, so what we're going to do is have the wizard attack
8:29 another creature, and then if we roll something higher than we win, otherwise we lose. And we're indicating that by returning True or False.
8:36 Alright, so we were able to take on a lot of the features and the blueprint of the creature to get this defensive role, and all we're doing is adding
8:46 an attack mechanism. Whereas the dragon, we said we're going to change the way defense works for dragons as well as, like, storing additional stuff.
8:54 So we're able to model these various actors in our game, all the while keeping a sort of common functionality so they can interact with each other.


Talk Python's Mastodon Michael Kennedy's Mastodon