2011-11-12 46 views
2

我試圖在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()%位置」,但這不起作用,它似乎只在打印東西或其他東西時才起作用。

有沒有辦法做到這一點?否則,即使爲這款遊戲寫引擎也是一場噩夢。或者我不得不製造一個完全不同的引擎,在這種情況下,我認爲這將是一種更好的方法。

對不起,如果我犯了任何錯誤,第一個問題/有史以來。

+2

我不明白你在做什麼。你能澄清嗎? – Robert

回答

0

如果我正確理解你想要的是這樣的:玩家將輸入一個位置名稱,你想調用相關的方法。 "%s"()%location將不起作用,一個字符串(這是什麼是「%s」是不可調用的)。

讓我們嘗試一個面向對象的方式:

class Maze: 
    def __init__(self): 
     # do what you need to initialize your maze 

    def bathroom(self): 
     #go to the bathroom 

    def kitchen(self): 
     # go to the kitchen 

    def shell(self, location="", item=""): 
     if location == "" and item == "": 
     print "Starting game..." 
     # starter_room (disabled until room is actually made) 
     elif location and item: 
     print "You picked up %s." % item 
     inventory.append(item) 
     getattr(self, location)() 
     elif location and item == "": 
     print "You enter the room." 
     getattr(self, location)() 
     else: 
     print "Error: Closing game." 

maze = Maze() 
while True: # or whatever you want as stop condition 
    location = raw_input("enter your location :") 
    item = raw_input("enter your location :") 
    maze.shell(location=location, item=item) 
+0

我最近在課程中沒有教過任何面向對象的方法,但是感謝你提供的信息。 – Ratio

1
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() 

我猜你想打電話給有其名字的功能。對於您需要的模塊或類的引用中,它被定義爲:

module = some_module # where the function is defined 
function = getattr(module, location) # get the reference to the function 
function() # call the function 

如果該功能在當前模塊中定義:

function = globals()[location] 
function() # call the function 
0

我想你可以使用GETATTR()方法。

例子:你想從模塊「測試」調用方法「HELLOWORD()」,你會然後做:

methodYouWantToCall = getattr(test, "helloworld") 
caller = methodYouWantToCall() 

希望它給你一個線索。

+0

另外,如果我沒記錯,那些行可以壓縮在:caller = getattr(test,「helloworld」) – Oscuro1987

+0

Python函數和方法名應該是小寫:'methodYouWantToCall' - 首選 - >'method_to_call'您應該檢查PEP -8 – joaquin

+0

這是一個例子...... :) – Oscuro1987

相關問題