How do I let enemies damage the player? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I let enemies damage the player?

The Python course here has a "simple game" project at the end of object oriented programming. I decided to iterate on it further by adding more enemies as well as data for player character in a class called Player(GameObject). I figured out how to heal the player and allow us to hit ourselves. I need help writing code so that when "Hit" is used to attack the enemy, player also takes some damage. Player's health is stored self.health in Player(GameObject). How do I go about doing this? Also, I want to make it so that the program only runs while player's health is above zero. Thanks in advance!

17th Apr 2020, 11:44 AM
Kausar De
Kausar De - avatar
1 Answer
+ 1
if player_hit: eneme_health -= 10 player_health -= 5 - You can decide how much health each will lose add inside the while loop the following: if player_health <= 0: break - This should break out of the loop when the player got down to 0 and end the game - Change the variables to match your own. Hope this helps!
17th Apr 2020, 3:42 PM
Matias Dure
Matias Dure - avatar