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实现在matplotlib中两个坐标轴之间画一条直线光标的方法
May 20 Python
Python中查看文件名和文件路径
Mar 31 Python
Python:Scrapy框架中Item Pipeline组件使用详解
Dec 27 Python
python实现k-means聚类算法
Feb 23 Python
解决pycharm运行时interpreter为空的问题
Oct 29 Python
Python3 chardet模块查看编码格式的例子
Aug 14 Python
使用遗传算法求二元函数的最小值
Feb 11 Python
浅谈Python 命令行参数argparse写入图片路径操作
Jul 12 Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 Python
详解Python中的Lock和Rlock
Jan 26 Python
在pycharm中无法import所安装的库解决方案
May 31 Python
Python借助with语句实现代码段只执行有限次
Mar 23 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 Smarty生成EXCEL文档的代码
2008/08/23 PHP
PHP将session信息存储到数据库的类实例
2015/03/04 PHP
正确的PHP匹配UTF-8中文的正则表达式
2015/05/13 PHP
基于php实现的php代码加密解密类完整实例
2016/10/12 PHP
js 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome
2009/11/26 Javascript
Array.prototype 的泛型应用分析
2010/04/30 Javascript
让jQuery与其他JavaScript库并存避免冲突的方法
2013/12/23 Javascript
JS下载文件|无刷新下载文件示例代码
2014/04/17 Javascript
js获取滚动距离的方法
2015/05/30 Javascript
深入探究angular2 UI组件之primeNG用法
2017/07/26 Javascript
简述jQuery Easyui一些用法
2017/08/01 jQuery
Vue.js做select下拉列表的实例(ul-li标签仿select标签)
2018/03/02 Javascript
vue2.0 实现页面导航提示引导的方法
2018/03/13 Javascript
详解关于Vue2.0路由开启keep-alive时需要注意的地方
2018/09/18 Javascript
vue视图不更新情况详解
2019/05/16 Javascript
vue+elementUI 复杂表单的验证、数据提交方案问题
2019/06/24 Javascript
在react项目中使用antd的form组件,动态设置input框的值
2020/10/24 Javascript
[37:37]DAC2018 4.4 淘汰赛 Optic vs Mineski 第二场
2018/04/05 DOTA
pymongo实现多结果进行多列排序的方法
2015/05/16 Python
python的中异常处理机制
2018/08/30 Python
python中的句柄操作的方法示例
2019/06/20 Python
PyQt5创建一个新窗口的实例
2019/06/20 Python
Django自定义用户登录认证示例代码
2019/06/30 Python
python zip()函数使用方法解析
2019/10/31 Python
Python + Requests + Unittest接口自动化测试实例分析
2019/12/12 Python
Windows下Anaconda和PyCharm的安装与使用详解
2020/04/23 Python
python类共享变量操作
2020/09/03 Python
详解Django关于StreamingHttpResponse与FileResponse文件下载的最优方法
2021/01/07 Python
意大利和国际奢侈品牌购物网站:Suitnegozi.com
2021/01/15 全球购物
工会工作先进事迹
2014/08/18 职场文书
学校政风行风评议工作总结
2014/10/21 职场文书
先进班集体事迹材料
2014/12/25 职场文书
pytorch训练神经网络爆内存的解决方案
2021/05/22 Python
SQL实现LeetCode(180.连续的数字)
2021/08/04 MySQL
python实现简单的三子棋游戏
2022/04/28 Python
uniapp引入支付宝原生扫码插件步骤详解
2022/07/23 Javascript