python单机五子棋详解(tkinter)

  • 简介
  • 样式创建
  • 逻辑编写

简介

这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏。面向新手。

程序主要包括两个部分,图形创建与逻辑编写两部分。

程序的运行结果:

样式创建

老规矩,先把用到的包导入进来。

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0
'''from tkinter import *
import math

然后建立一个样式的类,类名称chessBoard。这里加了很多注释,避免新手看不懂函数的作用,说实话我觉得挺别扭的。

#定义棋盘类
class chessBoard() :def __init__(self) :#创建一个tk对象,即窗口self.window = Tk()#窗口命名self.window.title("五子棋游戏")#定义窗口大小self.window.geometry("660x470")#定义窗口不可放缩self.window.resizable(0,0)#定义窗口里的画布self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)#画出画布内容self.paint_board()#定义画布所在的网格self.canvas.grid(row = 0 , column = 0)def paint_board(self) :#画横线for row in range(0,15) :if row == 0 or row == 14 :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)else :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)#画竖线for column in range(0,15) :if column == 0 or column == 14 :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)else :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)#画圆self.canvas.create_oval(112, 112, 118, 118, fill="black")self.canvas.create_oval(352, 112, 358, 118, fill="black")self.canvas.create_oval(112, 352, 118, 358, fill="black")self.canvas.create_oval(232, 232, 238, 238, fill="black")self.canvas.create_oval(352, 352, 358, 358, fill="black")

逻辑编写

这里的主要看每个函数的功能就好了。

#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :#初始化def __init__(self) :self.board = chessBoard()self.game_print = StringVar()self.game_print.set("")#16*16的二维列表,保证不会out of indexself.db = [([2] * 16) for i in range(16)]#悔棋用的顺序列表self.order = []#棋子颜色self.color_count = 0 self.color = 'black'#清空与赢的初始化,已赢为1,已清空为1self.flag_win = 1self.flag_empty = 1self.options()#黑白互换def change_color(self) :self.color_count = (self.color_count + 1 ) % 2if self.color_count == 0 :self.color = "black"elif self.color_count ==1 :self.color = "white"#落子def chess_moving(self ,event) :#不点击“开始”与“清空”无法再次开始落子if self.flag_win ==1 or self.flag_empty ==0  :return#坐标转化为下标x,y = event.x-25 , event.y-25x = round(x/30)y = round(y/30)#点击位置没用落子,且没有在棋盘线外,可以落子while self.db[y][x] == 2 and self.limit_boarder(y,x):self.db[y][x] = self.color_countself.order.append(x+15*y)   self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")if self.game_win(y,x,self.color_count) :print(self.color,"获胜")self.game_print.set(self.color+"获胜")else :self.change_color()self.game_print.set("请"+self.color+"落子")#保证棋子落在棋盘上def limit_boarder(self , y , x) :if x<0 or x>14 or y<0 or y>14 :return Falseelse :return True#计算连子的数目,并返回最大连子数目def chessman_count(self , y , x , color_count ) :count1,count2,count3,count4 = 1,1,1,1#横计算for i in range(-1 , -5 , -1) :if self.db[y][x+i] == color_count  :count1 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y][x+i] == color_count  :count1 += 1else:break #竖计算for i in range(-1 , -5 , -1) :if self.db[y+i][x] == color_count  :count2 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x] == color_count  :count2 += 1else:break #/计算for i in range(-1 , -5 , -1) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break #\计算for i in range(-1 , -5 , -1) :if self.db[y+i][x-i] == color_count :count4 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x-i] == color_count :count4 += 1else:break return max(count1 , count2 , count3 , count4)#判断输赢def game_win(self , y , x , color_count ) :if self.chessman_count(y,x,color_count) >= 5 :self.flag_win = 1self.flag_empty = 0return Trueelse :return False#悔棋,清空棋盘,再画剩下的n-1个棋子def withdraw(self ) :if len(self.order)==0 or self.flag_win == 1:returnself.board.canvas.delete("chessman")z = self.order.pop()x = z%15y = z//15self.db[y][x] = 2self.color_count = 1for i in self.order :ix = i%15iy = i//15self.change_color()self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")self.change_color()self.game_print.set("请"+self.color+"落子")#清空def empty_all(self) :self.board.canvas.delete("chessman")#还原初始化self.db = [([2] * 16) for i in range(16)]self.order = []self.color_count = 0 self.color = 'black'self.flag_win = 1self.flag_empty = 1self.game_print.set("")#将self.flag_win置0才能在棋盘上落子def game_start(self) :#没有清空棋子不能置0开始if self.flag_empty == 0:returnself.flag_win = 0self.game_print.set("请"+self.color+"落子")def options(self) :self.board.canvas.bind("<Button-1>",self.chess_moving)Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)self.board.window.mainloop()

最后,main函数


if __name__ == "__main__":game = Gobang()

将以上的所有程序复制粘贴,即为完整的程序了,可以运行。
最后来一个完整程序,一个一个复制粘贴简直不要太麻烦。

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0'''from tkinter import *
import math #定义棋盘类
class chessBoard() :def __init__(self) :self.window = Tk()self.window.title("五子棋游戏")self.window.geometry("660x470")self.window.resizable(0,0)self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)self.paint_board()self.canvas.grid(row = 0 , column = 0)def paint_board(self) :for row in range(0,15) :if row == 0 or row == 14 :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)else :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)for column in range(0,15) :if column == 0 or column == 14 :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)else :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)self.canvas.create_oval(112, 112, 118, 118, fill="black")self.canvas.create_oval(352, 112, 358, 118, fill="black")self.canvas.create_oval(112, 352, 118, 358, fill="black")self.canvas.create_oval(232, 232, 238, 238, fill="black")self.canvas.create_oval(352, 352, 358, 358, fill="black")#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :#初始化def __init__(self) :self.board = chessBoard()self.game_print = StringVar()self.game_print.set("")#16*16的二维列表,保证不会out of indexself.db = [([2] * 16) for i in range(16)]#悔棋用的顺序列表self.order = []#棋子颜色self.color_count = 0 self.color = 'black'#清空与赢的初始化,已赢为1,已清空为1self.flag_win = 1self.flag_empty = 1self.options()#黑白互换def change_color(self) :self.color_count = (self.color_count + 1 ) % 2if self.color_count == 0 :self.color = "black"elif self.color_count ==1 :self.color = "white"#落子def chess_moving(self ,event) :#不点击“开始”与“清空”无法再次开始落子if self.flag_win ==1 or self.flag_empty ==0  :return#坐标转化为下标x,y = event.x-25 , event.y-25x = round(x/30)y = round(y/30)#点击位置没用落子,且没有在棋盘线外,可以落子while self.db[y][x] == 2 and self.limit_boarder(y,x):self.db[y][x] = self.color_countself.order.append(x+15*y)   self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")if self.game_win(y,x,self.color_count) :print(self.color,"获胜")self.game_print.set(self.color+"获胜")else :self.change_color()self.game_print.set("请"+self.color+"落子")#保证棋子落在棋盘上def limit_boarder(self , y , x) :if x<0 or x>14 or y<0 or y>14 :return Falseelse :return True#计算连子的数目,并返回最大连子数目def chessman_count(self , y , x , color_count ) :count1,count2,count3,count4 = 1,1,1,1#横计算for i in range(-1 , -5 , -1) :if self.db[y][x+i] == color_count  :count1 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y][x+i] == color_count  :count1 += 1else:break #竖计算for i in range(-1 , -5 , -1) :if self.db[y+i][x] == color_count  :count2 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x] == color_count  :count2 += 1else:break #/计算for i in range(-1 , -5 , -1) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break #\计算for i in range(-1 , -5 , -1) :if self.db[y+i][x-i] == color_count :count4 += 1else:break for i in  range(1 , 5 ,1 ) :if self.db[y+i][x-i] == color_count :count4 += 1else:break return max(count1 , count2 , count3 , count4)#判断输赢def game_win(self , y , x , color_count ) :if self.chessman_count(y,x,color_count) >= 5 :self.flag_win = 1self.flag_empty = 0return Trueelse :return False#悔棋,清空棋盘,再画剩下的n-1个棋子def withdraw(self ) :if len(self.order)==0 or self.flag_win == 1:returnself.board.canvas.delete("chessman")z = self.order.pop()x = z%15y = z//15self.db[y][x] = 2self.color_count = 1for i in self.order :ix = i%15iy = i//15self.change_color()self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")self.change_color()self.game_print.set("请"+self.color+"落子")#清空def empty_all(self) :self.board.canvas.delete("chessman")#还原初始化self.db = [([2] * 16) for i in range(16)]self.order = []self.color_count = 0 self.color = 'black'self.flag_win = 1self.flag_empty = 1self.game_print.set("")#将self.flag_win置0才能在棋盘上落子def game_start(self) :#没有清空棋子不能置0开始if self.flag_empty == 0:returnself.flag_win = 0self.game_print.set("请"+self.color+"落子")def options(self) :self.board.canvas.bind("<Button-1>",self.chess_moving)Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)self.board.window.mainloop()if __name__ == "__main__":game = Gobang()

python单机五子棋详解(tkinter)相关推荐

  1. python嗅探工具详解附源码(使用socket,带tkinter界面)

    python嗅探工具详解(带tkinter界面) 点击详见原理 点击详见原理 TCP/IP协议号补充 必备基础知识 IP数据包格式 详见点此 TCP报文格式 详见点此 struct模块 在Python ...

  2. python20191031_20191031:Python取反运算详解

    20191031:Python取反运算详解 取反运算:~3 == 4 1.对于数字 3 =======>转换为二进制表示为011 2.对011取反为100 3.为什么表示-4 a.计算机用补码表 ...

  3. Python字符编码详解

    Python字符编码详解 转自http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html Python字符编码详解 本文简单介绍了各种常用的 ...

  4. python的执行过程_在交互式环境中执行Python程序过程详解

    前言 相信接触过Python的伙伴们都知道运行Python脚本程序的方式有多种,目前主要的方式有:交互式环境运行.命令行窗口运行.开发工具上运行等,其中在不同的操作平台上还互不相同.今天,小编讲些Py ...

  5. windows上安装Anaconda和python的教程详解

    一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...

  6. python变量类型-Python 变量类型详解

    变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...

  7. python安装教程windows-windows上安装Anaconda和python的教程详解

    一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...

  8. python语言编程基础-Python语言入门详解!快速学成Python!

    原标题:Python语言入门详解!快速学成Python! 很多技能是被职场所需要的,但很可惜... 这些技能在大学中并学习不到. 大学和职场现实存在的横沟对大部分同学来说难以跨越或碰得头破血流... ...

  9. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几 ...

最新文章

  1. 多分类问题的ROC曲线绘制
  2. 在Eclipse中制作SSH配置文件提示插件
  3. php %3cphp用大括号表示,整理HTML5中支持的URL编码与字符编码_html5教程技巧
  4. Fabric权限管理和策略
  5. [云炬python3玩转机器学习]sklearn中的Scaler
  6. servlet的理解
  7. TCP/IP协议族-----10、搬家IP
  8. lz98n外接电源注意问题
  9. 2021年“最美科技工作者”李德仁院士:科学要为祖国服务
  10. java 俄罗斯方块窗口_[代码全屏查看]-java 俄罗斯方块
  11. python绘制一棵樱花树
  12. VS2012统计代码量
  13. 软考 - 系统架构设计师资料
  14. 税盘的批量抄报税和批量清卡--支持金税盘,税控盘,税务UKey
  15. 犀牛插件学习——T-Splines
  16. 3G模块驱动运用开发总结
  17. matlab 图片雾化代码,雾化效果图片制作代码汇总
  18. 中文邮件格式模板、工作汇报邮件模板这样写,90%人都爱看
  19. linux内核源码分析之CFS调度
  20. 智能温度计APP一键开发(快速搭建)

热门文章

  1. 软件工程导论复试问题总结
  2. 成都拓嘉启远:拼多多取消订单以及退款规则详解
  3. 小技巧:每一行输出4个元素
  4. 打印机里找不到服务器属性,win7没有打印机服务器属性设置
  5. 不寻常的发现:“猎头”行业的独家解密zz
  6. 2018-12-18全球区块链今日热点
  7. Learning-Driven Interference-Aware Workload Parallelization for Streaming Applications (TPDS2021)
  8. 支付宝缴纳罚款显示服务器在维护,交通罚款支付宝交不了原因分析
  9. 新建Android项目,会出现两个项目一个是自己创建的项目,另一个是“appcompat_v7”项目,这是怎么回事呢?该怎么解决呢?...
  10. 设定可执行目标的4个技巧