Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Exploring specialized (derived) classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we basically have a working game here, I guess one final thing to consider is what happens if there are no more creatures,
0:08 so we can come down here and add one more part after this section here and say if not creatures
0:15 and we'll leverage the implicit truthiness of collections, this is a list of creatures, if it's empty, it'll be false.
0:22 So then we'll say print, something like you defeated all the creatures, and then we'll just exit, like so, let's just test that super quick,
0:32 I'm not sure we'll be able to defeat the evil wizard of 1000 so let's just there is a theoretical chance but- it's not so high,
0:41 so let's go over here and just comment out the wizard for just a minute, so here we can attack all the creatures,
0:48 how many are left- oh, it looks like we defeated all the creatures, well done, game over, perfect.
0:53 So our game is working, but it's kind of blunt and basically the only factor we have to consider here in the entire game play is that number,
1:04 what is the level of our creature and what is the level of our wizard and then its randomness.
1:11 Do you feel like a fighting a toad should be equivalent of fighting the dragon
1:14 or fighting evil wizard should be the same as fighting a standard like bat, probably not, not only do they have different ways to fight
1:23 but the properties that you would take into consideration around how they would fight would be different,
1:30 maybe a toad's worthiness would have some sort of factor there and if a dragon had scales maybe you would consider like how thick his scales were
1:39 or if he was a fire breathing dragon or something like this. But if we just use our creature class let's go over to that
1:46 if we just use our creature class here, it doesn't model scales or worthiness or whether it breaths fire,
1:53 it simply is the most common denominator thing that models all creatures, right, they have names and levels.
2:01 And that's exactly what we want we want to have this sort of commonality so regardless the type of creature we are fighting, we can just use that, ok.
2:10 So let's add 1 more feature that will give us a little flexibility to this creature class and then we are going to look at
2:16 how we create other classes based off of creature and use inheritance and polymorphism in that kind of thing.
2:23 Let's add a method say def and we'll say get_defensive_roll() or something like that. Now up here we just had the creature do the roll directly here
2:38 but rather than do this let's comment this out, and let's say we'll go to the creature and we'll say get_defensive_roll(),
2:46 and it's up to the creature to take all of its attributes into account, if it's a dragon, let's think about its scaliness,
2:52 if it's a toad, it's worthiness or whatever, right, we can make our game behave exactly the same
2:59 if we just use sort of the same algorithm, we randomly compute our 1 to 12 number,
3:05 that's throwing a 12 sided dice and then we are going to multiply that by the level, and that's what the base creature does,
3:12 but maybe we'll do something different for a dragon, if we can invent this idea of a dragon and so on.
3:17 So let's just make sure we can still play the game, so we are going to look around and we see a level 1 toad so let's attack it, excellent,
3:25 toad rolls 2 let's look around again, now we see the evil wizard let's attack it just to see that it's throwing something high,
3:34 oh we've been defeated, it rolled 5000, of course we've been defeated. So now let's go back and think about our creatures here.
3:44 We want to categorize these different things, these different fighters into more specialized versions of creatures,
3:53 so maybe a toad and a bat maybe we would actually want to call these like small animals here, so let me just sort of sketch this out,
4:02 so maybe these would be something like small animals, and small animals let's see, small animals would have the same properties as the creatures
4:10 but maybe they would have some diminished sort of throw to save or as the tiger, maybe this is a predator, dragon maybe this would be a dragon
4:21 and maybe to the dragon we could say scaliness = 20 or whatever a scaliness factor is right and that might be considered when the dragon is fighting,
4:32 if it has more scales it's even harder to beat and then similarly maybe the wizard maybe it has some type of magic
4:38 and it somehow takes that into account as well, right, so maybe we put a wizard here, this isn't going to turn out really well for us, so far,
4:47 first of all we don't have small animals, right, these are not a thing we are modelling, and we don't have a dragon,
4:56 let me just comment those out for a minute, we do have a wizard,
5:00 now if I try to run this it's probably not going to work so well, a wizard has appeared,
5:04 let's attack it; oh bam, the wizard doesn't have this concept of a defensive roll, Hum? all the creatures in our game are supposed
5:15 to sort of have this commonality of everything that is a creature and then some specialization in them.
5:21 So let's take a moment and look at this core concept called inheritance that allows us
5:26 to sort of layer on this specialization and share attributes and behavior across these different types of creatures in our game
5:33 and then we'll come back and add it to our classes.


Talk Python's Mastodon Michael Kennedy's Mastodon