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 相关文章推荐
详解Python中__str__和__repr__方法的区别
Apr 17 Python
Python利用flask sqlalchemy实现分页效果
Aug 02 Python
python实现机器人行走效果
Jan 29 Python
解决Python 中英文混输格式对齐的问题
Jul 16 Python
详解python多线程之间的同步(一)
Apr 03 Python
python安装virtualenv虚拟环境步骤图文详解
Sep 18 Python
Tensorflow 多线程与多进程数据加载实例
Feb 05 Python
TensorFlow固化模型的实现操作
May 26 Python
PythonPC客户端自动化实现原理(pywinauto)
May 28 Python
如何使用PyCharm引入需要使用的包的方法
Sep 22 Python
Python+OpenCV检测灯光亮点的实现方法
Nov 02 Python
python制作抽奖程序代码详解
Jan 15 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
php面向对象全攻略 (四)构造方法与析构方法
2009/09/30 PHP
php中的比较运算符详解
2013/10/28 PHP
PHP变量的定义、可变变量、变量引用、销毁方法
2013/12/20 PHP
实例分析10个PHP常见安全问题
2019/07/09 PHP
使用laravel的migrate创建数据表的方法
2019/09/30 PHP
JavaScript监测ActiveX控件是否已经安装过的代码
2008/09/02 Javascript
jQuery关于导航条背景切换效果实现示例
2013/09/04 Javascript
javascript设计模式之解释器模式详解
2014/06/05 Javascript
javascript从image转换为base64位编码的String
2014/07/29 Javascript
javascript在当前窗口关闭前检测窗口是否关闭
2014/09/29 Javascript
PHP+jQuery+Ajax实现多图片上传效果
2015/03/14 Javascript
bootstrap timepicker在angular中取值并转化为时间戳
2017/06/13 Javascript
vue之将echart封装为组件
2018/06/02 Javascript
简述JS浏览器的三种弹窗
2018/07/15 Javascript
小程序封装路由文件和路由方法(5种全解析)
2019/05/26 Javascript
no-vnc和node.js实现web远程桌面的完整步骤
2019/08/11 Javascript
JavaScript实现指定数量的并发限制的示例代码
2020/03/10 Javascript
[06:48]DOTA2-DPC中国联赛2月26日Recap集锦
2021/03/11 DOTA
Python基于动态规划算法计算单词距离
2015/07/25 Python
django model去掉unique_together报错的解决方案
2016/10/18 Python
Pandas中DataFrame的分组/分割/合并的实现
2019/07/16 Python
Selenium向iframe富文本框输入内容过程图解
2020/04/10 Python
如何通过Python3和ssl实现加密通信功能
2020/05/09 Python
python通过cython加密代码
2020/12/11 Python
中国制造网:Made-in-China.com
2019/10/25 全球购物
护理个人求职信范文
2014/01/08 职场文书
计算机数据库专业职业生涯规划书
2014/02/08 职场文书
出售房屋委托书范本
2014/09/24 职场文书
交通局领导班子群众路线教育实践活动对照检查材料思想汇报
2014/10/09 职场文书
秋季运动会开幕词
2015/01/28 职场文书
公司经营目标责任书
2015/01/29 职场文书
2016继续教育研修日志
2015/11/13 职场文书
Go语言操作数据库及其常规操作的示例代码
2021/04/21 Golang
TypeScript中条件类型精读与实践记录
2021/10/05 Javascript
JavaScript实例 ODO List分析
2022/01/22 Javascript
mysql聚集索引、辅助索引、覆盖索引、联合索引的使用
2022/02/12 MySQL