Add Win and Lose Conditions
One is that the health of the player reaches
0, or the health of the monster reaches
0. So, where would we introduce this? I would say where we’re attacking, right? So after an attack of the player, we can go and check.
if monster_health <= 0: the game ends.
So we’re going to break out of the loop and probably also keep being a bit descriptive.
"You defeated the monster!" Yay, congratulations! That means the person won the game. The player won the game.
And then the second condition will be if, after the attack of the monster, the player’s health is going to be below or at
0. So, we’ll have to add the same condition again. I’m going to say
if player_health <= 0: then I’m going to print another message that reads something like
"You were defeated by the monster!"
Exclamation mark because it’s a tragic event, of course. Oh yeah, and we’ve got to break. So, now we have three ways to end this loop. It’s an endless loop, but then we have three
break statements in here.
One is if the
monster_health goes below
0, one is if you run away, and one is if your health goes below
0. And each of those checks happen after the respective attacks of yourself and the monster. Looking good. Let’s try it out.
02:00 Can I defeat this monster just with attacking? Oh, it’s getting close. Got to heal a bit. It’s a tough, it’s a tough monster, but I’m not going to run. I’m going to stick it out. Oh, health six. Whew.
02:28 So, this essentially was the first real play-through of this little role-playing game. And you see that all the functionality is there. You actually did it and completed the challenge well, but there’s more, right?
Become a Member to join the conversation.