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调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件
Aug 15 Python
Python兔子毒药问题实例分析
Mar 05 Python
网站渗透常用Python小脚本查询同ip网站
May 08 Python
深入学习Python中的上下文管理器与else块
Aug 27 Python
Python2/3中urllib库的一些常见用法
Dec 19 Python
浅谈python正则的常用方法 覆盖范围70%以上
Mar 14 Python
Django中如何防范CSRF跨站点请求伪造攻击的实现
Apr 28 Python
初次部署django+gunicorn+nginx的方法步骤
Sep 11 Python
在pycharm中为项目导入anacodna环境的操作方法
Feb 12 Python
Django用户登录与注册系统的实现示例
Jun 03 Python
用python实现名片管理系统
Jun 18 Python
python 中的@运算符使用
May 26 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
PHP函数之日期时间函数date()使用详解
2013/09/09 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
jQuery中fadeIn、fadeOut、fadeTo的使用方法(图片显示与隐藏)
2013/05/08 Javascript
js给页面加style无效果的解决方法
2014/01/20 Javascript
jQuery照片伸缩效果不影响其他元素的布局
2014/05/09 Javascript
JS定义网页表单提交(submit)的方法
2015/03/20 Javascript
学习JavaScript设计模式之单例模式
2016/01/19 Javascript
jQuery实现点击按钮文字变成input框点击保存变成文字
2016/05/09 Javascript
Javascript中内建函数reduce的应用详解
2016/10/20 Javascript
JavaScript将base64图片转换成formData并通过AJAX提交的实现方法
2016/10/24 Javascript
js返回顶部实例分享
2016/12/21 Javascript
vue中实现滚动加载更多的示例
2017/11/08 Javascript
简单了解Vue computed属性及watch区别
2020/07/10 Javascript
element中table高度自适应的实现
2020/10/21 Javascript
[48:37]EG vs OG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
Python第三方库的安装方法总结
2016/06/06 Python
Python 结巴分词实现关键词抽取分析
2017/10/21 Python
详解django三种文件下载方式
2018/04/06 Python
Python代码块批量添加Tab缩进的方法
2018/06/25 Python
pandas删除指定行详解
2019/04/04 Python
详解Python3除法之真除法、截断除法和下取整对比
2019/05/23 Python
Numpy与Pytorch 矩阵操作方式
2019/12/27 Python
Python 实现一行输入多个数字(用空格隔开)
2020/04/29 Python
Python使用pyexecjs代码案例解析
2020/07/13 Python
html5各种页面切换效果和模态对话框用法总结
2014/12/15 HTML / CSS
英国著名的药妆网站:Escentual
2016/07/29 全球购物
加拿大领先的冒险和户外零售商:Atmosphere
2017/12/19 全球购物
微软中国官方旗舰店:销售Surface、Xbox One、笔记本电脑、Office
2018/07/23 全球购物
Carolina工作鞋官网:Carolina Footwear
2019/03/14 全球购物
营销与策划专业求职信
2014/06/20 职场文书
受伤赔偿协议书
2014/09/24 职场文书
2016年员工政治思想表现评语
2015/12/02 职场文书
24句精辟的现实社会语录,句句扎心,道尽人性
2019/08/29 职场文书
CSS3 制作的彩虹按钮样式
2021/04/11 HTML / CSS
python基于tkinter制作m3u8视频下载工具
2021/04/24 Python
mysql在项目中怎么选事务隔离级别
2021/05/25 MySQL