簡體   English   中英

Python 運行我的游戲時 True 循環未迭代

[英]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!')

您的主要問題是您在print語句之前放置了break語句,這意味着while循環在到達 print 語句之前停止。

例如:

    # 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

我希望這是一個令人滿意的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM