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创建系统目录的方法
Mar 11 Python
python使用xpath中遇到:到底是什么?
Jan 04 Python
python基于socket进行端口转发实现后门隐藏的示例
Jul 25 Python
python实现翻转棋游戏(othello)
Jul 29 Python
Python实现捕获异常发生的文件和具体行数
Apr 25 Python
Python监听剪切板实现方法代码实例
Nov 11 Python
Django用内置方法实现简单搜索功能的方法
Dec 18 Python
Python使用tkinter制作在线翻译软件
Feb 22 Python
教你用Python爬取英雄联盟皮肤原画
Jun 13 Python
python 常用的异步框架汇总整理
Jun 18 Python
基于Python编写简易版的天天跑酷游戏的示例代码
Mar 23 Python
Python+OpenCV实现图片中的圆形检测
Apr 07 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强制文件下载而非在浏览器打开的自定义函数分享
2014/05/08 PHP
ThinkPHP中Common/common.php文件常用函数功能分析
2016/05/20 PHP
PHP面相对象中的重载与重写
2017/02/13 PHP
Laravel学习教程之request validation的编写
2017/10/25 PHP
网页常用特效代码整理
2006/06/23 Javascript
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
javascript英文日期(有时间)选择器
2007/05/02 Javascript
浅析Js中的单引号与双引号问题
2013/11/06 Javascript
js捕获鼠标滚轮事件代码
2013/12/16 Javascript
Javascript设置对象的ReadOnly属性(示例代码)
2013/12/25 Javascript
jQuery过滤选择器详解
2015/01/13 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
jQuery+PHP实现动态数字展示特效
2015/03/14 Javascript
jQuery实现切换页面过渡动画效果
2015/10/29 Javascript
javascript实现html页面之间参数传递的四种方法实例分析
2015/12/15 Javascript
AngularJs表单验证实例详解
2016/05/30 Javascript
javascript 将共享属性迁移到原型中去的实现方法
2016/08/31 Javascript
JavaScript实现移动端轮播效果
2017/06/06 Javascript
Angular2+国际化方案(ngx-translate)的示例代码
2017/08/23 Javascript
Angularjs中ng-repeat的简单实例
2017/08/25 Javascript
vue返回上一页面时回到原先滚动的位置的方法
2018/12/20 Javascript
javascript判断一个变量是数组还是对象
2019/04/10 Javascript
vue在index.html中引入静态文件不生效问题及解决方法
2019/04/29 Javascript
微信小程序页面调用自定义组件内的事件详解
2019/09/12 Javascript
React Native登录之指纹登录篇的示例代码
2020/11/03 Javascript
老生常谈Python基础之字符编码
2017/06/14 Python
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
Tensorflow 自定义loss的情况下初始化部分变量方式
2020/01/06 Python
德国高尔夫商店:Golfshop.de
2019/06/22 全球购物
全球领先的中国制造商品在线批发平台:DHgate
2020/01/28 全球购物
珍惜时间演讲稿
2014/05/14 职场文书
2014年残疾人工作总结
2014/12/06 职场文书
部队2015年终工作总结
2015/04/02 职场文书
冬季作息时间调整通知
2015/04/24 职场文书
2015年人事科工作总结
2015/04/28 职场文书
2016教师廉洁教育心得体会
2016/01/13 职场文书