python实现单机五子棋对战游戏
作者:mfansheng 发布时间:2022-01-11 04:38:10
标签:python,五子棋
本文实例为大家分享了python实现单机五子棋对战的具体代码,供大家参考,具体内容如下
引入pygame模块
# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
initChessList = []
initRole = 1 # 代表白子下 2:代表当前是黑子下
resultFlag = 0
userFlag = True
class StornPoint():
def __init__(self, x, y, value = 0):
'''
:param x: 代表x轴坐标
:param y: 代表y轴坐标
:param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
'''
self.x = x
self.y = y
self.value = value
pass
def initChessSquare(x, y):
'''
初始化棋盘的坐标
:param x:
:param y:
:return:
'''
# 使用二维列表保存了棋盘是的坐标系,和每个落子点的数值
for i in range(15): # 每一行的交叉点坐标
rowList = []
for j in range(15): # 每一列的交叉点坐标
pointX = x + j*40
pointY = y + i*40
# value = 0
sp = StornPoint(pointX, pointY, 0)
rowList.append(sp)
pass
initChessList.append(rowList)
pass
# 处理事件
def eventHandler():
global userFlag
'''
监听各种事件
:return:
'''
for event in pygame.event.get():
global initRole
# 监听点积退出按钮事件
if event.type == QUIT:
pygame.quit()
sys.exit()
pass
# 监听鼠标点积事件
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos() #
print((x, y))
i = j = 0
for temp in initChessList:
for point in temp:
if x >= point.x - 15 and x <= point.x + 15 \
and y >= point.y - 15 and y <= point.y + 15:
# 当前区域没有棋子,并且是白子下
if point.value == 0 and initRole == 1 and userFlag:
point.value = 1
judgeResult(i, j, 1)
initRole = 2 # 切换棋子颜色
pass
elif point.value == 0 and initRole == 2 and userFlag:
point.value = 2
judgeResult(i, j, 2)
initRole = 1 # 切换棋子颜色
pass
break
pass
j += 1
pass
i += 1
j = 0
pass
pass
pass
# 判断输赢函数
def judgeResult(i, j, value):
global resultFlag
flag = False # 用于判断是否已经判决出输赢
for x in range(j - 4, j + 5): # 水平方向有没有出现5连
if x >= 0 and x + 4 < 15 :
if initChessList[i][x].value == value and \
initChessList[i][x + 1].value == value and \
initChessList[i][x + 2].value == value and \
initChessList[i][x + 3].value == value and \
initChessList[i][x + 4].value == value :
flag = True
break
pass
for x in range(i - 4, i + 5): # 垂直方向有没有出现5连
if x >= 0 and x + 4 < 15:
if initChessList[x][j].value == value and \
initChessList[x + 1][j].value == value and \
initChessList[x + 2][j].value == value and \
initChessList[x + 3][j].value == value and \
initChessList[x + 4][j].value == value:
flag = True
break
pass
# 判断东北方向的对角线是否出现了5连
for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
if x >= 0 and x+4 < 15 and y + 4 >= 0 and y < 15:
if initChessList[y][x].value == value and \
initChessList[y - 1][x + 1].value == value and \
initChessList[y - 2][x + 2].value == value and \
initChessList[y - 3][x + 3].value == value and \
initChessList[y - 4][x + 4].value == value:
flag = True
break
pass
pass
pass
# 判断西北方向的对角是否出现了五连
for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
if initChessList[y][x].value == value and \
initChessList[y + 1][x + 1].value == value and \
initChessList[y + 2][x + 2].value == value and \
initChessList[y + 3][x + 3].value == value and \
initChessList[y + 4][x + 4].value == value:
flag = True
break
pass
pass
pass
if flag:
resultFlag = value
pass
pass
# 加载素材
def main():
global resultFlag, initChessList
initChessSquare(27, 27) # 初始化棋牌
pygame.init() # 初始化游戏环境
# 创建游戏窗口
screen = pygame.display.set_mode((620,620), 0, 0) # 第一个参数是元组:窗口的长和宽
# 添加游戏标题
pygame.display.set_caption("五子棋小游戏")
# 图片的加载
background = pygame.image.load('images/bg.png')
blackStorn = pygame.image.load('images/storn_black.png')
whiteStorn = pygame.image.load('images/storn_white.png')
winStornW = pygame.image.load('images/result.jpg')
winStornB = pygame.image.load('images/result.jpg')
rect = blackStorn.get_rect()
while True:
screen.blit(background, (0, 0))
# 更新棋盘棋子
for temp in initChessList:
for point in temp:
if point.value == 1:
screen.blit(whiteStorn, (point.x - 18, point.y - 18))
pass
elif point.value == 2:
screen.blit(blackStorn, (point.x - 18, point.y - 18))
pass
pass
pass
# 如果已经判决出输赢
if resultFlag > 0:
initChessList = [] # 清空棋盘
initChessSquare(27, 27) # 重新初始化棋盘
if resultFlag == 1:
screen.blit(winStornW, (50,100))
else:
screen.blit(winStornB, (50,100))
pass
pygame.display.update()
if resultFlag >0:
time.sleep(3)
resultFlag = 0
pass
eventHandler()
pass
pass
if __name__ == "__main__":
main()
pass
来源:https://blog.csdn.net/mfansheng/article/details/100191003


猜你喜欢
- 自己搭建IP数据库占资源,而且更新不便,何不使用现成的IP查询呢?下面自己写了个获取IP物理地址的PHP代码(有一定的瑕疵,请高手不吝赐教)
- 本文研究的主要是Python编程通过pandas将数据分割成时间跨度相等的数据块的相关内容,具体如下。先上数据,有如下dataframe格式
- v1.0.0完成基础框架、初始功能背景:为了提高日常工作效率、学习界面工具开发,可以将一些常用的功能集成到一个小的测试工具中,供大家使用。一
- 获取计算机名# 获取计算机名,常用的方法有三种,但最常用的是第一种import osimport socket# method onenam
- 主要代码是参考:https://github.com/SoulDGXu/NLPVisualizationSystem/tree/master
- 需求分析:项目中根据测得的数据在界面上实时绘制运行环境:Python 3.7 + Matplotlib 3.0.2 + PyQt 5matp
- 什么是闭包?简单说,闭包就是根据不同的配置信息得到不同的结果。再来看看专业的解释:闭包(Closure)是词法闭包(Lexical Clos
- commands模块的适用commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到FUNCT
- CLI工程全局安装vue-clinpm install -g @vue/cli通过cli创建uni-app项目 vue creat
- 函数原型resample(self, rule, how=None, axis=0, fill_method=None, closed=No
- 1.如果每页都增加打印时间,又如何设置?打印时间的,你可以参考 for(var i=0;i<page.length;i++)
- 问题你想使用一个装饰器去包装函数,但是希望返回一个可调用的实例。 你需要让你的装饰器可以同时工作在类定义的内部和外部。解决方案为了将装饰器定
- 如下:data = pd.read_csv('20180201.txt',sep = '|',dtype =
- 查看隐藏参数SELECT x.ksppinm name,y.ksppstvl value,y.ksppstdf isdefault,deco
- 柱状图分类QBarSeries:竖向柱状图QPercentBarSeries:竖向百分比柱状图QStackedBarSeries:竖向堆叠柱
- 上节我们提到解决赋值中等号两边参数不一致的方法可以通过切片,但在Python3中我们可以利用特定的语法更加方便的处理这种情况,如下示例。当带
- SQL Server 2008“阻止保存要求重新创建表的更改”的错误的解决方案是本文我们主要要介绍的内容,情况是这样的:我们在用SQL Se
- 1.引入正则模块(Regular Expression)要使用python3中的RE则必须引入 re模块import re #引入正则表达式
- 今天微软正式发布上SQL Server 2016 SP1,根据以往的SP1定律,可以在生产环境上使用了。打了SP1的标准版将具有企业版几乎所
- Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes