python 单机五子棋对战游戏


Posted in Python onApril 28, 2022

 引入pygame模块

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

python 单机五子棋对战游戏

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


Tags in this post...

Python 相关文章推荐
使用PyCharm配合部署Python的Django框架的配置纪实
Nov 19 Python
Python实现带百分比的进度条
Jun 28 Python
Python探索之静态方法和类方法的区别详解
Oct 27 Python
Python二叉树的定义及常用遍历算法分析
Nov 24 Python
python3+PyQt5使用数据库表视图
Apr 24 Python
numpy数组广播的机制
Jul 12 Python
在python中将list分段并保存为array类型的方法
Jul 15 Python
python正则-re的用法详解
Jul 28 Python
Python中正反斜杠(‘/’和‘\’)的意义与用法
Aug 12 Python
Python如何实现爬取B站视频
May 20 Python
20行Python代码实现一款永久免费PDF编辑工具的实现
Aug 27 Python
python实现简单倒计时功能
Apr 21 Python
python井字棋游戏实现人机对战
Apr 28 #Python
Python开发五子棋小游戏
Python简易开发之制作计算器
Apr 28 #Python
Python实现对齐打印 format函数的用法
Apr 28 #Python
python实现简单的三子棋游戏
Apr 28 #Python
Python内置类型集合set和frozenset的使用详解
使用Python获取字典键对应值的方法
Apr 26 #Python
You might like
全国FM电台频率大全 - 4 山西省
2020/03/11 无线电
php批量缩放图片的代码[ini参数控制]
2011/02/11 PHP
神盾加密解密教程(三)PHP 神盾解密工具
2014/06/08 PHP
PHP图片处理之使用imagecopyresampled函数实现图片缩放例子
2014/11/19 PHP
PHP内核探索:哈希表碰撞攻击原理
2015/07/31 PHP
PHP验证码生成原理和实现
2016/01/24 PHP
PHP双向链表定义与用法示例
2018/01/31 PHP
PHP使用观察者模式处理异常信息的方法详解
2019/09/24 PHP
豆瓣网的jquery代码实例
2008/06/15 Javascript
js操作textarea方法集合封装(兼容IE,firefox)
2011/02/22 Javascript
javascript日期处理函数,性能优化批处理
2015/09/06 Javascript
jquery实现邮箱自动填充提示功能
2015/11/17 Javascript
JavaScript实现弹出模态窗体并接受传值的方法
2016/02/12 Javascript
JS中创建函数的三种方式及区别
2016/03/13 Javascript
完美实现js焦点轮播效果(一)
2017/03/07 Javascript
jQuery实现文章图片弹出放大效果
2017/04/06 jQuery
Nuxt 项目性能优化调研分析
2020/11/07 Javascript
[01:20]DOTA2 2017国际邀请赛冠军之路无止竞
2017/06/19 DOTA
[01:04:14]VP vs TNC 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
python 利用栈和队列模拟递归的过程
2018/05/29 Python
python排序函数sort()与sorted()的区别
2018/09/18 Python
详解Python3中ceil()函数用法
2019/02/19 Python
Python内置加密模块用法解析
2019/11/25 Python
在Python中使用K-Means聚类和PCA主成分分析进行图像压缩
2020/04/10 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
tensorflow下的图片标准化函数per_image_standardization用法
2020/06/30 Python
图解CSS3制作圆环形进度条的实例教程
2016/05/26 HTML / CSS
生产总经理岗位职责
2013/12/19 职场文书
关工委先进个人事迹材料
2014/05/23 职场文书
2014年物流工作总结
2014/11/25 职场文书
2014年新农村建设工作总结
2014/12/01 职场文书
还款承诺书范本
2015/01/20 职场文书
2015年财务部年度工作总结
2015/05/19 职场文书
浅谈Golang 嵌套 interface 的赋值问题
2021/04/29 Golang
利用python Pandas实现批量拆分Excel与合并Excel
2021/05/23 Python
中国古风插画师排行榜:夏达第一,第三是阴阳师姑获鸟皮肤创作者
2022/03/18 国漫