2016-02-21 22 views
-1

我在網站上發現了這個問題的幾個版本,但沒有一個答案給我一個答案,我明白(this question是最接近的,但'已經回答'答案似乎以不同的方向發展)。轉換到類後未定義python全局名稱

我正在努力通過學習python這本艱難的方法書,並且已經到了我想要爲遊戲構建一個簡單的戰鬥系統的地步。好消息是,當我把它作爲一個獨立的程序時,它似乎起作用。壞消息是,只要我嘗試添加它作爲課程,它就會中斷。我可以添加完整的代碼,如果它是有用的,但我認爲這個問題本質上是與代碼看起來像這樣:

class Room1(Scene): 

    def kick(): 
     #things happen here 

    def start(): 
     move = raw_input("> ") 
     if move == "kick": 
      kick() 
    start() 

這工作得很好時,這只是一個獨立的一套DEFS的,但現在,我添加了類,當移動==踢時拋出全局名稱錯誤。我錯過了什麼?

在此先感謝,對不起,如果有明顯的答案,我錯過了。

感謝大家的快速回復!看起來像添加整個代碼可能會有幫助。只是要清楚,這是模擬example 43 from Learn Python the Hard Way的大型遊戲的一部分。我非常感謝關於改善結構的建議,但我現在的感覺是,我想知道爲什麼這不適用於我幾乎理解的結構,然後繼續改變更多的東西。當然,我更願意接受「你試圖做的事不符合你嘗試使用的結構」的答案。當我將下面的代碼作爲更大結構的一部分運行時(爲了空間的利益,我不會粘貼整個東西,但遊戲引擎結構與上面相關),我得到了我所描述的錯誤。我試着添加'self.start()'或'Room1.start()'我得到錯誤,名稱'self'或名稱'Room1'未定義。

class Room1(Scene): 

gothon_power = 500 
move_points = 10 
damage = 0 


def kick(self): 
    global gothon_power 
    global move_points 
    global damage 
    damage = randint(10,201) 
    gothon_power = gothon_power - damage 
    move_points = move_points - 2 
    result() 

def punch(self): 
    global gothon_power 
    global move_points 
    global damage 
    damage = randint(1, 101) 
    gothon_power = gothon_power - damage 
    move_points = move_points -1 
    result() 

def result(self): 
    if gothon_power > 0 and move_points > 1: 
     print "You did %s damage." % damage 
     print "The Gothon is down to %s health points." %     gothon_power 
     print "You are down to %s move points." % move_points 
     print "\n" 
     print "What's your next move?" 
     move = raw_input("> ") 
     if move == "kick": 
      kick() 
     elif move == "punch": 
      punch() 
     else: 
      print "This isn't going to go anywhere unless      you type 'kick' or 'punch'" 
      print "\n" 
      result() 

    elif gothon_power > 0 and move_points == 1: 
     print "You did %s damage." % damage 
     print "The Gothon is down to %s health points." %     gothon_power 
     print "You are down to %s move points." % move_points 
     print "\n" 
     print "What's your next move? Remember, you only have     1 move point." 
     move = raw_input("> ") 
     if move == "kick": 
      print "You don't have enough move points for a      kick." 
      print "\n" 
      result() 
     elif move == "punch": 
      punch() 
     else: 
      print "This isn't going to go anywhere unless      you type 'kick' or 'punch'" 
      print "\n" 
      result() 

    elif gothon_power < 1 and move_points > 0: 
     print "Congratuations, you killed the Gothon!" 
     return 'room2' 

    else: 
     print "The Gothon still has health but you don't have     moves." 
     print "You know what that means." 
     print "\n" 
     print "The Gothon killed you." 
     return 'death' 



def start(self): 
    print "It is time to fight the Gothon" 
    print "First, let's pause to explain the fighting rules." 
    print "\n" 
    print "The Gothon has 500 health points." 
    print "You have 10 move points." 
    print "\n" 
    print "Kicks cost 2 move points and do between 10 and 200    points of damage." 
    print "Punches cost 1 move opint and do between 1 and 100    points of damage." 
    print "\n" 
    print "If you get rid of all 500 Gothon health points before    you run out of" 
    print "move points you win. If you run out of move points    before the Gothon" 
    print "moves out of health points you lose." 
    print "\n" 
    print "What's your first move?" 

    move = raw_input("> ") 

    if move == "kick": 
     kick() 

    elif move == "punch": 
     punch() 
    else: 
     print "This isn't going to go anywhere unless you type     'kick' or 'punch'" 
     start() 
start() 
+1

請提供整個回溯。 – zondo

+0

一旦你的函數進入一個類,他們被稱爲方法。因此他們接受一個通常稱爲自我的強制性第一個論證。 – danidee

回答

0

要調用kick(),因爲你需要使用self.<method Name>語法,並添加self作爲第一個參數,以它的方法:

def kick(self): 
    #things happen here 
    print('kicking') 

#call method 
self.kick() 

或附加做出kick()靜態方法通過調用簡單

A.kick() 
0

如果不創建實例,則無法執行類的實例方法。例如,你可以寫:

class Room1(Scene): 

    def kick(self): 
     #things happen here 

    def start(self): 
     move = raw_input("> ") 
     if move == "kick": 
      self.kick() 

room = Room1() 
room.start() 

但是,我不建議在這種情況下使用類。類旨在提供一種方式來表示您自己的具有狀態的自定義對象。例如,您可能有一個Monster類,其屬性爲damage,name等。

對於此示例,您可能最好只使用Room1的函數。

1

一組適當的一類方法將看起來像:

class Room1(Scene): 

    def kick(self): 
     #things happen here 

    def start(self): 
     move = raw_input("> ") 
     if move == "kick": 
      self.kick() 


Room1().start() 

但你可能要重新考慮你的設計一點點。它真的沒有意義的場景來查詢輸入。您必須在遊戲中的每個房間中複製該代碼。

想想這樣的頂級駕駛的一分鐘:

game = Game() 
starting_room = FrontPorch() 
game.move_to(starting_room) 

while not game.is_over(): 
    move = raw_input("> ") 
    cmd, args = move.split(None, 1) 
    game.current_room.do_command(cmd, args) 

然後讓每個房間處理這些命令,它確實特別。在基本級別的房間類中,您可以執行大多數房間常見的命令,例如「GOTO」,「LOOK」,「WHERE」等。然後允許踢的房間將覆蓋do_command幷包含您現在的邏輯。

Here is a presentation I gave at PyCon '06上寫文字冒險。跳過解析的東西,然後轉到描述遊戲/房間/項目設計的部分。你不必逐字按照這個設計,但是你可能會得到一些關於你的對象和類如何相互作用的想法。在真正潛入書寫大量代碼之前,請考慮一下。