Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Let the Monster Attack

00:00 Okay, we’ve got a working game, but so far, it’s very unfair because the monster can’t even attack yet. So let’s tackle this next task that after the player, the monster also gets a chance to attack.

00:13 This ends the player’s turn. They either select "A", "H", or "R", and now after their turn, if the monster is still alive, it deals between 15 and 20 damage to the player. So this is, again, I’m going to use the random module to calculate the damage, and it’s going to be damaged to the player.

00:29 And first, I have a conditional where I want to check whether the monster is still alive after the player’s attack. And this should happen inside of the game loop as well.

00:37 So you need to indent this to keep it inside of the while loop. If the monster is still alive is basically my first check, so I’m going to say if monster_health > 0: that means the monster is still alive, and then it gets a chance to attack. And then we need to calculate the damage, so let’s call this one monster_damage,

00:59 and that’ll be another call to random.randint(), but now it’s 15 to 20. So the monster is a little stronger. I’m going to say 15, whoops, to 20.

01:10 And that’s the monster_damage. Here we’re calculating it, and now we have to apply it by reducing the player’s health. I’m going to say player_health -= monster_damage.

01:21 Similar to what we did up here when the player attacks, now we have also give the monster a chance to attack. -= monster_damage. Then let’s also print out the player_health to make sure that this is working as expected. Yeah, let’s give it a go.

01:41 Do you want to attack, heal, or run away? I want to attack, and you see, my option is attack. The monster’s health is at 135, and the monster attacked the player as well. So my health went down to 81. I can attack again.

01:54 All right. My health is getting dangerously low at 63, so I want to now heal. And it seems like my health went from 63 up to 78. Wait, is that the monster’s health? 135123.

02:10 Okay, so that shouldn’t have happened. If I heal, the monster’s health shouldn’t have changed. Let me try that again. Oh, okay. So, this is confusing. This is not the monster’s health.

02:21 This is the player’s health.

02:25 And still this value shouldn’t be— Also, I’m a little confused now because I don’t really see what I’m printing out, right? You see, I’m. like. kind of struggling to correctly interpret what these things are, so I would say this is high time to make the output a little bit more understandable.

02:40 But let’s assume for now that this is working, and we’ll figure out whether it really does after we’ve updated these print() calls to make them a little bit more descriptive.

Become a Member to join the conversation.