python - 更新整数的值(我认为)

标签 python

我是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/

相关文章:

python - 删除级联的 Django 在 PostgreSQL 中没有产生正确的约束

python - 如何使用电视马拉松获取特定私有(private) channel 的更新?

python - 按值查找嵌套字典路径

Python map() 函数遍历 'None' 元素

python - 如何在蓝图中获取应用程序上下文,而不是在请求中?

python - 在 SocketServer 请求之间重用变量

python - Python 中检查对象属性是否分配了 DataFrame 的最有效方法?

c++ - 在 Visual Studio 2005 中以 Python 获取 C++ 扩展的无痛方式

python 3 typing varadic "apply"样式定义

python - 将两本词典合二为一