这篇文章旨在介绍一个双人的五子棋程序。再次重申,本人不擅长对代码的可读性进行优化,所以可能有些杂乱(在所难免)。

先瞅一眼效果图:

请注意,这个棋子……是这么圆润立体!本程序不需任何素材图片,完全用代码绘制所需的图像,因此这样立体的棋子十分难能可贵。那么,这究竟是如何做到的呢?别急,听我慢慢道来。

首先,一个好的程序必须配有高端大气的文字。对于博大精深的中文,gbk或utf-8的编码声明自然是非常必要的。于是,就有了第一行代码:

#coding:utf-8

然后,当然是模块的导入。本次所需的模块不多,只有sys、pygame和random。其中pygame需要用pip工具进行安装。

import sys
import pygame
import random

接下来,我们定义一个函数:do(),里面输入我们所需要的代码。至于为何要定义函数,这是因为在游戏结束后需要重新运行该程序,因而不可避免地要将全部的程序代码输入一个函数中,并调用这个函数。

def do():

然后,就是最重磅的棋子绘制函数,我们先看黑棋:

    def black(x, y):
a = 30
b = 30
c = 30
d = 8
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], 111 / d)
a += 0.3
b += 0.3
c += 0.3
d += 0.2
pygame.display.update()

这里的x和y是绘制黑棋的位置,暂且先不管。可以看到,这一个圆润的棋子是有50个同心圆组成。这些同心圆的颜色逐个变浅,相邻两个圆的颜色差值不变。因此,我们只需要使圆的直径(或半径)呈曲线变化,就可以使绘制的棋子边缘非常圆润。作为一个初二的学生,我立马想到了反比例函数。因此,“d=8”“111/d”和“d+=0.2”实际上是使同心圆的半径随循环变量的变化呈一个偏移的反比例函数,这样就可以营造一种圆润的视感。

同理,白棋的绘制也是遵循类似的方式。在此不在赘述,只给出代码:

    def white(x, y):
a = 200
b = 200
c = 200
d = 8
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], 111 / d)
a += 0.3
b += 0.3
c += 0.3
d += 0.2
pygame.display.update()

接下来,是冗长无味的棋盘绘制:

    pygame.init()
screen = pygame.display.set_mode((615, 615))
pygame.display.set_caption('五子棋')
screen.fill("#DD954F")
a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)
a.fill(color='#121010')
b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)
b.fill(color="#DD954F")
c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)
c.fill(color='#121010')
d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)
d.fill(color="#DD954F")
e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)
e.fill(color="#DD954F")
screen.blit(a, (6.5, 6.5))
screen.blit(b, (15, 15))
screen.blit(c, (18, 18))
for j in range(18):
for i in range(18):
screen.blit(e, (20 + 32 * i, 20 + 32 * j))
alist = []
for j in range(19):
alistone = []
for i in range(19):
alistone.append(0)
alist.append(alistone)
pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
pygame.display.flip()

可以看到,我们先画了一个花哨的边框,然后画上其中的格子,顺便定义了一个被0填满的19*19的二维列表(在此处似乎很冗余,但到后面,你会发现它异常有用!)。最后,九个平平无奇的点被画上了棋盘。

至此,我们的五子棋初见雏形。但是,要使用它进行对弈,这还远远不够。

    wb = "black"
font1 = pygame.font.SysFont('stxingkai', 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
x = round((x - 19.5) / 32)
y = round((y - 19.5) / 32)
if x < 0:
x = 0
if x > 18:
x = 18
if y < 0:
y = 0
if y > 18:
y = 18
z = False
if alist[x][y] == 0:
eval(wb + "({},{})".format(x, y))
if wb == "black":
alist[x][y] = 1
wb1 = "黑棋"
wb = "white"
elif wb == "white":
alist[x][y] = 2
wb1 = "白棋"
wb = "black"

这里,就是最核心的对弈程序。首先我们进入主循环,并获取事件。在这里,我们除了对按下关闭按钮进行了几乎每个pygame程序都会进行的处理外,还对按下鼠标事件进行了处理。首先,我们获取鼠标点击的坐标,通过计算来得到对应的格点(这里对不在格点上的点击进行四舍五入,对棋盘之外的点击自动匹配与其最近的格点)。然后,根据此时的先手方和计算得的格点运行black/white函数,绘制所需的棋子。

                    xx = x
yy = y
while True:
if xx == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
break
else:
xx -= 1
num = 0
while True:
if xx == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
yy += 1
break
else:
yy -= 1
num = 0
while True:
if yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy += 1
break
else:
xx -= 1
yy -= 1
num = 0
while True:
if xx == 18:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy -= 1
break
else:
xx -= 1
yy += 1
num = 0
while True:
if xx == 18:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy -= 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()

这是冗长的胜负判断,具体内容我自己也难以解释(这个程序编了有一段时间了)。主要思路,是向各个方向寻找同色棋子的连接,并判断是否满五个棋。当然,不得不承认,这一段确实不太高明,似乎有别人发布过比我更好的方案,感兴趣的可以上网找找。

do()

这是程序的收尾,也就是对do()函数的运行。至此,整个程序完全结束。

完整代码:

#coding:utf-8
import sys
import pygame
import random
def do():
def black(x, y):
a = 20
b = 20
c = 20
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update() def white(x, y):
a = 170
b = 170
c = 170
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((615, 615))
pygame.display.set_caption('五子棋')
screen.fill("#DD954F")
a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)
a.fill(color='#121010')
b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)
b.fill(color="#DD954F")
c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)
c.fill(color='#121010')
d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)
d.fill(color="#DD954F")
e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)
e.fill(color="#DD954F")
screen.blit(a, (6.5, 6.5))
screen.blit(b, (15, 15))
screen.blit(c, (18, 18))
for j in range(18):
for i in range(18):
screen.blit(e, (20 + 32 * i, 20 + 32 * j))
alist = []
for j in range(19):
alistone = []
for i in range(19):
alistone.append(0)
alist.append(alistone)
pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
pygame.display.flip()
wb = "black"
font1 = pygame.font.SysFont('stxingkai', 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
x = round((x - 19.5) / 32)
y = round((y - 19.5) / 32)
if x < 0:
x = 0
if x > 18:
x = 18
if y < 0:
y = 0
if y > 18:
y = 18
z = False
if alist[x][y] == 0:
eval(wb + "({},{})".format(x, y))
if wb == "black":
alist[x][y] = 1
wb1 = "黑棋"
wb = "white"
elif wb == "white":
alist[x][y] = 2
wb1 = "白棋"
wb = "black"
xx = x
yy = y
while True:
if xx == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
break
else:
xx -= 1
num = 0
while True:
if xx == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
yy += 1
break
else:
yy -= 1
num = 0
while True:
if yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy += 1
break
else:
xx -= 1
yy -= 1
num = 0
while True:
if xx == 18:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy -= 1
break
else:
xx -= 1
yy += 1
num = 0
while True:
if xx == 18:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy -= 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}赢了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
do()

Python双人五子棋的更多相关文章

  1. 双人五子棋对战(需要EasyX图像库)

    实训要做项目呐.天天坐在电脑面前累死了.最近题刷的少.大多数都挺水.就不挨个编辑发上来了.发发白天写的项目吧.可能好几天更一下.实训结束恢复正常. 这个游戏需要EasyX的图像库.有兴趣的可以下一个图 ...

  2. js+html5双人五子棋(源码下载)

    代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  3. Python开发五子棋游戏【新手必学】

    五子棋源码,原创代码,仅供 python 开源项目学习.目前电脑走法笨笨的,下一期版本会提高电脑算法ps:另外很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为 ...

  4. java swing 双人五子棋源代码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...

  5. [深度学习]实现一个博弈型的AI,从五子棋开始(2)

    嗯,今天接着来搞五子棋,从五子棋开始给小伙伴们聊AI. 昨天晚上我们已经实现了一个五子棋的逻辑部分,其实讲道理,有个规则在,可以开始搞AI了,但是考虑到不够直观,我们还是顺带先把五子棋的UI也先搞出来 ...

  6. js,jquery,css,html5特效

    包含js,jquery,css,html5特效,源代码 本文地址:http://www.cnblogs.com/roucheng/p/texiao.html 2017新年快乐特效 jQuery最新最全 ...

  7. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  8. Python:游戏:五子棋之人机对战

    本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知 ...

  9. Unity2017五子棋大战_人机_双人_UNET联网

    五子棋大战源码工程基于Unity2017.2进行开发,分为人机.双人.UNET网络三种对战方式,配有案例讲解视频, 其中人机五子棋AI有三种开发难度,欢迎有兴趣的同学加入学习! . 目录 000-展示 ...

随机推荐

  1. Mybatis-自定义类型处理器

    类型转换器:mybatis中有一些常用的类型转换器,比如把Java中的short类型转换为mysql中的short类型:但是如果现在是Java中的Date类型,但是我想要存储到数据库中转换为Long类 ...

  2. JVM诊断及工具笔记(4) 使用visualvm分析JVM堆内存泄漏

    在这里感谢最近一直阅读我文章的小伙伴,如果觉得文章对你有用,可以帮忙关注转载,需要的时候可以及时找到文章. 背景 今年Q3季度我们在推广业务方使用Iceberg,当时为了让不同业务线的用户可以使用自己 ...

  3. Educational Codeforces Round 108 (Div. 2), C map套vector存储

    地址  Problem - C - Codeforces 题目 题意 一个学校有n个人参加比赛,他们分别属于ui队,每个人的能力值为si 当每个队需要1~n个人的时候,这个学校能参加的人的能力值和最大 ...

  4. Java开发中关于资源路径获取问题

    描述 在开发中经常会读取配置文件,在Web开发中大多数都是在项目路径下.核心的API类或者是Controller异或是jsp页面等,基本都是基于web应用的相对路径,很少去操作绝对路径,但是在客户端. ...

  5. Xray学习

    Xray 目前支持的漏洞检测类型包括: XSS漏洞检测 (key: xss) SQL 注入检测 (key: sqldet) 命令/代码注入检测 (key: cmd-injection) 目录枚举 (k ...

  6. 漏洞复现:MS14-064 OLE远程代码执行漏洞

    MS14-064OLE远程代码执行漏洞 攻击机:Kali Linux 2019 靶机:Windows 7 x64.x32 攻击步骤: 1.打开攻击机Kali Linux 2019系统和靶机Window ...

  7. ReadWriteLock 接口详解

    ReadWriteLock 接口详解 这是本人阅读ReadWriteLock接口源码的注释后,写出的一篇知识分享博客 读写锁的成分是什么? 读锁 Lock readLock(); 只要没有写锁,读锁可 ...

  8. c++:-3

    上一节学习了C++的函数:c++:-2,本节学习C++的数组.指针和字符串 数组 定义和初始化 定义 例如:int a[10]; 表示a为整型数组,有10个元素:a[0]...a[9] 例如: int ...

  9. 【Pandas vs SQL】数据分析代码逐行比对,孰优孰劣?

    在数据分析领域,pandas是python数据分析基础工具,SQL是数据库最常用分析语言.二者有相通的地方,也有很大的语法不同,做起数据分析来,谁将更胜一筹呢? 做过业务开发.跟数据库打交道比较多的小 ...

  10. 攻防世界web进阶题—bug

    攻防世界web进阶题-bug 1.打开题目看一下源码,没有问题 2.扫一下目录,没有问题 3.查一下网站的组成:php+Apache+Ubuntu 只有登录界面 这里可以可以想到:爆破.万能密码.进行 ...