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