项目简介

之前学python的时候 写了个游戏来练手 用的是 pygame 没有别的依赖 只用了一两百行的代码就实现了 整体来说功能并不算完整

项目背后的故事

这个项目是在大学的时候 偶然一个机遇交一个小朋友Python时 小朋友大概10多岁 正在打算上初一 小朋友分非常非常非常聪明!!! 当时给他讲东西 他很快就可以接受 立马就可以模仿出来 小朋友会的东西很多 其中一项我非常感兴趣哈哈 — 围棋 好像还是业余挺高的那种(不好意思 我不太懂段位) 好像是什么定段之后就可以打职业那种?对我来说是非常非常厉害的存在了 当时我还让他简单的交了交我如何下围棋 以及围棋的一些概念

除了五子棋之外 当时还写了 贪吃蛇、扫雷等等这些游戏 还给他讲了爬虫相关的东西 还有HTML啊CSS之类的 当时有一个游戏叫 “人类资源机器(HumanResource)” 游戏是一个通过简单编程 控制小人来实现目标的游戏 ↑就是这个游戏!当时我很惊讶 他过关速度非常快!搞得我压力都上来了哈哈

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

项目扩展思路

当然围棋其实也是一个道理 只是计算胜负、计算气的逻辑会不一样 可以改进一下 使用鼠标来落子会更有意思 大家可以参考一下 主项目在GitHub上 除了单机版以外还有一个局域网版

运行截图

安装依赖

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

参考文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: