Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i take health off a variable in my game

so i’m wanting to be able to display the user’s health after they’ve been kicked, punched or slapped, but it doesn’t keep it for some reason. I go to display it on a separate line, but it just says "100". Here’s my code:

#random attack damage
player1Health = 100
player2Health = 100
punch = random.randint(5, 20)
kick = random.randint(5, 20)
slap = random.randint(5, 20)

#Checking if player chooses any of the attackTypes
player2HealthSlap = player2Health - slap
player2HealthPunch = player2Health - punch
player2HealthKick = player2Health - kick
if punch:
    puchAttack = print(
        f"Ouchh! {player1} just punched {player2}, and reduced their health amount to {player2HealthPunch}"
    )

elif kick:
    kickAttack = print(
        f"Wow! {player1} just kicked {player2}, reducing their health amount to {player2HealthKick}"
    )

elif slap:
    slapAttack = print(
        f"Oh no, that seemed to harm {player2} badly. Their health has reduced to       {player2HealthSlap}"
    )

I want to be able to call that variable, and have the amount of health player 1 did to player 2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You never change player2Health. Instead of storing player2HealthSlap etc. in separate variables, you could

if punch:
    player2Health -= random.randint(5, 20) # subtract some HP from total
    print(f"[...] and reduced their health amount to {player2Health}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading