我想從LPTHW的練習36中得到一個遊戲來工作。爲什麼這個程序重複自己?
我可以讓遊戲的整個部分工作,除非它進入戰鬥狀態。這是我認爲問題所在的地方。
def combat_engine():
global wins, hit_points, current_hp
if monster_current_hp or current_hp > 0:
print "You can type 'a' for attack, or 'q' for quit, choose one."
answer = raw_input(":> ")
if 'a' in answer:
attack(monster)
attack('player')
combat_engine()
elif 'q' in answer:
print t.white_on_red("You give up and are devoured by the %s.") % monster
exit(0)
else:
print "I dont understand %s." % answer
next()
combat_engine()
elif monster_hp == 0:
print "You defeated the %s, congradulations %s!" % monster, name
wins = wins + 1
if wins == 5:
you_win()
elif victim == 'player':
hit_points = hit_points + d6()
current_hp = hit_points
print "You feel hardier."
next()
barracks()
elif current_hp == 0:
print "You have been deafeted by the %s, better luck next time." % monster
next()
exit(0)
else:
print "Bug Somewhere"
我相信這個bug是在這個函數的某個地方。當我打印出每個角色的HP值時,在怪物降低到-2馬力後,戰鬥仍在繼續。也許這是布爾人的問題?
我想要做的是要麼做所有統計調整的勝利和退出遊戲,如果你輸了。我只是想要通過這個,所以我可以開始學習班級和口號,這應該讓我的生活更輕鬆。請讓我知道,如果我應該發佈更多信息,我是新來的,而不是在這裏張貼問題的最佳選擇。
在此先感謝!
嘗試減少代碼示例 - 刪除無關緊要的代碼行,直到只有觸發錯誤的代碼行。這涉及擺脫實際的戰鬥 - 只要看看你是否可以硬編碼導致意外行爲的一系列事件。 – millimoose
原因是您的代碼示例很難通過閱讀進行檢查,並且該過程可能會幫助您找到錯誤。 – millimoose
雖然我認爲問題出現在第一個關於combat_engine功能的'if'聲明上,但我不太確定哪條線是誠實的。也許我誤解你了?吮吸是一個新生事物。 – lerugray