将四个游戏错误与 self(属性错误:类型对象'GUI'没有属性"画布")



我正在制作一款《Connect Four》游戏,我希望当你点击一个按钮时,它能够创造出一个红色/黑色圆圈的图像,这取决于你的回合,使用tkinter画布的特定坐标。我创建了一个逻辑和GUI类,但我不能让逻辑类看到画布,因为出于某种原因,它说画布不在GUI中。

AttributeError: type object 'GUI'没有属性'canvas'

逻辑类:

class ConnectFour:
    def __init__(self):
        self.__grid = []
        self.__value = ""
        self.__turn = ""
        #self.canvas = GUI.canvas
    def buttonOneCallback(self):
        self.__value = 0
        self.getColumnNumber()
        self.runGame()        
    def findLowest(self, grid, columnNum):
        board = grid
        for y in range(BOARD_Y-1, -1, -1):
            if board[y][columnNum] == 0:
                return y
    def changeValue(self, grid, row, turn, columnNum):
        grid[row][columnNum] = turn 
        return turn
    def getPieceColor(self):
        color = ""
        if self.__turn == PLAYER1:
            color = "self.__redImage"
        if self.__turn == PLAYER2:
            color = "self.__blackImage"
        return color

    def runGame(self):
        #from GUI import GUI
        #gui = GUI() 
        turn = self.setPlayerOrder()
        grid = self.makeGrid()
        gameInPlay = True
        if gameInPlay:
            turn = self.getTurnNumber()
            if turn == PLAYER1:
                #columnNum = int(input("Pick a column: "))
                columnNum = self.getColumnNumber()
                #print(grid)
                #print(columnNum)
                row = self.getSpace(grid, columnNum)
                self.changeValue(grid, row, PLAYER1, columnNum)
                #print(grid)
                GUI.placePiece(GUI.canvas,columnNum, row)
                if self.isWinner(grid, PLAYER1):
                    gameInPlay = False
                    result = messagebox.askyesno("YOU WON! Play again?")
                    if result == True:
                        gameInPlay = True
                        grid = self.makeGrid()
                turn = PLAYER2
                #print(grid)
            elif turn == PLAYER2:
                #columnNum = int(input("Pick a column: "))
                columnNum = self.getColumnNumber()
                #print(columnNum)
                row = self.getSpace(grid, columnNum)
                self.changeValue(grid, row, PLAYER2, columnNum)
                GUI.placePiece(GUI.canvas,columnNum, row)
                if self.isWinner(grid, PLAYER2):
                    gameInPlay = False
                    result = messagebox.askyesno("YOU WON! Play again?")
                    if result == True:
                        gameInPlay = True
                        grid = self.makeGrid()
                turn = PLAYER1
                #print(grid)
            if self.isBoardFull(grid):
                gameInPlay = False
                result = messagebox.askyesno("It's a tie! Play again?")
                if result == True:
                    gameInPlay = True
                    board = self.makeGrid()
GUI类:

class GUI:
    def __init__(self):
        #Created the main window and sets the window to a certain length/height
        self.__win = Tk()
        self.__win.title("Connect Four")
        #self.__win.geometry("625x600+30+30")
        #self.__win.configure(background='white')
        #self.__win.grid(rowspan=7)
        #Created an instance of the ConnectFour class 
        game = ConnectFour()

        #Buttons to choose where to drop the chip
        self.redImage = PhotoImage(file = "red.gif")
        self.blackImage = PhotoImage(file = "black.gif")

        self.__buttonOne = Button(self.__topFrame, width = 10, command = game.buttonOneCallback)
        #self.__redButton.grid(row=0, column=0)
        #self.__blackButton.grid(row=1, column=0)
        self.canvas = Canvas(self.__win, height = 467, width = 725)
        self.canvas.pack()
        #Pieces on board
        #self.__canvas.create_image(116, 430, image = self.__redImage)
        #self.__canvas.create_image(194, 352, image = self.__blackImage)
        self.__win.mainloop()
    def placePiece(canvas,columnNum, row):
        game = ConnectFour()
        placementY = (468/6) * (columnNum+1)
        placementX = (546/7) * (row+1)
        color = game.getPieceColor()
        #print(color)
        #print(self)
        canvas.create_image(placementX, placementY, image = self.redImage)
GUI()

谢谢!

在设置GUI类中的canvas属性之前,您正在创建ConnectFour对象。在创建ConnectFour实例之前,您需要确保已经创建了所有依赖项。

相关内容

  • 没有找到相关文章

最新更新