更新一个整数的值(我想)

2024-05-18 23:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手。目前我正在和一个朋友编写一个游戏。我们目前正在研制一种作战系统,唯一的问题是我们不知道一旦造成伤害,如何更新敌人的健康状况。代码如下

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

Tags: ifintprinthealthjabattackwhilestr
3条回答

你需要在打印声明之外更改敌人的生命值。 做:

 if attackchoice == ("rusty knife jab"):
    enemy1_health = enemy1_health - 10.5
    print(enemy1_health)

你也可以对其他攻击做同样的处理

您还可以在while循环中定义敌人的生命值。您需要在循环之外定义它

您需要使用以下语句更改打印语句之外的敌人生命值:

enemy1_health = enemy1_health - 20

或者像这样,做同样的事情:

enemy1_health -= 20

您还可以在每次循环循环时重置enemy1_运行状况,然后将其移除

你没有定义玩家的健康,定义它

你的循环会一直持续到你死

因此,您的代码应该更像这样:

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

必须在while循环上方定义健康状况,如下所示:

broadsword_attack = 20
rusty_knife = 10.5
attacks = ["broadsword swing " +  str(broadsword_attack), "rusty knife jab " + str(rusty_knife) ]
enemy1_health = 150


while enemy1_health > 0: 
  while player_health > 0: 
    print(attacks)
    attackchoice = input("Choose an attack: ")
    if attackchoice == ("broadsword swing"):
      enemy1_health -=20
      print (int(enemy1_health))
    if attackchoice == ("rusty knife jab"):
      enemy1_health -=10.5
      print (int(enemy1_health))
  print("you died")
  quit()   
print("you cleared the level")

避免在循环的每次迭代中将运行状况重新定义为150。并将这些值分配给health,而不仅仅是打印它。这意味着无论你做什么,敌人都不会失去健康。 此外,还有另一个问题,因为你从来没有减少球员的健康,这将导致一个无限循环

相关问题 更多 >

    热门问题