Python+Pygame实现彩色五子棋游戏

作者:武子康 时间:2021-03-29 23:47:34 

项目简介

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

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

项目背后的故事

这个项目是在大学的时候

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

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

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

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

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

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

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

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

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

Python+Pygame实现彩色五子棋游戏

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

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

Python+Pygame实现彩色五子棋游戏

项目扩展思路

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

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

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

运行截图

Python+Pygame实现彩色五子棋游戏

Python+Pygame实现彩色五子棋游戏

安装依赖

pip install pygame

或者

pip3 install pygame

运行游戏

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

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

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!")

来源:https://blog.csdn.net/w776341482/article/details/128930013

标签:Python,Pygame,五子棋,游戏
0
投稿

猜你喜欢

  • 基于Tensorflow的MNIST手写数字识别分类

    2023-12-01 11:35:18
  • TensorFlow卷积神经网络MNIST数据集实现示例

    2023-04-20 18:33:12
  • python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警

    2022-01-13 09:48:25
  • ASP下标越界错误的解决方法

    2008-10-19 17:39:00
  • 如何配置关联Python 解释器 Anaconda的教程(图解)

    2021-06-13 15:27:30
  • ASP中数据库调用中常见错误的现象和解决

    2007-09-20 13:24:00
  • Python3.10的一些新特性原理分析

    2023-06-17 06:35:10
  • python实现三子棋游戏

    2021-11-20 04:10:37
  • ASP.NET(C#)读取Excel的文件内容

    2023-07-10 22:38:35
  • 两个JS之间的函数互相调用方式

    2024-04-10 10:39:45
  • mysql使用source 命令乱码问题解决方法

    2024-01-13 13:11:16
  • Python编程中运用闭包时所需要注意的一些地方

    2021-10-27 06:07:21
  • Pandas.DataFrame时间序列数据处理的实现

    2022-09-20 08:43:41
  • Python 如何批量更新已安装的库

    2023-06-05 12:15:40
  • 利用Python内置库实现创建命令行应用程序

    2022-04-26 03:39:19
  • 详解Docker创建Mysql容器并通过命令行连接到容器

    2024-01-24 22:25:18
  • JavaScript实现鼠标经过表格某行时此行变色

    2024-04-16 08:51:18
  • 使用python将图片改为灰度图或黑白图

    2023-04-17 12:28:52
  • python实现按键精灵找色点击功能教程,使用pywin32和Pillow库

    2023-11-08 18:30:34
  • Python数据结构与算法之图的广度优先与深度优先搜索算法示例

    2022-05-05 07:34:53
  • asp之家 网络编程 m.aspxhome.com