[英]English command [text RPG] - python
我正在創建一個文本RPG,它從較早的文本冒險中汲取了靈感,在該文本冒險中,玩家輸入了英語命令。 例如“撿劍”之類的東西。 我已經建立了一個簡單的方法。 輸入“ A”來執行此操作,然后輸入“ B”來執行此操作,但是我想擴展我的系統以獲得更多的自由。 我需要創建一個系統; 當玩家鍵入命令時,程序會挑選出關鍵詞。 我認為這可以通過“輸入”命令來實現。 這是我的代碼:
print "What would you like to do??"
input_loop_sleep = str('')
choice_sleep = raw_input(str('>>>'))
loop_sleep = False
table_time = False
bed_time = False
error_time = False
while loop_sleep == False:
if str('sleep') in choice_sleep or str('bed') in choice_sleep or str('goodnight') in choice_sleep or str('Sleep') in choice_sleep or str('tired') in choice_sleep:
while bed_time == False:
print "you decide to go back to sleep"
time.sleep(1)
print "..."
time.sleep(1)
print ""
time.sleep(1)
print "darkness"
time.sleep(1)
print ""
print "you wake up..."
time.sleep(1)
print "it is now 9:15am"
time == int(9.15)
time.sleep(1)
print "You are standing in your room, slightly more refreshed."
time.sleep(1)
print "there is a table with some things on it, stairs, and a wardrobe... with the doors wide open..."
time.sleep(1)
print "that's strange... you swear that they were shut when you went to sleep..."
break
else:
bed_time == True
break
bed_loop_choice = raw_input('>>>')
elif str('bedside') in choice_sleep or str('table') in str(choice_sleep):
while table_time == False:
print "You rub your eyes and pick up some belongings from a"
print "bedside table."
time.sleep(1)
print "Map added!"
time.sleep(1)
print "100 gold added!"
time.sleep(1)
print "Leather Bag added!"
cash == int(100)
time.sleep(1)
Map == str('map of', str(province))
Inventory == [str(Map)]
container == str('leather bag')
print "your", str(container), str("contains a"), str(Map), str('and'), str(cash)
break
else:
table_time == True
break
else:
print "invalid command!"
當我運行代碼時,無論我鍵入什么代碼,總是帶有'sleep'選項。 我可能只是犯了一個簡單的錯誤! 您能幫我解決我做錯了什么以及如何解決。
要回答有關為什么一直重復sleep
循環的問題:
您正在通過控制循環
while bed_time == False:
但您絕不要在循環中將bed_time
設置為True
(僅在else
子句中,但該子句僅在循環正常退出時才執行, 而不是在像現在這樣通過break
退出時才執行-因此bed_time
永遠不會改變)。
此外,通常不贊成直接與布爾值進行比較。 慣用的方式(在大多數語言中,不僅是Python) while not bedtime:
會出現while not bedtime:
。
在着手進行如此大的項目之前,您可能應該閱讀一些初學者的編程書籍和/或Python教程 。 您的代碼中有幾個問題傳達了這樣的印象:您確實需要掌握一些基本的編程原理和Python習慣用法。
例如,
int(9.15)
這不是存儲時間的好方法-結果將是9
。
然后,您將使用time == int(9.15)
,這意味着“將模塊time
與整數9
進行比較”。 我猜你的意思是time = int(9.15)
,由於上述原因,它已經很糟糕了,但是還會出現另一個問題:您將覆蓋模塊名稱time
,這將導致隨后的time.sleep(1)
命令執行失敗並出現AttributeError
。
您的代碼中不需要大多數str()
調用,因為您已在已經是字符串的對象上使用了它。 在您不在的地方,這是不正確的: str('map of', str(province))
將引發TypeError
( str
僅接受一個參數)。
您正在為不是類實例的對象使用大寫的變量名。
等等...
我認為這足以解決問題
In [1]: str('bed') in "bedside"
Out[1]: True
因此,當您在bedside
書寫時,如果情況允許,它將進入sleep
選項,因此您得到了錯誤的答案。
您應該寫:
if str('bed') == choice_sleep or *other conditions* :
then got inside the sleep option
PS:我假設您已經導入了time
模塊。 PPS:我檢查了輸入table
的代碼,它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.