鱼C论坛

 找回密码
 立即注册
查看: 3798|回复: 26

[作品展示] 用 Python 的 tkinter 做一个简单的五子棋

[复制链接]
发表于 2023-5-25 18:44:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhangjinxuan 于 2023-7-18 23:01 编辑

程序实现

两个人互相下棋子,看谁的棋子先连成5连方,谁就获胜

思路

打算使用 tkinter。

使用二维数组存储棋盘,每一个格子都是一个 button,这个 button 都有对应的指令和 string

并且定义函数 ckeck, draw, back, init , check 位判断是否有玩家获胜,draw 为将本位置改为对应的棋子(直接修改 string),back 为悔棋,init 为初始化。

代码
  1. from tkinter import *
  2. from tkinter.messagebox import *
  3. from threading import Thread as ths
  4. import sys,time

  5. buttons = [[None for i in range(18)] for j in range(18)]
  6. strings = [[None for i in range(18)] for j in range(18)] #对应的字符串
  7. command = [[lambda x=j, y=i: draw(x, y) for i in range(18)] for j in range(18)]
  8. now_type = 0
  9. last_opts = []
  10. tot = -1
  11. window = Tk()
  12. window.resizable(False, False)
  13. window.geometry("800x600+100+120")
  14. window.title("五子棋——tkinter")
  15. huiqi_state = StringVar(window, "")
  16. white_win = 0
  17. black_win = 0
  18. state = StringVar(window, "")
  19. def clear():
  20.     for i in range(15):
  21.         for j in range(15):
  22.             x = i * 30 + 40
  23.             y = j * 30 + 70
  24.             buttons[i][j].destroy()
  25.             buttons[i][j] = Button(window, bg = "orange", textvariable = strings[i][j], state = 'disabled')
  26.             buttons[i][j].place(x = x, y = y, width = 30, height = 30)
  27. def check():
  28.     for i in range(15):
  29.         for j in range(15):
  30.             if i + 4 < 15:
  31.                 if strings[i + 1][j].get() == strings[i + 2][j].get() == strings[i + 3][j].get() == strings[i + 4][j].get() == strings[i][j].get():
  32.                     if strings[i][j].get() == '':
  33.                         continue
  34.                     clear()
  35.                     if strings[i][j].get() == 'X':
  36.                         showinfo("获胜提示", "黑子获胜")
  37.                     else:
  38.                         showinfo("获胜提示", "白子获胜")
  39.                     for k in range(5):
  40.                         x = (i + k) * 30 + 40
  41.                         y = j * 30 + 70
  42.                         buttons[i + k][j].destroy()
  43.                         buttons[i + k][j] = Button(window, bg = "red", textvariable = strings[i + k][j], state = 'disabled')
  44.                         buttons[i + k][j].place(x = x, y = y, width = 30, height = 30)
  45.                     state.set("游戏结束")
  46.                     return
  47.             if j + 4 < 15:
  48.                 if strings[i][j + 1].get() == strings[i][j + 2].get() == strings[i][j + 3].get() == strings[i][j + 4].get() == strings[i][j].get():
  49.                     if strings[i][j].get() == '':
  50.                         continue
  51.                     clear()
  52.                     if strings[i][j].get() == 'X':
  53.                         showinfo("获胜提示", "黑子获胜")
  54.                     else:
  55.                         showinfo("获胜提示", "白子获胜")
  56.                     for k in range(5):
  57.                         x = i * 30 + 40
  58.                         y = (j + k) * 30 + 70
  59.                         buttons[i][j + k].destroy()
  60.                         buttons[i][j + k] = Button(window, bg = "red", textvariable = strings[i][j + k], state = 'disabled')
  61.                         buttons[i][j + k].place(x = x, y = y, width = 30, height = 30)
  62.                     state.set("游戏结束")
  63.                     return
  64.             if j + 4 < 15 and i + 4 < 15:
  65.                 if strings[i + 1][j + 1].get() == strings[i + 2][j + 2].get() == strings[i + 3][j + 3].get() == strings[i + 4][j + 4].get() == strings[i][j].get():
  66.                     if strings[i][j].get() == '':
  67.                         continue
  68.                     clear()
  69.                     if strings[i][j].get() == 'X':
  70.                         showinfo("获胜提示", "黑子获胜")
  71.                     else:
  72.                         showinfo("获胜提示", "白子获胜")
  73.                     for k in range(5):
  74.                         x = (i + k) * 30 + 40
  75.                         y = (j + k) * 30 + 70
  76.                         buttons[i + k][j + k].destroy()
  77.                         buttons[i + k][j + k] = Button(window, bg = "red", textvariable = strings[i + k][j + k], state = 'disabled')
  78.                         buttons[i + k][j + k].place(x = x, y = y, width = 30, height = 30)
  79.                     state.set("游戏结束")
  80.                     return
  81.             if j + 4 < 15 and i - 4 >= 0:
  82.                 if strings[i - 1][j + 1].get() == strings[i - 2][j + 2].get() == strings[i - 3][j + 3].get() == strings[i - 4][j + 4].get() == strings[i][j].get():
  83.                     if strings[i][j].get() == '':
  84.                         continue
  85.                     clear()
  86.                     if strings[i][j].get() == 'X':
  87.                         showinfo("获胜提示", "黑子获胜")
  88.                     else:
  89.                         showinfo("获胜提示", "白子获胜")
  90.                     for k in range(5):
  91.                         x = (i - k) * 30 + 40
  92.                         y = (j + k) * 30 + 70
  93.                         buttons[i - k][j + k].destroy()
  94.                         buttons[i - k][j + k] = Button(window, bg = "red", textvariable = strings[i - k][j + k], state = 'disabled')
  95.                         buttons[i - k][j + k].place(x = x, y = y, width = 30, height = 30)
  96.                     state.set("游戏结束")
  97.                     return
  98. def draw(x, y):
  99.     global now_type, window, buttons, strings, commands, tot
  100.     if now_type == 0:
  101.         strings[x][y].set("X")
  102.         now_type = 1
  103.         state.set("请白手落子")
  104.     else:
  105.         strings[x][y].set("O")
  106.         now_type = 0
  107.         state.set("请黑手落子")
  108.     buttons[x][y].destroy()
  109.     buttons[x][y] = Button(window, textvariable = strings[x][y], bg = "orange", fg = "white" if now_type == 0 else "black")
  110.     buttons[x][y].place(x = x * 30 + 40, y = y * 30 + 70, width = 30, height = 30)
  111.     tot += 1
  112.     last_opts.append((x, y))
  113.     huiqi_state.set(f"")
  114.     check()
  115. def back():
  116.     if state.get() == "游戏结束":
  117.         return
  118.     global now_type, window, buttons, strings, commands, tot, huiqi_state
  119.     if tot == -1:
  120.         huiqi_state.set(f"无效悔棋。")
  121.         return
  122.     x = last_opts[tot][0]
  123.     y = last_opts[tot][1]
  124.     if now_type == 1:
  125.         strings[x][y] = StringVar(window, "")
  126.         now_type = 0
  127.         state.set("请黑手落子")
  128.     else:
  129.         state.set("请白手落子")
  130.         strings[x][y] = StringVar(window, "")
  131.         now_type = 1
  132.     buttons[x][y].destroy()
  133.     buttons[x][y] = Button(window, textvariable = strings[x][y], command = command[x][y], bg = "orange")
  134.     buttons[x][y].place(x = x * 30 + 40, y = y * 30 + 70, width = 30, height = 30)
  135.     huiqi_state.set(f"已撤销 {'白子' if now_type == 1 else '黑子'} \n 在 ({x}, {y}) 的操作。")
  136.     tot -= 1
  137.     last_opts.pop()
  138. def init():
  139.     global tot, now_type, last_opts
  140.     tot = -1
  141.     now_type = 0
  142.     state.set("请黑手落子")
  143.     huiqi_state = StringVar(window, "")
  144.     last_opts = []
  145.     for i in range(15):
  146.         for j in range(15):
  147.             strings[i][j] = StringVar(window, "")
  148.             x = i * 30 + 40
  149.             y = j * 30 + 70
  150.             if buttons[i][j] != None:
  151.                 buttons[i][j].destroy()
  152.             buttons[i][j] = Button(window, bg = "orange", textvariable = strings[i][j], command = command[i][j])
  153.             buttons[i][j].place(x = x, y = y, width = 30, height = 30)
  154. def main():
  155.     global now_type, window, buttons, strings, commands
  156.     Label(window, text = "五子棋", font = ('等线', 20)).place(x = 350, y = 20)
  157.     Label(window, textvariable = huiqi_state, font = ('等线', 13)).place(x = 630, y = 500)
  158.     Button(window, text = "悔棋", command = back).place(x = 550, y = 500, width = 70, height = 40)
  159.     Button(window, text = "重置", command = init).place(x = 550, y = 400, width = 70, height = 40)
  160.     Label(window, textvariable = state, font = ('等线', 14)).place(x = 550, y = 300)
  161.     init()
  162.     Label(window, textvariable = huiqi_state, font = ('等线', 13)).place(x = 630, y = 500)
  163. main()
  164. mainloop()
复制代码


本程序主要不是求得评分,而是为了狠狠地打 @sfqxx 的脸,整个代码创作的时间小于 3 小时,码量适中,165 行,大部分都是复制粘贴。

sfqxx,不服来战!

@歌者文明清理员 @元豪

评分

参与人数 3荣誉 +8 贡献 +2 收起 理由
liuhongrun2022 + 5
歌者文明清理员 + 3
sfqxx + 3 -1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-25 18:46:46 From FishC Mobile | 显示全部楼层
行,有空我拿你的来对比一下我的

看看谁打谁的脸?

点评

巧了,我也在做五子棋  发表于 2023-5-25 18:49
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:48:43 | 显示全部楼层
啊评错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:49:33 | 显示全部楼层
不是我不想做,而是这个太简单了。

评分

参与人数 1鱼币 +5 收起 理由
歌者文明清理员 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:51:30 | 显示全部楼层
sfqxx 发表于 2023-5-25 18:46
行,有空我拿你的来对比一下我的

看看谁打谁的脸?

你要更精美的界面,可以,但我不想做,这样的简易版就能满足很多要求了。

评分

参与人数 1鱼币 +5 收起 理由
歌者文明清理员 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:52:05 | 显示全部楼层
zhangjinxuan 发表于 2023-5-25 18:49
不是我不想做,而是这个太简单了。

巧了,我也在做五子棋(pygame)
元豪也在做,而且他也是pygame
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:52:32 | 显示全部楼层

至少我还完成了与 歌者文明清理员 相同的挑战

评分

参与人数 1鱼币 +4 收起 理由
歌者文明清理员 + 4

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:53:05 | 显示全部楼层
歌者文明清理员 发表于 2023-5-25 18:52
巧了,我也在做五子棋(pygame)
元豪也在做,而且他也是pygame

pygame 已经被我忘得一干二净
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:53:33 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:53:36 | 显示全部楼层
歌者文明清理员 发表于 2023-5-25 18:52
巧了,我也在做五子棋(pygame)
元豪也在做,而且他也是pygame

主要是 tkinter 容易实现落子,就是一个按钮
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:54:12 | 显示全部楼层
zhangjinxuan 发表于 2023-5-25 18:53
主要是 tkinter 容易实现落子,就是一个按钮

pygame的按钮,棋子啥的都要自己做,比较好控制
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:55:42 | 显示全部楼层
zhangjinxuan 发表于 2023-5-25 18:53
主要是 tkinter 容易实现落子,就是一个按钮

你有背景吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:57:23 | 显示全部楼层

纯色背景,你的还不是纯色背景
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:58:07 | 显示全部楼层
歌者文明清理员 发表于 2023-5-25 18:54
pygame的按钮,棋子啥的都要自己做,比较好控制

好主意

可我只会 tkinter
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 18:58:25 | 显示全部楼层
怎么就不是纯色的呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 18:59:09 | 显示全部楼层
歌者文明清理员 发表于 2023-5-25 18:53
https://fishc.com.cn/forum.php?mod=forumdisplay&fid=243&filter=typeid&typeid=605

可以供个参考

但我搞竞赛,可惜竞赛不考 pygame
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 19:02:49 | 显示全部楼层
zhangjinxuan 发表于 2023-5-25 18:59
可以供个参考

但我搞竞赛,可惜竞赛不考 pygame

没事啦,我也要更pygame教程了https://fishc.com.cn/thread-228565-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 19:04:06 | 显示全部楼层
本帖最后由 zhangjinxuan 于 2023-5-25 19:08 编辑
sfqxx 发表于 2023-5-25 18:58
怎么就不是纯色的呢?


都是纯色啊,你是橘色,我是灰色。

我的程序有以下优势:

1. 不受界面限制,scratch 界面受限,tkinter 界面几乎不受限制,这就可以让我的程序空间发挥更大。
2. 界面简洁,有直观的文字提示;
3. 可以直接重置棋盘,对局结束无需重新运行,直接一键重置‘
4. 有"谁获胜“了的提示,并且高亮连成 5 子的棋子。
5. 当场上无子,终止悔棋操作。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-25 19:13:21 | 显示全部楼层
zhangjinxuan 发表于 2023-5-25 19:04
都是纯色啊,你是橘色,我是灰色。

我的程序有以下优势:


不都一样吗?第3条比较多余

直接点绿旗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 19:15:14 | 显示全部楼层
sfqxx 发表于 2023-5-25 19:13
不都一样吗?第3条比较多余

直接点绿旗

点绿棋还不是相当于重新开始程序?

重新开始的话就无法统计获胜的次数,我这样就可以很好实现,只不过懒

这样,说程序优势没意义,只能说语言优势。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 23:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表