我試圖在Python中製作基於文本的遊戲,但是,如果我不能在一行上做一件事,代碼可能會很快失去控制。如何有一個可以在Python中調用不同函數的多功能函數調用?
首先,源代碼:
from sys import exit
prompt = "> "
inventory = []
def menu():
while True:
print "Enter \"start game\" to start playing."
print "Enter \"password\" to skip to the level you want."
print "Enter \"exit\" to exit the game."
choice = raw_input(prompt)
if choice == "start game":
shell()
elif choice == "password":
password()
elif choice == "exit":
exit(0)
else:
print "Input invalid. Try again."
def password():
print "Enter a password."
password = raw_input(prompt)
if password == "go back":
print "Going to menu..."
else:
print "Wrong password. You are trying to cheat by (pointlessly) guess passwords."
dead("cheating")
def shell(location="default", item ="nothing"):
if location == "default" and item == "nothing":
print "Starting game..."
# starter_room (disabled until room is actually made)
elif location != "default" and item != "nothing":
print "You picked up %s." % item
inventory.append(item)
location()
elif location != "default" and item == "nothing":
print "You enter the room."
location()
else:
print "Error: Closing game."
def location():
print "Nothing to see here."
# Placeholder location so the script won't spout errors.
def dead(reason):
print "You died of %s." % reason
exit(0)
print "Welcome."
menu()
首先,如何我的遊戲基本工作原理的解釋。 遊戲有一個'外殼'(輸入完成),它從遊戲中的不同「房間」接收信息並向其發送信息,並存儲庫存。它可以接收兩個參數,即位置和最終要添加到清單的項目。然而,第40-42行('shell'中的第一個elif塊)和第43-45行('shell'中的最後一個elif塊)應該返回到位置所在的位置(第42行和第45行)精確)。我試過「%s()%位置」,但這不起作用,它似乎只在打印東西或其他東西時才起作用。
有沒有辦法做到這一點?否則,即使爲這款遊戲寫引擎也是一場噩夢。或者我不得不製造一個完全不同的引擎,在這種情況下,我認爲這將是一種更好的方法。
對不起,如果我犯了任何錯誤,第一個問題/有史以來。
我不明白你在做什麼。你能澄清嗎? – Robert