#100DaysOfCode in Python Transcripts
Chapter: Days 13-15: Text-based games (and classes)
Lecture: Demo: Implementing the game play

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we've created our actors in the game, our creature, our dragon, and our wizard, let's go and actually use them to put the behaviors
0:10 or the implementation of the game together. So over here, let's also import wizard and dragon. Now down here, these bats, and toads, and tigers,
0:20 they're fine, but this one, we're going to create a dragon, and this will be called the black dragon or something like that.
0:27 And notice PyCharm says the black dragon actually takes, breathes fire and a scaliness. Alright, so let's do that. Let's say this one,
0:35 its scaliness is 2. We can make that super explicit and I'll say breathe fire, false. Okay, so we have a chance to defeat it, but it's still,
0:43 it's going to be tough. And here we'll make this a wizard. This is an evil wizard. So we can have our standard concept of a creature,
0:52 or we can have these specialized ones, but we're going to end up treating them all as creatures. We don't need this. We'll come over here,
0:59 and now we're going to create our hero like this. And his name is going to be Gandalf, and his level is going to be 75.
1:10 So, it's going to be a little tough for him to beat that, he should be able to beat this wizard, he's got no chance.
1:16 Well, he has a chance, but it's highly unlikely, okay? Alright, so now we need to randomly choose a creature.
1:22 Now let's go up here and say, import random. There's a really great way in Python to randomly choose a creature. You could say, create the integer,
1:33 figure out how many there were, use the index, end to this list of creatures. Or you could just say choice, and say,
1:39 give it the creatures, and given a collection here, and we'll just grab one. Okay, so now we need to print out some information about the creature.
1:48 So we can say activecreature., now we're not getting very much help here from our IntelliSense, but that's okay.
1:57 So then we say name, activecreature.value, and that's going to print out a nice little thing. And we should be able to actually run it now.
2:04 Let's go ahead and run it and see what happens. If it's nice and big, not so happy. Where did we make this mistake? I typed value, this would be level.
2:15 Okay, a tiger of level 12 has appeared. Okay, so run away. The wizard runs away. A black dragon of level 50, has appeared. Run away.
2:25 The evil wizard, definitely ran away. Okay? Then we just hit Enter to exit. So it looks like our actors are working.
2:32 All we have to do is have them battle, so if they say attack, we'll just have this. If a hero.attack(activecreature), if that's true then they win.
2:43 So what we actually want to do is make them leave the game. So we'll say creatures.remove(activecreature) and then we'll say print,
2:51 something like, the wizard defeated them. So we'll say just something like that, the wizard defeated such and such. And we're all good there.
2:59 Wizards run away, there's nothing to that. If the wizard looks around, we just want to show all the creatures. So that's easy.
3:05 We'll just say, for seeing creatures. Now we'll do a little format string. Like this, and so we'll say, see.name, see.level. And notice,
3:19 all we're using are the features of the base creature. Not the scaliness, not whether it's breathing fire,
3:25 we're just talking about the standard creature, which means we can treat these all uniformly. Alright. That looks like we've written the game.
3:34 I think we're done. I think the last, final thing to do is to play it, and it seems more fun to play if we play it full screen.
3:42 So, let's say this, Python 3 a lot. Excellent, an evil wizard has appeared. Shall we try to attack it? I'm sure we'll lose.
3:52 Oh, we're not printing anything if we lose. Alright, let's do that one more time. So we'll say, if else print the wizard has been dealt a defeat.
4:04 Alright, so the wizard has been dealt a defeat. Now the wizard doesn't actually lose. The game's not over. Maybe it could be, but right now it's not.
4:13 So we could attack, first let's look around. Alright. So we see the bat, the toad, all the things. Now if we attack the toad, we should defeat it.
4:23 We do, luckily. If we look around, you'll see now the bat, no, the toad is gone. This level 12 tiger, we can attack it, and this wizard here,
4:31 let's run away. We can look around again. Now we just have the bat, the dragon, and the wizard. The wizard we're going to run away from.
4:38 The bat we can attack. Now what's left? Just the dragon and the wizard. Alright, we're going to run away from that. Maybe we can beat the dragon?
4:45 The wizard has been, guess I got a spelling there, the wizard has been defeat by the powerful black dragon, has been defeated. And we probably,
4:54 it would be nice to see the two roles that came back, but that's okay. Alright, so if we look around, there's still that. Run away, run away, run away.
5:01 Okay, maybe we beat the dragon? Yes! Now if we look around, all there is this, the chances that we beat this evil wizard,
5:08 not so high, so we're just going to leave. But we've defeated all the other creatures.
5:14 There you have it. We've built a game and we've modeled it with classes and objects.


Talk Python's Mastodon Michael Kennedy's Mastodon