2017-10-28 102 views
-2

我正在用Python編寫一個文本冒險遊戲來讓自己開始,因爲我還沒有擅長Python。我試圖實現一組健康規則:如何創建一個始終運行但不影響代碼運行順序的while循環?

while True: 
    if health == maxHealth: 
     playerAtMaxHealth = True; 
    elif health > maxHealth: 
     health = maxHealth; 
    elif health < 0: 
     health = 0 
    elif health == 0: 
     playerDead = True; 

這是它與我的代碼的其餘部分:

def startUp(): 
    StartPRINT("""╔═══╗ 
║╔══╝ 
║╚══╦══╦══╦══╦══╦══╗ 
║╔══╣══╣╔═╣╔╗║╔╗║║═╣ 
║╚══╬══║╚═╣╔╗║╚╝║║═╣ 
╚═══╩══╩══╩╝╚╣╔═╩══╝ 
─────────────║║ 
─────────────╚╝ \n""") 
    global gold 
    global health 
    global inventory 
    global berryCollected; 
    global maxHealth; 
    global playerAtMaxHealth; 
    global playerDead 
    berryCollected = False; 
    playerAtMaxHealth = True; 
    playerDead = False 
    gold = 0 
    health = 30 
    maxHealth = 30 
    inventory = [] 
    time.sleep(2) 
    while True: 
    if health == maxHealth: 
     playerAtMaxHealth = True; 
    elif health > maxHealth: 
     health = maxHealth; 
    elif health < 0: 
     health = 0 
    elif health == 0: 
     playerDead = True; 
    forest() 

基本上什麼情況是,它打印出開始的文字,但隨後只是卡在while循環中,並沒有移動到forest()上。

+2

你以爲什麼會*結束循環? – jonrsharpe

+0

爲什麼你有一個循環?您是否正在尋找多線程? https://www.tutorialspoint.com/python/python_multithreading.htm – cdarke

回答

0

我不知道你是否已經考慮過這個問題,但它可能會更好地以面向對象的方式進行。所以,把英雄或玩家當作班級,他的屬性就是金子,健康等等。然後,你可以很容易地擁有一個功能,你可以在戰鬥或交互後運行,檢查是否玩家是否存活

0

不要使用true,以覆蓋健康規則。 所以如果你想檢查玩家的生命是這樣的.. 雖然如此 森林()//也許有的一拼與怪物 //健康檢查到這裏......

它會做健康檢查後,在森林裏的一些行動.... 如果你寫的像你的它只會卡在健康檢查