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中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
Python中的os.path路径模块中的操作方法总结
Jul 07 Python
Python编程实现蚁群算法详解
Nov 13 Python
python中的tcp示例详解
Dec 09 Python
使用selenium模拟登录解决滑块验证问题的实现
May 10 Python
pandas分区间,算频率的实例
Jul 04 Python
在django admin中添加自定义视图的例子
Jul 26 Python
Django中自定义查询对象的具体使用
Oct 13 Python
在matplotlib中改变figure的布局和大小实例
Apr 23 Python
Python flask框架实现查询数据库并显示数据
Jun 04 Python
Python 2.6.6升级到Python2.7.15的详细步骤
Dec 14 Python
详解Python中__new__方法的作用
Mar 31 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
ThinkPHP 防止表单重复提交的方法
2011/08/08 PHP
ThinkPHP查询语句与关联查询用法实例
2014/11/01 PHP
Joomla简单判断用户是否登录的方法
2016/05/04 PHP
PHP实现接收二进制流转换成图片的方法
2017/01/10 PHP
PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解
2019/06/03 PHP
PHP判断当前使用的是什么浏览器(推荐)
2019/10/27 PHP
做网页的一些技巧(续)
2007/02/01 Javascript
JavaScript让IE浏览器event对象符合W3C DOM标准
2009/11/24 Javascript
Jquery升级新版本后选择器的语法问题
2010/06/02 Javascript
DB.ASP 用Javascript写ASP很灵活很好用很easy
2011/07/31 Javascript
JavaScript Scoping and Hoisting 翻译
2012/07/03 Javascript
使用AngularJS中的SCE来防止XSS攻击的方法
2015/06/18 Javascript
JavaScript制作颜色反转小游戏
2016/09/25 Javascript
vue插件vue-resource的使用笔记(小结)
2017/08/04 Javascript
javascript闭包的使用之按钮切换功能
2018/08/30 Javascript
JS实现十分钟倒计时代码实例
2018/10/18 Javascript
微信小程序MUI导航栏透明渐变功能示例(通过改变opacity实现)
2019/01/24 Javascript
微信小程序:报错(in promise) MiniProgramError
2020/10/30 Javascript
[39:53]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第一场 11.19
2020/11/19 DOTA
9种python web 程序的部署方式小结
2014/06/30 Python
解决python中使用PYQT时中文乱码问题
2019/06/17 Python
Django发送邮件功能实例详解
2019/09/02 Python
Python中*args和**kwargs的区别详解
2019/09/17 Python
在Python 的线程中运行协程的方法
2020/02/24 Python
水果花束:Fruit Bouquets
2017/12/20 全球购物
英国珠宝钟表和家居礼品精品店:David Shuttle
2018/02/24 全球购物
ASOS比利时:英国线上零售商及自有品牌
2018/07/29 全球购物
SportsDirect.com马来西亚:英国第一体育零售商
2018/11/21 全球购物
大学生如何写自荐信
2014/01/08 职场文书
大学辅导员事迹材料
2014/02/05 职场文书
弘扬雷锋精神活动演讲稿
2014/03/04 职场文书
口才训练演讲稿范文
2014/09/16 职场文书
介绍信如何写
2015/01/31 职场文书
基石观后感
2015/06/12 职场文书
2015年思想品德教学工作总结
2015/07/22 职场文书
Go语言特点及基本数据类型使用详解
2022/03/21 Golang