python pygame 开发五子棋双人对弈


Posted in Python onMay 02, 2022

本文实例为大家分享了python pygame 开发五子棋双人对弈的具体代码,供大家参考,具体内容如下

我用的是pygame模块来制作窗口

代码如下:

# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
 
initChessList = []
initRole = 2 # 代表白子下 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():
    initRole = 2  # 代表白子下 2:代表当前是黑子下
    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/white.png')
    winStornB = pygame.image.load('images/black.png')
    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

插图:放在同一目录下的images文件夹里

bg.png

python pygame 开发五子棋双人对弈

storn_white.png

python pygame 开发五子棋双人对弈

storn_black.png

python pygame 开发五子棋双人对弈

white.png

python pygame 开发五子棋双人对弈

black.png

python pygame 开发五子棋双人对弈

以上就是本文的全部内容,希望对大家的学习有所帮助。


Tags in this post...

Python 相关文章推荐
Python 探针的实现原理
Apr 23 Python
Python根据指定日期计算后n天,前n天是哪一天的方法
May 29 Python
python中利用h5py模块读取h5文件中的主键方法
Jun 05 Python
python中退出多层循环的方法
Nov 27 Python
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
Feb 17 Python
python实现在函数中修改变量值的方法
Jul 16 Python
使用python实现kNN分类算法
Oct 16 Python
python实现矩阵和array数组之间的转换
Nov 29 Python
python实现将中文日期转换为数字日期
Jul 14 Python
基于logstash实现日志文件同步elasticsearch
Aug 06 Python
Python 随机按键模拟2小时
Dec 30 Python
详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案
Jan 29 Python
Python开发简易五子棋小游戏
May 02 #Python
Python开发五子棋小游戏
python获取带有返回值的多线程
May 02 #Python
总结三种用 Python 作为小程序后端的方式
Python如何用re模块实现简易tokenizer
May 02 #Python
Python实现简单得递归下降Parser
使用Python开发贪吃蛇游戏 SnakeGame
Apr 30 #Python
You might like
dede3.1分页文字采集过滤规则详说(图文教程)
2007/04/03 PHP
laravel 使用auth编写登录的方法
2019/09/30 PHP
thinkPHP框架乐观锁和悲观锁实例分析
2019/10/30 PHP
PHP中SESSION过期设置
2021/03/09 PHP
easyui Draggable组件实现拖动效果
2015/08/19 Javascript
原生javascript实现解析XML文档与字符串
2016/03/01 Javascript
JS实现的表格行上下移动操作示例
2016/08/03 Javascript
js实现StringBuffer的简单实例
2016/09/02 Javascript
js放大镜放大购物图片效果
2017/01/18 Javascript
JavaScript中object和Object的区别(详解)
2017/02/27 Javascript
node.js中grunt和gulp的区别详解
2017/07/17 Javascript
从vue基础开始创建一个简单的增删改查的实例代码(推荐)
2018/02/11 Javascript
mpvue跳转页面及注意事项
2018/08/03 Javascript
JavaScript页面倒计时功能完整示例
2019/05/15 Javascript
vue中的使用token的方法示例
2020/03/10 Javascript
Vue如何将页面导出成PDF文件
2020/08/17 Javascript
js屏蔽F12审查元素,禁止修改页面代码等实现代码
2020/10/02 Javascript
Python新手实现2048小游戏
2015/03/31 Python
利用python代码写的12306订票代码
2015/12/20 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
2016/03/30 Python
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
2016/05/27 Python
python中将函数赋值给变量时需要注意的一些问题
2017/08/18 Python
Python request设置HTTPS代理代码解析
2018/02/12 Python
python实现简单图片物体标注工具
2019/03/18 Python
python接口自动化(十六)--参数关联接口后传(详解)
2019/04/16 Python
解决Python3下map函数的显示问题
2019/12/04 Python
在tensorflow中设置保存checkpoint的最大数量实例
2020/01/21 Python
anaconda3安装及jupyter环境配置全教程
2020/08/24 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
2021/03/03 Python
香港通票:Hong Kong Pass
2019/02/26 全球购物
毕业生个人求职自荐信
2014/02/26 职场文书
庆祝教师节标语
2014/10/09 职场文书
《狼王梦》读后感:可怜天下父母心
2019/11/01 职场文书
background-position百分比原理详解
2021/05/08 HTML / CSS
关于SpringBoot 使用 Redis 分布式锁解决并发问题
2021/11/17 Redis
win11怎么用快捷键锁屏? windows11锁屏的几种方法
2021/11/21 数码科技