Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Implement Attacking

00:00 Previously, we created the option to attack, heal, or run away. It doesn’t do anything yet, but I’m going take this off still. And now we move into the conditionals.

00:09 You don’t have to do this in the order, but I think it’ll work, so I’m just going to start with implementing attack. So, if the player attacks, they deal between 10 and 15 damage to the monster.

00:19 I’m going to need some way of deciding what’s it going to be. And I think it makes sense to use the random module here again. So I’m going to import random.

00:28 What else? Let’s move it over. Make clear what we’re working on.

00:34 It’s the player attacking and dealing damage to the monster. So I will not have to change player_health, but I have to change monster_health and subtract whatever damage gets calculated using random.

00:47 First, I need the conditional to say this option should trigger if the player pressed A for attack. I’m going to say if action == "A" because I used .upper() to uppercase this.

01:00 Right over here you do this.

01:04 If the action is "A", then I want to first calculate the damage. So I need a variable for damage that I’m going to calculate with random.randint(), passing in between 10 and 15.

01:18 So this is inclusive, so I can send (10, 15) just like this. That’ll be my damage, so every time the player attacks, there’s going to be a new random amount of damage between 10 and 15.

01:31 And now I want to reduce the monster_health by that damage. monster_health -= damage.

01:42 And then let’s try it out. Oh, I also have a typo in here. monster_health. See what happens when we attack.

01:56 I want to attack, and the monster_health has been reduced from the starting value of 150 to 136. Great. This is working.

02:05 We can attack the monster. Next we’re going to look at healing the player.

Become a Member to join the conversation.