# Python双人五子棋

[En]

#coding:utf-8


import sys
import pygame
import random


def do():


[En]

Then, there is the most important chess piece drawing function, let’s first look at black chess:

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()


[En]

In the same way, White is drawn in a similar way. I will not repeat it here, but only give the code:

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()


[En]

Next, there is a long and tasteless drawing of the chessboard:

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()


[En]

As you can see, we first drew a fancy border, then drew the grid, and by the way defined a two-dimensional list of 1919 filled with 0 (it seems redundant here, but later, you will find it extremely useful! ). Finally, nine mediocre dots were drawn on the chessboard. At this point, our Gobang is in its infancy. However, it is not enough to use it to play games.

    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()


[En]

This is a lengthy judgment, and it is difficult for me to explain the details myself (this program has been compiled for some time). The main idea is to find the connection of the same color pieces in all directions, and to determine whether there are five chess pieces (here we will use a two-dimensional list of the chessboard situation). Of course, I have to admit that this paragraph is really not very clever. It seems that someone else has released a better plan than me. Those who are interested can look it up on the Internet.

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()

Original: https://www.cnblogs.com/godforever/p/16389057.html
Author: GodForever
Title: Python双人五子棋

(0)

### 大家都在看

• #### 数据分析处理快速上手教程matplotlib+numpy+pandas（基础讲解）

文章目录 python数据挖掘 * 一、数据挖掘基础环境安装与使用 – 1.1 库的安装 1.2 Jupyter Notebook使用 + 1.2.1 Jupyter …

Python 2023年8月23日
0130
• #### SQL（及存储过程）跑得太慢怎么办？

SQL作为目前最常用的数据处理语言，广泛应用于查询、跑批等场景。当数据量较大时，使用SQL（以及存储过程）经常会发生跑得很慢的情况，这就要去优化SQL。优化SQL有一些特定的套路，…

Python 2023年9月26日
0100
• #### 使用GitHub Actions实现自动化部署

前言 大家在工作中想必都是通过自动化部署来进行前端项目的部署的，也就是我们在开发完某个需求时，我们只需要将代码推送到某个分支，然后就能自动完成部署，我们一般不用关心项目是如何bui…

Python 2023年10月16日
0155
• #### Matplotlib 可视化必备神书，附pdf下载

[ Matplotlib Release 1.2.0 I User’s Guide 1 1 Introduction 3 2 Installing 5 2.1 Manu…

Python 2023年9月2日
0130
• #### 知识图谱构建全流程

一、知识图谱简介 知识图谱，是结构化的语义知识库，用于迅速描述物理世界中的概念及其相互关系，通过知识图谱能够将Web上的信息、数据以及链接关系聚集为知识，使信息资源更易于计算、理解…

Python 2023年9月28日
0127
• #### Python实现导弹自动追踪

啊哦~你想找的内容离你而去了哦 内容不存在，可能为如下原因导致： ① 内容还在审核中 ② 内容以前存在，但是由于不符合新 的规定而被删除 ③ 内容地址错误 ④ 作者删除了内容。 可…

Python 2023年10月8日
0102
• #### 30个Python小游戏，上班摸鱼我能玩一天【内附源码】

大家好，我是辣条。 今天给大家带来30个py小游戏，一定要收藏！ 目录 有手就行 1、吃金币 2、打乒乓 3、滑雪 4、并夕夕版飞机大战 5、打地鼠 简简单单 6、小恐龙 7、消消…

Python 2023年7月31日
0109
• #### python从入门到实践经验分享——外星人小游戏 转为exe

前言 一、首先需要安装pyinstaller 二、使用步骤 1.以书中外星人侵略为例： 2.多个py文件转换为exe，首先要制作spec文件 3.我们进入pycharm修改制作的s…

Python 2023年9月24日
0146
• #### pandas 插入空列_Pandas个人操作练习（1）创建dataframe及插入列、行操作

使用pandas之前要导入包： import numpy as np import pandas as pd import random #其中有用到random函数，所以导入 一…

Python 2023年8月16日
0138
• #### 安装爬虫框架Scrapy，安装后运行不了~

### 回答1： 好的，下面是一个Python 爬虫 框架 Scrapy_的实例，包括如何定义 _爬虫_和使用 _Scrapy 框架_中的其他功能。 首先，您需要 _安装 Scra…

Python 2023年10月1日
0122
• #### python教你写游戏超级玛丽，可运行有代码

一、先看效果： 二、安装python3.92，pycharm编辑器 PyCharm 是一款功能强大的 Python 编辑器，具有跨平台性，鉴于目前最新版 PyCharm 使用教程较…

Python 2023年9月19日
0122
• #### Django实现注册登录功能

1.在templates文件下创建 register.html 2.在模板里写注册的样式表单 综合内容如上，具体解析如下 想要实现表单中的内容，必须要写表单标签 在form标签里写…

Python 2023年8月5日
0128
• #### Pytorch:手把手教你搭建简单的全连接网络

代码里的注释一定要看！！！里面包括了一些基本知识和原因 可以依次把下面的代码段合在一起运行，也可以通过jupyter notebook分次运行 第一步：一些库的导入 import …

Python 2023年8月1日
0141
• #### Python学习：import的正确用法

import语句用来导入其他python文件（称为模块module），使用该模块里定义的类、方法或者变量，从而达到代码复用的目的。 将要建立文件的结构为： Tree |____ m…

Python 2023年11月1日
0102
• #### 开始使用DataGrip —— 激活

404. 抱歉，您访问的资源不存在。 可能是网址有误，或者对应的内容被删除，或者处于私有状态。 代码改变世界，联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

Python 2023年6月9日
0164
• #### Python常用库的使用（Numpy，Pandas，Matplotlib，wordcloud，Opencv-python，PIL）

一、前言 最近做python实验的时候，重点考察了对题述的库的使用，经过一段时间学习，将其汇总至一处，方便取用。 二、Numpy库 首先安装numpy库，只需要在cmd窗口输入pi…

Python 2023年9月1日
0127