我正在用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()上。
你以爲什麼會*結束循環? – jonrsharpe
爲什麼你有一個循環?您是否正在尋找多線程? https://www.tutorialspoint.com/python/python_multithreading.htm – cdarke