Python while True loop not iterate over when 运行 我的游戏
Python while True loop not iterating over when running my game
我一直在研究这款奇幻战斗游戏,它按预期工作,尽管在最后开始游戏的 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!')
您的主要问题是您将 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
我希望这是一个令人满意的解释。
我一直在研究这款奇幻战斗游戏,它按预期工作,尽管在最后开始游戏的 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!')
您的主要问题是您将 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
我希望这是一个令人满意的解释。