【问题标题】:Python while True loop not iterating over when running my gamePython while True 循环在运行我的游戏时没有迭代
【发布时间】:2022-01-09 05:09:40
【问题描述】:

我一直在开发这款奇幻战斗游戏,尽管在游戏最后开始的 while True 循环没有迭代,但它按预期工作。我尝试使用 continue 没有成功。我已经包含了一个流程图来直观地展示游戏的逻辑。我没有收到任何错误,应该是无限的循环只是在最后停止而没有重新开始。我可以让它迭代直到它到达break 之一吗?

"""
Fantasy Battle Game
Player against Dragon
"""

# Player
wizard = "Wizard"
elf = "Elf"
human = "Human"

# Player health
wizard_hp = 70
elf_hp = 100
human_hp = 150

# Player damage force
wizard_damage = 150
elf_damage = 100
human_damage = 20

# Dragon health and damage force
dragon_hp = 300
dragon_damage = 50

# Print the list of characters
print(wizard)
print(elf)
print(human)

# Input function to choose player
character = input("Choose your character: ")

# While loop to represent player profile
while True:
    if character == "Wizard":
        my_hp = wizard_hp
        my_damage = wizard_damage
        break
    elif character == "Elf":
        my_hp = elf_hp
        my_damage = elf_damage
        break
    elif character == "Human":
        my_hp = human_hp
        my_dammage = human_damage
        break
    else:
        print("Unknown Character")
        break

# Print player selection
print(character)

# print player health:
print(my_hp)

# print player damage force:
print(my_damage)

# Start game
while True:
    # Player start first battle against Dragon
    dragon_hp = dragon_hp - my_damage
    
    # If dragon health is positive show remaining health
    if dragon_hp > 0:
        print(f'{character} damaged the dragon!')
        print(f'The Dragon hitpoints are now {dragon_hp}')
    
    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        break
        print(f'The Dragon lost the battle!')
        
    # Dragon start second battle against player
    my_hp = my_hp - dragon_damage
    
    # If player health is positive show remaining health
    if my_hp > 0:
        print(f'The Dragon strikes back at {character}')
        print(f'The {character} hitpoints are now {my_hp}')
        
    # If player health is negative - game over
    elif my_hp <= 0:
        break
        print(f'The {character} lost the battle!')

【问题讨论】:

  • print 语句放在break 之前,这样您就可以看到它何时爆发。
  • 第一个while 循环完全没用——它在每个分支之后都会中断,因此永远不会真正循环。你也可以删除它。在第二个循环中,如果您希望它基于某些条件运行,您可以将其作为 while 条件而不是 Truewhile dragon_hp &gt; 0 and my_hp &gt;0

标签: python python-3.x loops while-loop nested-loops


【解决方案1】:

您的主要问题是您将break 语句放在print 语句之前,这意味着while 循环在到达打印语句之前停止。

例如:

    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        break #Break should not be here
        print(f'The Dragon lost the battle!')
    # If player health is negative - game over
    elif my_hp <= 0:
        break #Break should not be here
        print(f'The {character} lost the battle!')

请尝试:

    # If dragon health is negative or null - game over
    elif dragon_hp <= 0:
        print(f'The Dragon lost the battle!')
        break
    # If player health is negative - game over
    elif my_hp <= 0:
        print(f'The {character} lost the battle!')
        break

另外,一个提示:你应该为你的角色使用类,因为它更容易管理类。它将角色及其变量保持在一起,因此您在创建新角色时不必不断地制作超过 1 个变量。

你的角色的一个例子:

class character:
    def __init__(self,name,damg=0,hp=0):
        self.damage=damg
        self.hp=hp
        self.name=name

然后您将创建一个新角色,例如:

wizard=character("Wizard",150,70)

调用每个属性,如:

wizard.hp
# returns 70
wizard.name
# returns "Wizard"
wizard.damage
# returns 150

如果你保持这样的字符,这意味着你可以运行更有效的代码,例如我会在一个列表中取出新的character对象,然后放入一个攻击for循环遍历角色列表并打印出他们造成的伤害、他们的健康和他们的名字。例如

character_list=[wizard,elf,human] #some imaginary list
for x in character_list:
    print(f"{x.name} attacked the dragon!")
    print(f"{x.damage} was dealt.")
    print(f"{x.name} has {x.hp} left")

此外,您可以照常编辑这些内容。 例如,从上方获取我们的向导:

# oh no! some event caused your wizard to lose health!
wizard.hp-=10
# returns 60 when called again

我希望这是一个令人满意的解释。

【讨论】:

  • 这样一个徒劳的错误!谢谢!
  • @elideli 你应该再读一遍我的帖子...更多关于如何改进你的代码的信息!
  • 这是一个非常好的答案!感谢您的宝贵时间!
  • @elideli 没问题,很高兴能对你有所帮助!
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多