python实现简单的五子棋游戏
作者:assasinSteven 发布时间:2023-07-30 13:24:31
标签:python,五子棋
本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下
# -*- coding:utf-8 -*-
# @Time: 2017/8/29 0029 10:14
# @Author: assasin
# @Email: assasin0308@sina.com
from tkinter import *
import math
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 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("<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__':
chess_game = Gobang()
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
javascript经典小游戏汇总
来源:https://blog.csdn.net/assasin0308/article/details/108292206
0
投稿
猜你喜欢
- 2.彻底弄懂CSS盒子模式二(导航栏实例) 3.彻底弄懂CSS盒子模式三(浮动的表演和清除的自述) 4.彻底弄懂CSS盒子模式四(绝对定位和
- 内容摘要:图片随机显示是一个应用非常广泛的技巧。比如随机banner的显示,当你进入一个网站时它的banner总是不同的,或者总有内容不同的
- datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1.da
- 如何发送一个XMLHttpRequest的检索的特定部分HTML标题数据。<html> <head> <tit
- 第一列按照goodsid局部分组,然后在分组后的记录中按照audittime升序排序得到序号,从而显示某商品得第几次变迁。 第二列是取该商品
- 安装python-alipay-sdkpip install python-alipay-sdk --upgrade配置视图函数orders
- Ajax的流行给用户体验带来了很大程序的提升,而“注册“这项做为互联网最常用到的功能也自然而然的成为Ajax最常光顾的地方,实时判断用户输入
- 不知大家对精华区的表格排序终极优化是否还有记忆,当时讨论的结果曾以为是最快的JS排序了,实则不然,按前段时间我发的DHTML性能提升帖(转译
- 什么是RC4算法呢?也许您还不知道,没关系我为您找了下相关资料方便大家查看;RC4加密算法 RC4加密算法是大名鼎鼎的RSA三人组
- 这不是什么原创,是我跟据OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.20
- Python应用编程需要用到的针对不同数据库引擎的数据库接口:http://wiki.python.org/moin/DatabaseInt
- 本文实例讲述了彻底删除thinkphp3.1案例blog标签的方法。分享给大家供大家参考。具体方法如下:thinkphp3.1框架中的案例b
- 由于业务需要,要查询客户的ip地址,将部分地区的客户过滤出来,开始想到使用ip数据库,发现读取纯真数据库的难度对我来说有些大,而我目前的时间
- 很久之前就对jQuery.animate的实现非常感兴趣,不过前段时间很忙,直到前几天端午假期才有时间去研究。jQuery.animate的
- PHP 301跳转的小代码<?php $the_host = $_SERVER['HTTP
- 还是决定冠上ajax的头衔,毕竟很多人会用这个关键词搜索。虽然我认为这只是个炒作的概念,不过不得不承认ajax叫起来要方便多了。ajax的意
- 本讲的内容是使用ASP的ActiveX Server Components(组件),说实话下面的内置组件我们用的很少。一、 Browser
- 这篇分享几个在地址栏实现的Javascript有趣效果和应用。能在浏览器地址栏实现的效果太多了,字体放大、显示所有图片、显示Cookie等等
- 一:自动化了解知识工具安装什么样的项目适合做自动化?自动化测试一般在什么阶段开始实施?你们公司自动化的脚本谁来维护?如何维护?自动化用例覆盖
- php操作xml最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星