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