我是Python新手。目前我正在和 friend 一起编写游戏。我们目前正在开发一个战斗系统,唯一的问题是我们不知道在造成伤害后如何更新敌人的生命值。代码如下。
enemy1_health = 150
broadsword_attack = 20
rusty_knife = 10.5
attacks = ["broadsword swing " + str(broadsword_attack), "rusty knife jab " + str(rusty_knife) ]
while enemy1_health > 0:
while player_health > 0:
enemy1_health = 150
print(attacks)
attackchoice = input("Choose an attack: ")
if attackchoice == ("broadsword swing"):
print (int(enemy1_health - 20))
if attackchoice == ("rusty knife jab"):
print (int(enemy1_health - 10.5))
print("you died")
quit()
print("you cleared the level")```
最佳答案
您需要使用如下语句在打印语句之外更改敌人的生命值:
enemy1_health = enemy1_health - 20
或者像这样,它做同样的事情:
enemy1_health -= 20
您还可以在每次循环时重置敌人1_健康状况,并将其删除。
你没有定义player_health,定义它。
你的循环会永远持续下去,直到你死。
所以你的代码最终应该看起来更像这样:
enemy1_health = 150
broadsword_attack = 20
rusty_knife = 10.5
player_health = 100
attacks = ["broadsword swing " + str(broadsword_attack), "rusty knife jab " + str(rusty_knife)]
while enemy1_health > 0:
print(attacks)
attackchoice = input("Choose an attack: ")
if attackchoice == ("broadsword swing"):
enemy1_health -= 20
if attackchoice == ("rusty knife jab"):
enemy1_health -= 10.5
print(enemy1_health)
if player_health <= 0:
print("you died")
quit()
print("you cleared the level")
这仍然需要相当多的调整,如果是这样的话,这将是一个完整的游戏(基本上,如果你发送垃圾大刀攻击,你会赢,因为它们会造成更大的伤害):
enemy1_health = 150
enemy1_attack = 10
player_health = 100
broadsword_attack = 20
rusty_knife = 10.5
attacks = ["broadsword swing " + str(broadsword_attack), "rusty knife jab " + str(rusty_knife)]
while enemy1_health > 0:
print(attacks)
attackchoice = input("Choose an attack: ")
if attackchoice == ("broadsword swing"):
enemy1_health -= broadsword_attack
if attackchoice == ("rusty knife jab"):
enemy1_health -= rusty_knife
print(f'A hit! The enemy has {enemy1_health} health left.')
if enemy1_health > 0:
player_health -= enemy1_attack
print(f'The enemy attacks and leaves you with {player_health} health.')
if player_health <= 0:
print("you died")
quit()
print("you cleared the level")
关于python - 更新整数的值(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66395018/