本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

以下文章来源于Python家庭,作者Python家庭

实战项目:使用Python编写一个能够完成基本对战的五子棋游戏。面向新手。

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

程序的运行结果:

样式创建

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

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")

逻辑编写

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

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

最后,main函数

if __name__ == "__main__":

game = Gobang()

将以上的所有程序复制粘贴,即为完整的程序了,可以运行。

最后来一个完整程序,一个一个复制粘贴简直不要太麻烦。

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 index

self.db = [([2] * 16) for i in range(16)]

#悔棋用的顺序列表

self.order = []

#棋子颜色

self.color_count = 0

self.color = 'black'

#清空与赢的初始化,已赢为1,已清空为1

self.flag_win = 1

self.flag_empty = 1

self.options()

#黑白互换

def change_color(self) :

self.color_count = (self.color_count + 1 ) % 2

if 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-25

x = 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_count

self.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 False

else :

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 += 1

else:

break

for i in range(1 , 5 ,1 ) :

if self.db[y][x+i] == color_count :

count1 += 1

else:

break

#竖计算

for i in range(-1 , -5 , -1) :

if self.db[y+i][x] == color_count :

count2 += 1

else:

break

for i in range(1 , 5 ,1 ) :

if self.db[y+i][x] == color_count :

count2 += 1

else:

break

#/计算

for i in range(-1 , -5 , -1) :

if self.db[y+i][x+i] == color_count :

count3 += 1

else:

break

for i in range(1 , 5 ,1 ) :

if self.db[y+i][x+i] == color_count :

count3 += 1

else:

break

#\计算

for i in range(-1 , -5 , -1) :

if self.db[y+i][x-i] == color_count :

count4 += 1

else:

break

for i in range(1 , 5 ,1 ) :

if self.db[y+i][x-i] == color_count :

count4 += 1

else:

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 = 1

self.flag_empty = 0

return True

else :

return False

#悔棋,清空棋盘,再画剩下的n-1个棋子

def withdraw(self ) :

if len(self.order)==0 or self.flag_win == 1:

return

self.board.canvas.delete("chessman")

z = self.order.pop()

x = z%15

y = z//15

self.db[y][x] = 2

self.color_count = 1

for i in self.order :

ix = i%15

iy = i//15

self.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 = 1

self.flag_empty = 1

self.game_print.set("")

#将self.flag_win置0才能在棋盘上落子

def game_start(self) :

#没有清空棋子不能置0开始

if self.flag_empty == 0:

return

self.flag_win = 0

self.game_print.set("请"+self.color+"落子")

def options(self) :

self.board.canvas.bind("",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制作单机五子棋游戏的文章就介绍到这了,更多相关Python tkinter单机五子制作内容请搜索聚米学院以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚米学院!

python五子棋游戏from tkinter import_Python tkinter制作单机五子棋游戏相关推荐

  1. python 单机程序_Python tkinter制作单机五子棋游戏

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  2. python五子棋游戏from tkinter import_Python tkinter制作单机五子棋游戏 | 一台服务器网...

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  3. 基于Python/Tkinter的飞机大战单机小游戏

    这是很早之前课余时间写的基于Python/Tkinter单机小游戏,用来练手,今天将代码贴出来,方便大家一起学习,通过Py/Tk对于学习GUI作为一个入口,其实是个不错入口,在这里推荐一下Tcl/Tk ...

  4. 五子棋游戏html5界面设计,HTML5制作黑白五子棋游戏教程

    今天分享一个只用css和js代码开发的黑白五子棋游戏,我们把代码进行解析,希望对大家可以有所帮助 先看看效果图: HTML5制作黑白五子棋游戏教程 js代码: 定义canvas及黑白棋变量 var c ...

  5. android游戏开发引擎唤境制作单机俯角射击h5小游戏教程

    1.新建项目 首先下载唤境,打开唤境,点击欢迎页左上角的新建项目按钮. 在弹出的项目设置中,选择窗口尺寸为1920*1080.命名为"俯角射击". 点击确定,即可进入我们新建的项目 ...

  6. html做的小游戏,41个用HTML5制作完成的游戏作品

    了解的人都应该知道HTML5有大量的动画和交互功能,完全可以用来完成一些游戏类的操作,今天收集分享:41个用HTML5制作完成的游戏作品,希望其中有你喜欢和需要的,或者可以给你带来灵感的. 1-Pir ...

  7. 计算机游戏配机方案,爽玩单机大型游戏 万元级i7-8700K配RTX2070高端电脑主机配置方案...

    虽然,intel已经推出了全新九代处理器,但是由于相比八代提升不大,没有七代到八代性能提升的诱惑力,加之价格偏贵,所以在目前,intel八代依然是主流装机平台.近期有一个网友称自己想要配一台一万元的高 ...

  8. 用blockly制作诗词学习游戏

    用blockly制作诗词学习游戏 文章目录 用blockly制作诗词学习游戏 引言 效果预览 制作流程 具体效果实现原理 一打开网页音乐就循环播放的原理 检查按钮工作原理 "标准答案&quo ...

  9. 使用unity3d制作像素鸟游戏

    个人博客文章链接:http://www.huqj.top/article?id=140 unity3d虽然是被设计用来制作3D游戏的,但是它提供了很多2D组件,所以也可以轻松的设计制作2D游戏,下面记 ...

最新文章

  1. 关于ATL生成COM注册失败解决方法
  2. 连接myeclipse和mysql数据库,MyEclipse与Mysql数据库的连接
  3. oracle时间用法
  4. 实现mvcc_一文读懂 etcd 的 mvcc 实现
  5. LeetCode 91. 解码方法(动态规划)
  6. TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
  7. Kubernetes 详解
  8. python3爬虫 - cookie登录实战
  9. Deadline调度器之(二):细节和使用方法
  10. iOS开发工具,ios开发类库
  11. 剑指Offer之二维数组中的查找
  12. java 基准测试 格式_JMH java基准测试
  13. android 4.4 x86 iso,android x86官方版下载_android x86 4.4 iso 官方最新版[网盘资源]_零度软件园...
  14. 2020-12-01
  15. success: function(res) {} 和 success: res = {}
  16. 萝卜家园 Win XP 极速安装版 3.0
  17. VBA自定义函数TEXTJOIN CONCAT FILTER EVALUATE
  18. 大数据课程体系-学习笔记概要
  19. 创意电子学小知识:电位器
  20. 11大Java开源中文分词器的使用方法和分词效果对比(转)

热门文章

  1. python50行小游戏_50行python代码实现的贪吃蛇小游戏
  2. (Matlab实现)蚂蚁狮子优化算法在电力系统中的应用
  3. AI学习笔记(六)三维计算机视觉与点云模型
  4. python实用的PDF自动化办公:解密、加水印、PPT/Word/TxT转PDF
  5. python的两种退出方式
  6. TCP 连接管理机制(二)——TCP四次挥手的TIME_WAIT、CLOSE_WAIT状态
  7. ssl证书验证失败打不开网页
  8. HTMLCSS三列布局
  9. Matlab中legend的位置
  10. Vue3报错之 Failed to load resource: the server responded with a status of 404 (Not Found)