Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版

简介: Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版

项目简介

之前学python的时候 写了个游戏来练手 用的是 pygame 没有别的依赖

只用了一两百行的代码就实现了 整体来说功能并不算完整


项目背后的故事

这个项目是在大学的时候

偶然一个机遇交一个小朋友Python时 小朋友大概10多岁 正在打算上初一

小朋友分非常非常非常聪明!!!

当时给他讲东西 他很快就可以接受 立马就可以模仿出来

小朋友会的东西很多 其中一项我非常感兴趣哈哈 — 围棋 好像还是业余挺高的那种(不好意思 我不太懂段位)

好像是什么定段之后就可以打职业那种?对我来说是非常非常厉害的存在了

当时我还让他简单的交了交我如何下围棋 以及围棋的一些概念


除了五子棋之外 当时还写了 贪吃蛇、扫雷等等这些游戏

还给他讲了爬虫相关的东西 还有HTML啊CSS之类的

当时有一个游戏叫 “人类资源机器(HumanResource)” 游戏是一个通过简单编程 控制小人来实现目标的游戏

↑就是这个游戏!当时我很惊讶 他过关速度非常快!搞得我压力都上来了哈哈

当时还准备了几页的 “课本” 方便小朋友以后能够回看

项目扩展思路

当然围棋其实也是一个道理 只是计算胜负、计算气的逻辑会不一样

可以改进一下 使用鼠标来落子会更有意思

大家可以参考一下 主项目在GitHub上 除了单机版以外还有一个局域网版

运行截图

安装依赖

pip install pygame
或者
pip3 install pygame
• 1
• 2
• 3

运行游戏

将游戏代码保存后 直接运行即可

上下左右移动光标 空格落子

import pygame

# 初始化
pygame.init()
# 设置窗口标题
screencaption=pygame.display.set_caption('Gobang')
# 设置大小
screen=pygame.display.set_mode([350,285])

# 初始化字体
myfont=pygame.font.Font(None,30)
textImage=myfont.render("Hello Pygame",True,[255,255,255])
screen.blit(textImage,(100,100))

# 棋子状态0为空 1为白色 2为黑色
status_list = {}
for i in range(0, 15*18):
    status_list[i] = 0  
#print(status_list)

clock = pygame.time.Clock()

# 0 是白棋走 1是黑棋走
flag = 0
# 将要绘制的棋子的位置
movex = 1
movey = 1
while True:
    clock.tick(30)
    
    # 绘制棋盘
    screen.fill([255,255,255])
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[0,i*20],[280,i*20],2)
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[i*20,0],[i*20,280],2)
    
    # 绘制棋子
    for x in range(0, 15):
        for y in range(0, 15):
            if status_list[x*15 + y] == 1:
                pygame.draw.circle(screen,[255,0,0],[ 2 + y * 20,2 + x*20],10)
            elif status_list[x*15 + y] == 2:
                pygame.draw.circle(screen,[0,0,0],[ 2 + y * 20, 2 + x*20],10)
            # 判断是否获胜
            # X轴的判定
            if y < 11:
                # 白棋获胜
                if status_list[x*15 + y] == 1 and status_list[x*15 + y + 1] == 1 and status_list[x*15 + y + 2] == 1 and status_list[x*15 + y + 3] == 1 and status_list[x*15 + y + 4] == 1:
                    print("白棋胜利")
                    # break
                    
                # 黑棋获胜
                if status_list[x*15 + y] == 2 and status_list[x*15 + y + 1] == 2 and status_list[x*15 + y + 2] == 2 and status_list[x*15 + y + 3] == 2 and status_list[x*15 + y + 4] == 2:
                    print("黑棋胜利")
                    # break

            # 判断是否获胜
            # Y轴的判定
            if x < 11:
                if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + y] == 1 and status_list[(x+2)*15 + y] == 1 and status_list[(x+3)*15 + y] == 1 and status_list[(x+4)*15 + y] == 1:
                    print("白棋胜利")
                    # break
                    
                if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + y] == 2 and status_list[(x+2)*15 + y] == 2 and status_list[(x+3)*15 + y] == 2 and status_list[(x+4)*15 + y] == 2:
                    print("黑棋胜利")
                    # break

            # 判断是否获胜
            # 斜着判断 正对角线
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y+1)] == 1 and status_list[(x+2)*15 + (y+2)] == 1 and status_list[(x+3)*15 + (y+3)] == 1 and status_list[(x+4)*15 + (y+4)] == 1:
                print("白棋胜利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y+1)] == 2 and status_list[(x+2)*15 + (y+2)] == 2 and status_list[(x+3)*15 + (y+3)] == 2 and status_list[(x+4)*15 + (y+4)] == 2:
                print("黑棋胜利")
                # break
            # 判断是否获胜
            # 斜着判断 反对角线
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y-1)] == 1 and status_list[(x+2)*15 + (y-2)] == 1 and status_list[(x+3)*15 + (y-3)] == 1 and status_list[(x+4)*15 + (y-4)] == 1:
                print("白棋胜利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y-1)] == 2 and status_list[(x+2)*15 + (y-2)] == 2 and status_list[(x+3)*15 + (y-3)] == 2 and status_list[(x+4)*15 + (y-4)] == 2:
                print("黑棋胜利")
                # break
    # 绘制落棋位置
    pygame.draw.circle(screen,[0,0,0],[ 2 + movex*20, 2 + movey*20],10,3)
    
    # 绘制文字 显示到谁落棋子
    if flag == 0: 
        textImage=myfont.render("White",True,[255,0,0])
    else:
        textImage=myfont.render("Black",True,[0,0,255])
    screen.blit(textImage,(290,10))
  
    # 判断事件
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
        # 键盘事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if movex > 0:
                    movex = movex - 1
            if event.key == pygame.K_RIGHT:
                if movex < 14:
                    movex = movex + 1
            if event.key == pygame.K_UP:
                if movey > 0:
                    movey = movey - 1
            if event.key == pygame.K_DOWN:
                if movey < 14:
                    movey = movey + 1
            if event.key == pygame.K_SPACE:
                if flag == 0:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 1
                        flag = 1
                elif flag == 1:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 2
                        flag = 0

    # 刷新页面
    pygame.display.flip()
print("Done!")


目录
相关文章
|
2月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
308 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
2月前
|
机器学习/深度学习 数据采集 算法
基于mediapipe深度学习的运动人体姿态提取系统python源码
本内容介绍了基于Mediapipe的人体姿态提取算法。包含算法运行效果图、软件版本说明、核心代码及详细理论解析。Mediapipe通过预训练模型检测人体关键点,并利用部分亲和场(PAFs)构建姿态骨架,具有模块化架构,支持高效灵活的数据处理流程。
|
2月前
|
小程序 PHP 图形学
热门小游戏源码(Python+PHP)下载-微信小程序游戏源码Unity发实战指南​
本文详解如何结合Python、PHP与Unity开发并部署小游戏至微信小程序。涵盖技术选型、Pygame实战、PHP后端对接、Unity转换适配及性能优化,提供从原型到发布的完整指南,助力开发者快速上手并发布游戏。
|
3月前
|
并行计算 算法 Java
Python3解释器深度解析与实战教程:从源码到性能优化的全路径探索
Python解释器不止CPython,还包括PyPy、MicroPython、GraalVM等,各具特色,适用于不同场景。本文深入解析Python解释器的工作原理、内存管理机制、GIL限制及其优化策略,并介绍性能调优工具链及未来发展方向,助力开发者提升Python应用性能。
206 0
|
存储 人工智能 算法
使用 Python 和 Pygame 制作游戏:第九章到第十章
使用 Python 和 Pygame 制作游戏:第九章到第十章
313 0
使用 Python 和 Pygame 制作游戏:第九章到第十章
|
Python
Python使用pygame播放MP3
Python使用pygame播放MP3
183 0
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
2528 1
|
12月前
|
JSON 开发工具 git
基于Python和pygame的植物大战僵尸游戏设计源码
本项目是基于Python和pygame开发的植物大战僵尸游戏,包含125个文件,如PNG图像、Python源码等,提供丰富的游戏开发学习素材。游戏设计源码可从提供的链接下载。关键词:Python游戏开发、pygame、植物大战僵尸、源码分享。
|
开发框架 Python
Python的`pygame`库用于2D游戏开发,涵盖图形、音频和输入处理。
【6月更文挑战第21天】Python的`pygame`库用于2D游戏开发,涵盖图形、音频和输入处理。要开始,先通过`pip install pygame`安装。基本流程包括:初始化窗口、处理事件循环、添加游戏元素(如玩家和敌人)、响应用户输入、更新游戏状态及结束条件。随着项目发展,可逐步增加复杂性。
448 1

推荐镜像

更多