简易五子棋上篇-设计界面

目标:

想要写一个人人对弈的五子棋游戏,要是GUI界面的,能够判断输赢、提示游戏结束,能重新开局。

思路:

  • 分析:我需要棋盘、棋子、输赢的显示区、落棋方的提示区以及一个重新开局按钮。使用Python库 tkinter
  • 棋盘绘制:
    • 窗口左半部分放置一块画布 canvas 用于棋盘绘制;右半部分放置四个标签 label ,分别用来落棋方提示的图片放置、文字放置,结果显示和状态显示;此外右下角放置一个按钮 button 用于重置。
    • 画布上设置背景,绘制线条、点、坐标的字母和数字
    • 将标签和按钮放到指定位置

实现:

import tkinter as tk  #补充:如果你用的是Python2,那么很可能库名是Tkinter

"""窗口主体"""
root = tk.Tk()

root.title("Gobang")
root.geometry("760x560")

"""棋子提示"""
side_canvas = tk.Canvas(root, width = 220, height = 50)
side_canvas.grid(row = 0, column = 1)
side_canvas.create_oval(110 - PIECE_SIZE, 25 - PIECE_SIZE,
                        110 + PIECE_SIZE, 25 + PIECE_SIZE,
                        fill = piece_color, tags = ("show_piece") )
"""棋子提示标签"""
var = tk.StringVar()
var.set("执黑棋")
person_label = tk.Label(root, textvariable = var, width = 12, anchor = tk.CENTER, 
                        font = ("Arial", 20) )
person_label.grid(row = 1, column = 1)

"""输赢提示标签"""
var1 = tk.StringVar()
var1.set("")
result_label = tk.Label(root, textvariable = var1, width = 12, height = 4, 
                        anchor = tk.CENTER, fg = "red", font = ("Arial", 25) )
result_label.grid(row = 2, column = 1, rowspan = 2)

"""游戏结束提示标签"""
var2 = tk.StringVar()
var2.set("")
game_label = tk.Label(root, textvariable = var2, width = 12, height = 4, 
                        anchor = tk.CENTER, font = ("Arial", 18) )
game_label.grid(row = 4, column = 1)

"""重置按钮"""
reset_button = tk.Button(root, text = "重新开始", font = 20, 
                          width = 8, command = gameReset)
reset_button.grid(row = 5, column = 1)

"""棋盘绘制"""
#背景
canvas = tk.Canvas(root, bg = "saddlebrown", width = 540, height = 540)
canvas.bind("<Button-1>", coorBack)  #对鼠标进行事件绑定,方便获取点击位置的坐标,下篇会用到
canvas.grid(row = 0, column = 0, rowspan = 6)
#线条
for i in range(15):
    canvas.create_line(32, (35 * i + 38), 522, (35 * i + 38))
    canvas.create_line((35 * i + 32), 38, (35 * i + 32), 528)
#点
point_x = [3, 3, 11, 11, 7]
point_y = [3, 11, 3, 11, 7]
for i in range(5):
    canvas.create_oval(35 * point_x[i] + 28, 35 * point_y[i] + 33, 
                       35 * point_x[i] + 36, 35 * point_y[i] + 41, fill = "black")
#数字坐标
for i in range(15):
    label = tk.Label(canvas, text = str(i + 1), fg = "black", bg = "saddlebrown",
                     width = 2, anchor = tk.E)
    label.place(x = 2, y = 35 * i + 28)
#字母坐标
count = 0
for i in range(65, 81):
    label = tk.Label(canvas, text = chr(i), fg = "black", bg = "saddlebrown")
    label.place(x = 35 * count + 25, y = 2)
    count += 1

"""窗口循环"""
root.mainloop()

效果图:

旧文-五子棋

下篇将分享内部逻辑的写法。(如何落子以及判断输赢)
此为旧文,图片水印为我的CSDN账号,无妨

发表评论

电子邮件地址不会被公开。 必填项已用*标注