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 相关文章推荐
仅用50行代码实现一个Python编写的计算器的教程
Apr 17 Python
Python中设置变量访问权限的方法
Apr 27 Python
浅析Python基础-流程控制
Mar 18 Python
python thrift搭建服务端和客户端测试程序
Jan 17 Python
PyCharm导入python项目并配置虚拟环境的教程详解
Oct 13 Python
关于numpy.where()函数 返回值的解释
Dec 06 Python
Python3交互式shell ipython3安装及使用详解
Jul 11 Python
pycharm 关闭search everywhere的解决操作
Jan 15 Python
python爬虫selenium模块详解
Mar 30 Python
PyCharm 安装与使用配置教程(windows,mac通用)
May 12 Python
Python+tkinter实现高清图片保存
Mar 13 Python
Python matplotlib绘制条形统计图 处理多个实验多组观测值
Apr 21 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版阿里大于(阿里大鱼)短信发送实例详解
2016/11/30 PHP
Thinkphp事务操作实例(推荐)
2017/04/01 PHP
Laravel 解决composer相关操作提示php相关异常的问题
2019/10/23 PHP
javascript instanceof 与typeof使用说明
2010/01/11 Javascript
IE 当eval遇上function的处理
2011/08/09 Javascript
JS实现图片放大镜效果的方法
2015/02/27 Javascript
js+CSS实现模拟华丽的select控件下拉菜单效果
2015/09/01 Javascript
跟我学习javascript的循环
2015/11/18 Javascript
EasyUI在表单提交之前进行验证的实例代码
2016/06/24 Javascript
Vue.js自定义指令的用法与实例解析
2017/01/18 Javascript
AngularJS表单验证功能
2017/10/19 Javascript
vue登录注册及token验证实现代码
2017/12/14 Javascript
vue项目首屏打开速度慢的解决方法
2019/03/31 Javascript
vue-cli3使用 DllPlugin 实现预编译提升构建速度
2019/04/24 Javascript
vue 授权获取微信openId操作
2020/11/13 Javascript
vue 在单页面应用里使用二级套嵌路由
2020/12/19 Vue.js
[01:04:06]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第一局
2016/02/26 DOTA
[01:29:31]VP VS VG Supermajor小组赛胜者组第二轮 BO3第一场 6.2
2018/06/03 DOTA
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
python3多线程知识点总结
2019/09/26 Python
在django admin详情表单显示中添加自定义控件的实现
2020/03/11 Python
python利用faker库批量生成测试数据
2020/10/15 Python
浅谈css3中的渐进增强和优雅降级
2017/12/01 HTML / CSS
施华洛世奇加拿大官网:SWAROVSKI加拿大
2018/06/03 全球购物
Dower & Hall官网:英国小众轻奢珠宝品牌
2019/01/31 全球购物
syb养殖创业计划书
2014/01/09 职场文书
物业工作计划书
2014/01/10 职场文书
工作过失检讨书
2014/02/23 职场文书
工程负责人任命书
2014/06/06 职场文书
幼儿教师师德师风演讲稿
2014/08/22 职场文书
学校领导班子对照检查材料
2014/08/28 职场文书
董事长秘书岗位职责
2015/02/13 职场文书
2016年“抗战胜利纪念日”71周年校园广播稿
2015/12/18 职场文书
详解如何修改nginx的默认端口
2021/03/31 Servers
jQuery ajax - getScript() 方法和getJSON方法
2021/05/14 jQuery
使用Pytorch实现two-head(多输出)模型的操作
2021/05/28 Python