当前位置 : 主页 > 编程语言 > java >

【案例】用 tkinter 实现五子棋(第一版)

来源:互联网 收集:自由互联 发布时间:2022-06-23
已实现的功能 鼠标控制落子 数据结构的确定 绘制棋盘 显示落子玩家 未实现的功能 判断胜负 计分 # 五子棋 # @author: jiaming import tkinter as tk rate = 0.1 rate_piece = 0.45 grid = 40 color = ( 249 , 21

已实现的功能

  • 鼠标控制落子
  • 数据结构的确定
  • 绘制棋盘
  • 显示落子玩家
  • 未实现的功能

    • 判断胜负
    • 计分
    # 五子棋
    # @author: jiaming
    import tkinter as tk

    rate = 0.1
    rate_piece = 0.45
    grid = 40
    color = (249, 214, 91)
    user = 'black'
    # k = 0
    chessBoardList = [] # [(x, y):"black"]

    def judge():
    global chessBoardList
    # chessBoardList.pop()
    print("判断:", chessBoardList)

    def drawPiece(canvas, coordinate:tuple)->None:
    """
    coordinate: (x, y)
    WB: white/black
    需要完成坐标映射
    """
    global user
    if coordinate[0] % grid >= rate_piece*grid or coordinate[1] % grid >= rate_piece*grid or coordinate[0] >= 16*grid or coordinate[1] >= 16*grid or coordinate[0] < grid or coordinate[1] < grid:
    return None
    x = coordinate[0] // grid
    y = coordinate[1] // grid

    if len(chessBoardList) == 0:
    user = 'white'
    canvas.create_oval(x*grid+rate_piece*grid, y*grid+rate_piece*grid, x*grid-rate_piece*grid, y*grid-rate_piece*grid, width=1, fill="black")
    print("落子: %s (%d, %d)" % ("black", x, y))
    return {(x, y):"black"}
    value = -1
    for k, v in chessBoardList[-1].items():
    value = v
    if value == "black":
    user = 'black'
    canvas.create_oval(x*grid+rate_piece*grid, y*grid+rate_piece*grid, x*grid-rate_piece*grid, y*grid-rate_piece*grid, fill="white")
    print("落子: %s (%d, %d)" % ("white", x, y))
    return {(x, y):"white"}
    else:
    user = 'white'
    canvas.create_oval(x*grid+rate_piece*grid, y*grid+rate_piece*grid, x*grid-rate_piece*grid, y*grid-rate_piece*grid, fill="black")
    print("落子: %s (%d, %d)" % ("black", x, y))
    return {(x, y):"black"}

    def drawBoard():
    # 界面对象的基本参数设置
    root = tk.Tk()
    root.title("五子棋")
    # root.geometry('335x265+250+250')
    # 设置界面是否可以随意拉伸
    root.resizable(False, False)

    canvas = tk.Canvas(root, bg='#F9D65B', height=grid*16, width=grid*16)
    canvas.grid(row=0, column=0)
    label = tk.Label(root, text=user, font=('Times', 16)).grid(row=1, column=0)
    # 绘制线
    canvas.create_rectangle(grid/2, grid/2, 16*grid-grid/2, 16*grid-grid/2, width=2)
    for i in range(1, 16):
    canvas.create_line(grid, i*grid, 15*grid, i*grid, fill='black')
    canvas.create_line(i*grid, grid, i*grid, 15*grid, fill='black')
    # 绘制点
    canvas.create_oval(8*grid+rate*grid, 8*grid+rate*grid, 8*grid-rate*grid, 8*grid-rate*grid, fill="black")
    canvas.create_oval(12*grid+rate*grid, 12*grid+rate*grid, 12*grid-rate*grid, 12*grid-rate*grid, fill="black")
    canvas.create_oval(4*grid+rate*grid, 4*grid+rate*grid, 4*grid-rate*grid, 4*grid-rate*grid, fill="black")
    canvas.create_oval(12*grid+rate*grid, 4*grid+rate*grid, 12*grid-rate*grid, 4*grid-rate*grid, fill="black")
    canvas.create_oval(4*grid+rate*grid, 12*grid+rate*grid, 4*grid-rate*grid, 12*grid-rate*grid, fill="black")
    # 鼠标事件
    def mouseEvent(event):
    # print(f"({event.x}, {event.y})")
    value = drawPiece(canvas, (event.x, event.y))
    tk.Label(root, text=user, font=('Times', 16)).grid(row=1,column=0)
    if value != None:
    chessBoardList.append(value)
    judge()
    # print(chessBoardList)
    # global k
    # k += 1
    # canvas.bind("<Button-1>", lambda event:mouseEvent(event, "white" if k % 2 == 0 else "black"))
    canvas.bind("<Button-1>", lambda event:mouseEvent(event))

    root.mainloop()

    if __name__ == "__main__":
    drawBoard()
    '''
    鼠标点击事件
    <Button-1> 鼠标左键
    <Button-2> 鼠标中间键(滚轮)
    <Button-3> 鼠标右键
    <Double-Button-1> 双击鼠标左键
    <Double-Button-3> 双击鼠标右键
    <Triple-Button-1> 三击鼠标左键
    <Triple-Button-3> 三击鼠标右键
    '''

    运行截图
    【案例】用 tkinter 实现五子棋(第一版)_数据结构
    【案例】用 tkinter 实现五子棋(第一版)_点击事件_02
    【案例】用 tkinter 实现五子棋(第一版)_数据结构_03


    上一篇:【LiteOS】第 0 章
    下一篇:没有了
    网友评论