需要帮助在国际象棋内移动棋子

时间:2019-08-07 12:26:59

标签: python tkinter

总体而言,我已经创建了棋盘并将棋子放置在棋盘上,并且我一直在使用tkinter进行此操作,我的问题在于我创建了一个称为棋子的字典,并且我有一个函数可以调用鼠标的位置,但是如果空间上有任何一块,我将无法得到命令返回:


mousex = 0
mousey = 0
class GameBoard(tk.Frame):
    def __init__(self, parent, rows=8, columns=8, size=64, color1="white", color2="gray"):
        '''size is the size of a square, in pixels'''

        self.rows = rows
        self.columns = columns
        self.size = size
        self.color1 = color1
        self.color2 = color2
        self.pieces = {}
        self.imageref ={}

        canvas_width = columns * size
        canvas_height = rows * size

        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, width=canvas_width, height=canvas_height)
        self.canvas.pack(side="top", fill="both", expand=True)

        # this binding will cause a refresh if the user interactively
        # changes the window size
        self.canvas.bind("<Configure>", self.refresh)
#this is how i created the pieces in the first place
    def addpiece(self, name, image, row, column):
        '''Add a piece to the playing board'''
        self.canvas.create_image(0,0, image=image, tags=(name, "piece"), anchor="c")
        self.placepiece(name, row, column)

#this allows me to move specific pieces
    def placepiece(self, name, row, column):
        '''Place a piece at the given row/column'''
        self.pieces[name] = (row, column)
        x0 = (column * self.size) + int(self.size/2)
        y0 = (row * self.size) + int(self.size/2)
        self.canvas.coords(name, x0, y0)

#this sets up the board
    def refresh(self, event):
        '''Redraw the board, possibly in response to window being resized'''
        xsize = int((event.width-1) / self.columns)
        ysize = int((event.height-1) / self.rows)
        self.size = min(xsize, ysize)
        self.canvas.delete("square")
        color = self.color2
        for row in range(self.rows):
            color = self.color1 if color == self.color2 else self.color2
            for col in range(self.columns):
                x1 = (col * self.size)
                y1 = (row * self.size)
                x2 = x1 + self.size
                y2 = y1 + self.size
                self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square")
                color = self.color1 if color == self.color2 else self.color2
        for name in self.pieces:
            self.placepiece(name, self.pieces[name][0], self.pieces[name][1])
        self.canvas.tag_raise("piece")
        self.canvas.tag_lower("square")

#this finds where the mouse is and returns it also this is where im trying to get the dict bit to work
    def callback(self, event):
        print ("clicked at", int(event.x/64), int(event.y/64))
        x1 = int(event.x/64)
        y1 = int(event.y/64)
        pieces = self.pieces
        print(pieces)
        for x0, y0 in pieces.items():    
            if x0 == x1 and y0 == y1:
                print(pieces)

        return int(event.y/64), int(event.x/64)

#this is where all the pieces get created the first time and placed on the board
if __name__ == "__main__":
    root = tk.Tk()
    board = GameBoard(root)
    board.pack(side="top", fill="both", expand=True, padx=0, pady=0)
    #bishops
    bishopb = tk.PhotoImage(file = 'D:/comp coursework/bishopb.png')
    board.addpiece("bishopb1",bishopb, 0,2)
    bishopw = tk.PhotoImage(file = 'D:/comp coursework/bishopw.png')
    board.addpiece("bishopw1",bishopw, 7,2)
    board.addpiece("bishopb2",bishopb, 0,5)
    board.addpiece("bishopw2",bishopw, 7,5)
    #kings
    kingb = tk.PhotoImage(file = 'D:/comp coursework/kingb.png')
    kingw = tk.PhotoImage(file = 'D:/comp coursework/kingw.png')
    board.addpiece("kingb",kingb, 0,4)
    board.addpiece("kingw",kingw, 7,4)
    #queens
    queenb = tk.PhotoImage(file = 'D:/comp coursework/queenb.png')
    queenw = tk.PhotoImage(file = 'D:/comp coursework/queenw.png')
    board.addpiece("queenb",queenb, 0,3)
    board.addpiece("queenw",queenw, 7,3)
    #kinghts
    knightb = tk.PhotoImage(file = 'D:/comp coursework/knightb.png')
    knightw = tk.PhotoImage(file = 'D:/comp coursework/knightw.png')
    board.addpiece("knightb1",knightb, 0,1)
    board.addpiece("knightw1",knightw, 7,1)
    board.addpiece("knightb2",knightb, 0,6)
    board.addpiece("knightw2",knightw, 7,6)
    #rooks
    rookb = tk.PhotoImage(file = 'D:/comp coursework/rookb.png')
    rookw = tk.PhotoImage(file = 'D:/comp coursework/rookw.png')
    board.addpiece("rookb1",rookb, 0,0)
    board.addpiece("rookw1",rookw, 7,0)
    board.addpiece("rookb2",rookb, 0,7)
    board.addpiece("rookw2",rookw, 7,7)
    #pawns
    pawnb = tk.PhotoImage(file = 'D:/comp coursework/pawnb.png')
    pawnw = tk.PhotoImage(file = 'D:/comp coursework/pawnw.png')
    board.addpiece("pawn1b",pawnb, 1,0)
    board.addpiece("pawn2b",pawnb, 1,1)
    board.addpiece("pawn3b",pawnb, 1,2)
    board.addpiece("pawn4b",pawnb, 1,3)
    board.addpiece("pawn5b",pawnb, 1,4)
    board.addpiece("pawn6b",pawnb, 1,5)
    board.addpiece("pawn7b",pawnb, 1,6)
    board.addpiece("pawn8b",pawnb, 1,7)
    board.addpiece("pawn1w",pawnw, 6,0)
    board.addpiece("pawn2w",pawnw, 6,1)
    board.addpiece("pawn3w",pawnw, 6,2)
    board.addpiece("pawn4w",pawnw, 6,3)
    board.addpiece("pawn5w",pawnw, 6,4)
    board.addpiece("pawn6w",pawnw, 6,5)
    board.addpiece("pawn7w",pawnw, 6,6)
    board.addpiece("pawn8w",pawnw, 6,7)

    board.placepiece("bishopb1",4,4)

    root.bind("<Button-1>", board.callback)

    #board.placepiece("pawn8w",)




    root.mainloop()

0 个答案:

没有答案