我一直没有定义错误回卡,即使我已经定义了它

时间:2016-05-13 21:32:09

标签: python

在我的课堂上,我定义了backcard = self.backcard,但无论我把它放在哪里,当我运行我的程序来完成主函数时,我不断得到backcard.isClicked没有被定义的错误

class MemoryGame(Deck, PlayingCard):

    def __init__(self, fulldeck):
        self.deck = Deck()
        self.fulldeck = fulldeck
        self.deck.shuffle()

    def gameboard(self, win, xpos, ypos):
        gameboard = self.gameboard
        dealtCard = self.deck.dealCard()
        suite = dealtCard.getSuite()
        rank = dealtCard.getRank()
        self.cardlist = []

        for rowsofcards in range(4):
            backcard = Image(Point(xpos,ypos),"playingcards/b1fv.gif")
            self.backcard = backcard
            ypos += 130
            xpos = 190
            for rowsofcard in range(6):
                rowsofcards = self.deck.dealCard()
                randomsuite = rowsofcards.getSuite()
                randomrank = rowsofcards.getRank()
                cardimage = Image(Point(xpos,ypos),"playingcards/"+ (randomsuite) + str(randomrank) +".gif")
                cardimage.draw(win)
                backcard = Image(Point(xpos,ypos),"playingcards/b1fv.gif")
                backcard.draw(win)
                self.cardlist.append(backcard)
                xpos += 100
                self.backcard = backcard
        self.backcard = backcard

我将它写在3个不同的位置只是为了显示我尝试过的位置,但没有任何效果。那么如果我把那部分代码放在for循环之前,我会在赋值之前引用该backcard。这也是我的主要功能中出现错误的部分:

while not Quitbutton.isClicked(p):
        #Start Game
        if StartGameButton.isClicked(p) and player == True:

            if first == True:
                Game.gameboard(win,110,6)
                if backcard.isClicked(p):
                    backcard.undraw()
                first = False

                if StartGameButton.isClicked(p):
                    p = win.getMouse()
                    if backcard.isClicked(p):
                        backcard.undraw()
                        p = win.getMouse()

2 个答案:

答案 0 :(得分:0)

最有可能发生的事情是,在调用backcard之前调用MemoryGame的{​​{1}}属性。您可以通过在gameboard()功能中设置self.backcard来验证这一点。

确实应该将所有类变量初始化为构造函数中的某些内容。尝试将其设置为__init__中的None,您应该会看到错误更改为__init__,这可以帮助您在准备好之前找到尝试访问的内容。

答案 1 :(得分:0)

gameboard方法中,您在for循环之后调用self.backcard = backcard ,但局部变量backcard = Image(Point(xpos,ypos),"playingcards/b1fv.gif")循环中定义。在进入for循环之前尝试将其移出。