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中的sort方法使用详解
Jul 25 Python
python基础教程之五种数据类型详解
Jan 12 Python
安装Python的教程-Windows
Jul 22 Python
Python线程创建和终止实例代码
Jan 20 Python
详解python持久化文件读写
Apr 06 Python
Python面向对象之继承和多态用法分析
Jun 08 Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
Jun 25 Python
解决Tensorflow 使用时cpu编译不支持警告的问题
Feb 03 Python
Python 高效编程技巧分享
Sep 10 Python
python从ftp获取文件并下载到本地
Dec 05 Python
Python turtle实现贪吃蛇游戏
Jun 18 Python
如何基于python实现单目三维重建详解
Jun 25 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+jQuery制作的列表分页的功能模块
2014/10/14 PHP
PHP的mysqli_rollback()函数讲解
2019/01/23 PHP
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
使用Jquery实现点击文字后变成文本框且可修改
2013/09/21 Javascript
JQuery的$命名冲突详细解析
2013/12/28 Javascript
jquery实现的树形目录实例
2015/06/26 Javascript
jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解
2016/01/19 Javascript
javascript中利用柯里化函数实现bind方法
2016/04/29 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
JS实现回到页面顶部动画效果的简单实例
2016/05/24 Javascript
jQuery解决$符号命名冲突
2016/06/18 Javascript
使用bootstrap实现多窗口和拖动效果
2016/09/22 Javascript
vuejs2.0运用原生js实现简单的拖拽元素功能示例
2017/02/24 Javascript
一个有意思的鼠标点击文字特效jquery代码
2017/09/23 jQuery
深入理解ES6 Promise 扩展always方法
2017/09/26 Javascript
微信小程序button组件使用详解
2018/01/31 Javascript
jQuery实现的点击标题文字切换字体效果示例【测试可用】
2018/04/26 jQuery
配置node服务器并且链接微信公众号接口配置步骤详解
2019/06/21 Javascript
JS实现拼图游戏
2021/01/29 Javascript
初步解析Python下的多进程编程
2015/04/28 Python
Python程序退出方式小结
2017/12/09 Python
Python爬虫中urllib库的进阶学习
2018/01/05 Python
Python生成器generator用法示例
2018/08/10 Python
python base64库给用户名或密码加密的流程
2020/01/02 Python
GUESS盖尔斯法国官网:美国时尚品牌
2016/09/23 全球购物
小学开学典礼主持词
2014/03/19 职场文书
《蝙蝠和雷达》教学反思
2014/04/23 职场文书
2014年办公室文员工作总结
2014/11/12 职场文书
学生保证书格式
2015/02/27 职场文书
2015年乡镇卫生院妇幼保健工作总结
2015/05/19 职场文书
男人帮观后感
2015/06/18 职场文书
2019年工作总结范文
2019/05/21 职场文书
python基础学习之生成器与文件系统知识总结
2021/05/25 Python
Python 类,对象,数据分类,函数参数传递详解
2021/09/25 Python
python 闭包函数详细介绍
2022/04/19 Python
Windows server 2012 R2 安装IIS服务器
2022/04/29 Servers