简体   繁体   中英

I'm trying to make a code so when the player attacks the enemy, the enemy's health goes down and doesn't reset

I'm new to coding and was struggling to make it so that when the player attacks the enemy, it updates the enemies health and the player is able to attack again until the health of the enemy is 0.

Ive come up with this but every time I make a second attack the goblins health goes back to 100 and goes from there.

For example, if I do a "basic attack" it does 100-20 = 80 , but when I attack again, lets say another "basic attack" it displays 80 again instead of 60.

def combat_enemy_goblin():
    
    small_goblin_health = 100
    attack_basic = 20
    attack_special = 50
    attack_ultimate = 100
    
    print("You are now in combat with a small goblin!")
    print("")
    print("")
    print("Small Goblin: ", small_goblin_health, "Health Points")

    while True:
        try:
            users_attack = int(input("""
            Your Moves
            ----------------------------
            1 - Basic attack [20]
            2 - Special attack [50]
            3 - Ultimate attack [100]
            4 - Run [repeating action]
            ----------------------------

            What do you choose? """))
            if  users_attack == 1:
                print("")
                print("You use your basic attack")
                print("")
                print("The goblin has taken some damage")
                print("")
                print("Small Goblin: ", small_goblin_health - 20, "Health points")
                print("")

            else:
                if users_attack == 2:
                    print("")
                    print("You use your basic attack")
                    print("")
                    print("The goblin has taken some damage")
                    print("")
                    print("Small Goblin: ", small_goblin_health - 50, "Health points")
                    print("")

                else:
                    if users_attack == 3:
                        print("")
                        print("You use your basic attack")
                        print("")
                        print("The goblin has taken some damage")
                        print("")
                        print("Small Goblin: ", small_goblin_health - 100, "Health points")
                        print("")   
                    
                    else:
                        if users_attack == 4:
                            print("")
                            print(" You tried to run away but failed!")      

        except:
            print("")
            print("You cant do that!")
            print("")

combat_enemy_goblin()

Your problem in code is you are only subtracting value from goblin health but not changing it.

I have written a comment where I change the code.

-= in code means that you are removing the value from a variable and assigning it to the result.

num = 10
print(num) # Output: 10
num-=1
print(num) # Output: 9

One more change in your code is you do not exit the program even if the goblin health is 0 or less I am adding this in the below code.

small_goblin_health = 100
attack_basic = 20
attack_special = 50
attack_ultimate = 100

print("You are now in combat with a small goblin!")
print("")
print("")
print("Small Goblin: ", small_goblin_health, "Health Points")

while True:
    if small_goblin_health<=0: # edited
        break # exit the loop if small_goblin_health is less than or equal to 0.
    try:
        users_attack = int(input("""
        Your Moves
        ----------------------------
        1 - Basic attack [20]
        2 - Special attack [50]
        3 - Ultimate attack [100]
        4 - Run [repeating action]
        ----------------------------

        What do you choose? """))
        if  users_attack == 1:
            small_goblin_health-=20 #edited
            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")

        elif users_attack == 2:
            small_goblin_health-=50 #edited

            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")

        elif users_attack == 3:
            small_goblin_health-=100 #edited
            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")   
        elif users_attack == 4:
                        print("")
                        print(" You tried to run away but failed!")      

    except ValueError: # edited
        print("")
        print("You cant do that!")
        print("")

In hope this helps

if users_attack == 1:
    small_goblin_health -= 20

elif users_attack == 2:
    small_goblin_health-=50

elif users_attack == 3:
    small_goblin_health-=100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM