繁体   English   中英

我正在尝试编写代码,以便当玩家攻击敌人时,敌人的健康状况下降并且不会重置

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

我是编码的新手,并且正在努力做到这一点,以便当玩家攻击敌人时,它会更新敌人的健康状况并且玩家能够再次攻击直到敌人的健康状况为 0。

我想出了这个,但每次我进行第二次攻击时,地精的生命值都会回到 100,然后从那里开始。

例如,如果我进行“基本攻击”,它会显示100-20 = 80 ,但是当我再次攻击时,让我们说另一个“基本攻击”,它会再次显示 80 而不是 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()

你在代码中的问题是你只是从地精健康中减去价值而不是改变它。

我在更改代码的地方写了一条评论。

-=在代码中表示您要从变量中删除值并将其分配给结果。

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

您的代码中的另一项更改是即使地精健康状况为 0 或更少,您也不会退出程序,我将其添加到下面的代码中。

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("")

希望这有帮助

if users_attack == 1:
    small_goblin_health -= 20

elif users_attack == 2:
    small_goblin_health-=50

elif users_attack == 3:
    small_goblin_health-=100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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