Python - Sikuli & Hangman 游戏

Python - Sikuli & Hangman Game

我一直在尝试制作一个刽子手游戏,如果启动命令“!hangman”,将执行以下代码:

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."

您的 class 似乎有两处问题导致它悄无声息地死去。当您调用 Playing 函数时,您是在尝试直接调用它,而不是从 class.

的实例调用

另外(由于我自己还在学习 python,我可能不太了解这个)class 中的函数应该至少传递 1 个参数,通常是 self.

如果您查看下面的示例,您会发现如果我尝试直接调用 Hangman.Playing(),我会收到有关它不是实例的错误消息。

如果我创建一个实例 x 并从中调用它,则打印语句会成功执行。

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
>>>