用Python创建类似于棋盘游戏的网格

用Python创建类似于棋盘游戏的网格,第1张

用Python创建类似于棋盘游戏的网格

使用Tkinter绘制棋盘非常简单。这是一个非常简单的示例

import Tkinter as tkclass GameBoard(tk.frame):    def __init__(self, parent, rows=8, columns=8, size=32, color1="white", color2="blue"):        '''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 = {}        canvas_width = columns * size        canvas_height = rows * size        tk.frame.__init__(self, parent)        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,          width=canvas_width, height=canvas_height, background="bisque")        self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)        # this binding will cause a refresh if the user interactively        # changes the window size        self.canvas.bind("<Configure>", self.refresh)    def addpiece(self, name, image, row=0, column=0):        '''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)    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)    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")# image comes from the silk icon set which is under a Creative Commons# license. For more information see http://www.famfamfam.com/lab/icons/silk/imagedata = '''    R0lGODlhEAAQAOeSAKx7Fqx8F61/G62CILCJKriIHM+HALKNMNCIANKKANOMALuRK7WOVLWPV9eR    ANiSANuXAN2ZAN6aAN+bAOCcAOKeANCjKOShANKnK+imAOyrAN6qSNaxPfCwAOKyJOKyJvKyANW0    R/S1APW2APW3APa4APe5APm7APm8APq8AO28Ke29LO2/LO2/L+7BM+7BNO6+Re7CMu7BOe7DNPHA    P+/FOO/FO+jGS+/FQO/GO/DHPOjBdfDIPPDJQPDISPDKQPDKRPDIUPHLQ/HLRerMV/HMR/LNSOvH    fvLOS/rNP/LPTvLOVe/LdfPRUfPRU/PSU/LPaPPTVPPUVfTUVvLPe/LScPTWWfTXW/TXXPTXX/XY    Xu/SkvXZYPfVdfXaY/TYcfXaZPXaZvbWfvTYe/XbbvHWl/bdaPbeavvadffea/bebvffbfbdfPvb    e/fgb/Pam/fgcvfgePTbnfbcl/bfivfjdvfjePbemfjelPXeoPjkePbfmvffnvbfofjlgffjkvfh    nvjio/nnhvfjovjmlvzlmvrmpvrrmfzpp/zqq/vqr/zssvvvp/vvqfvvuPvvuvvwvfzzwP//////    ////////////////////////////////////////////////////////////////////////////    ////////////////////////////////////////////////////////////////////////////    ////////////////////////////////////////////////////////////////////////////    ////////////////////////////////////////////////////////////////////////////    ////////////////////////////////////////////////////////////////////////////    /////////////////////////////////////////////////////yH+FUNyZWF0ZWQgd2l0aCBU    aGUgR0lNUAAh+QQBCgD/ACwAAAAAEAAQAAAIzAD/CRxIsKDBfydMlBhxcGAKNIkgPTLUpcPBJIUa    +VEThswfPDQKokB0yE4aMFiiOPnCJ8PAE20Y6VnTQMsUBkWAjKFyQaCJRYLcmOFipYmRHzV89Kkg    kESkOme8XHmCREiOGC/2TBAowhGcAyGkKBnCwwKAFnciCAShKA4RAhyK9MAQwIMMOQ8EdhBDKMuN    BQMEFPigAsoRBQM1BGLjRIiOGSxWBCmToCCMOXSW2HCBo8qWDQcvMMkzCNCbHQga/qMgAYIDBQZU    yxYYEAA7'''if __name__ == "__main__":    root = tk.Tk()    board = GameBoard(root)    board.pack(side="top", fill="both", expand="true", padx=4, pady=4)    player1 = tk.PhotoImage(data=imagedata)    board.addpiece("player1", player1, 0,0)    root.mainloop()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5632183.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存