2015-08-24 142 views
0

我一直試圖做一個劊子手遊戲,如果命令!「劊子手」是iniated,這個代碼將執行:的Python - Sikuli與劊子手遊戲

def cmdHangman(event): 
    print("!hangman") #this print works 
    type("Game: Hangman") 
    type(Key.ENTER, Key.SHIFT) 
    Hangman.Playing() 

Hangman.Playing()導致:

listOfWords = ["example", "says", "python", "rocks"] 
guessWord = random.choice(listOfWords) 
board = [" * " for char in guessWord] 
alreadySaid = "" 

class Hangman(): 

    def Playing(): 
     print("Playing") #print not showing 
     global guessWord, board, alreadySaid  

     whatplayersaid = hangmanRegion.text() 
     hangmanRegion.stopObserver() 

     if whatplayersaid in guessWord: 
      board = [char if char == whatplayersaid or char in alreadySaid else " * " for char in guessWord] 
      board = "".join(board) 
      type(board) 
      type(Key.ENTER) 
     else: 
     type("Nope") 
     type(Key.ENTER) 
     alreadySaid = alreadySaid + whatplayersaid 
     Hangman.PlayBuffer() 

    def gotoPlaying(event): 
     print("gotoPlaying") #print not showing 
     Hangman.Playing() 

    def PlayBuffer(): 
     print("PlayBuffer") #print not showing 
     wait(1) 
     hangmanRegion.onChange(Hangman.gotoPlaying) 
     hangmanRegion.observe(FOREVER) 

應該掃描下聊天輸入使用hangmanRegion.text()字母,所以如果有人在羣聊類型:「A」,whatplayersaid應該等於「A」

怪異的事情,它甚至沒有工作。完全一樣。 沒有錯誤日誌或任何東西。我試圖在class Hangman()的任何地方放置打印件,並且在日誌中沒有看到任何打印件。

有人能找到我的代碼中的缺陷嗎?它沒有被執行或者Sikuli IDE出現問題嗎?還是我真的很累?鍵入命令後

登錄:

!hangman 
[log] TYPE "Game: Hangman" 

[log] (Shift) TYPE "#ENTER." 

回答

2

它看起來像有兩件事情錯了你的類,它是導致它無聲無息地死去。當你調用播放函數時,你試圖直接調用它,而不是從類的一個實例中調用它。

此外(我可能會脫離基礎,因爲我仍然在學習Python本身)類中的函數應該至少傳遞1個參數,通常是自我。

如果你看看下面的例子,你可以看看我是否嘗試直接調用Hangman.Playing(),我得到一個關於它不是實例的錯誤。

如果我創建一個實例x並從中調用它,那麼print語句會成功執行。

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> class Hangman: 
...  def Playing(self): 
...    print "Playing" 
... 
>>> Hangman.Playing() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unbound method Playing() must be called with Hangman instance as  first argument (got nothing instead) 
>>> x = Hangman() 
>>> x.Playing() 
Playing 
>>> 
+0

神聖的廢話,非常感謝!我每天都在不斷學習! :)它終於有效!太糟糕了,我還不能投票給你。 – SikuliXUser