python五子棋_python 五子棋源码
五子棋源码,原创代码,仅供python开源项目学习。
目前电脑走法笨笨的,下一期版本会提高电脑算法
第二版已发布与另外一篇博文,有兴趣的可以去查看下载。
import pygame
import time
SCREEN_WIDTH=900
SCREEN_HEIGHT=800
BG_COLOR=pygame.Color(200, 200, 200)
Line_COLOR=pygame.Color(255, 255, 200)
TEXT_COLOR=pygame.Color(255, 0, 0)
# 定义颜色
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
class MainGame():
window = None
Start_X = 50
Start_Y = 50
Line_Span = 40
Max_X = Start_X + 18 * Line_Span
Max_Y = Start_Y + 18 * Line_Span
player1Color = 'B'
player2Color = 'W'
overColor = 'S'
# 1代表玩家1 , 2代表到玩家2 0代表结束
Putdownflag = player1Color
ChessmanList = []
def __init__(self):
'''初始化'''
def startGame(self):
MainGame.window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("五子棋")
#初始化
while True:
time.sleep(0.1)
#获取事件
MainGame.window.fill(BG_COLOR)
self.drawchChessboard()
self.bitechessman()
self.VictoryOrDefeat()
self.Computerplay()
self.getEvent()
pygame.display.update()
pygame.display.flip()
def drawchChessboard(self):
for i in range(0,19):
x = MainGame.Start_X + i * MainGame.Line_Span
y = MainGame.Start_Y + i * MainGame.Line_Span
pygame.draw.line(MainGame.window, BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], 1)
pygame.draw.line(MainGame.window, BLACK, [MainGame.Start_X, y], [MainGame.Max_X, y], 1)
def getEvent(self):
# 获取所有的事件
eventList = pygame.event.get()
for event in eventList:
if event.type == pygame.QUIT:
self.endGame()
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
mouse_x = pos[0]
mouse_y = pos[1]
if (mouse_x > MainGame.Start_X- MainGame.Line_Span/2 and mouse_x < MainGame.Max_X + MainGame.Line_Span / 2) and (mouse_y > MainGame.Start_Y- MainGame.Line_Span/2 and mouse_y < MainGame.Max_Y + MainGame.Line_Span / 2):
#print( str(mouse_x) + "" + str(mouse_y))
#print(str(MainGame.Putdownflag))
if MainGame.Putdownflag != MainGame.player1Color:
return
click_x = round((mouse_x - MainGame.Start_X) / MainGame.Line_Span)
click_y = round((mouse_y - MainGame.Start_Y) / MainGame.Line_Span)
click_mod_x = (mouse_x - MainGame.Start_X) % MainGame.Line_Span
click_mod_y = (mouse_y - MainGame.Start_Y) % MainGame.Line_Span
if abs(click_mod_x-MainGame.Line_Span/2) >=5 and abs(click_mod_y-MainGame.Line_Span/2) >=5:
#print("有效点:x="+str(click_x)+" y="+str(click_y))
#有效点击点
self.putdownchess(MainGame.player1Color, click_x, click_y)
else:
print("out")
def putdownchess(self, t, x, y):
flag = False
for item in MainGame.ChessmanList:
if item.x == x and item.y == y:
flag = True
if not flag:
cm = Chessman(t, x, y)
MainGame.ChessmanList.append(cm)
MainGame.Putdownflag = MainGame.player2Color
#print("ChessmanListlen:" + str(len(MainGame.ChessmanList)))
def bitechessman(self):
for item in MainGame.ChessmanList:
item.displaychessman()
def bureautime(self):
''''''
def Computerplay(self):
if MainGame.Putdownflag == MainGame.player2Color:
print("轮到电脑了")
computer = self.getComputerplaychess()
if computer==None:
return
#print("computer x="+str(computer.x) + " y="+str(computer.y))
MainGame.ChessmanList.append(computer)
MainGame.Putdownflag = MainGame.player1Color
def getComputerplaychess(self):
if len(MainGame.ChessmanList) == 0:
return Chessman(MainGame.player2Color, 9, 9)
# 进攻
for item in MainGame.ChessmanList:
if item.troops == MainGame.player2Color:
prev_x = item.x - 1
prev_y = item.y - 1
next_x = item.x + 1
next_y = item.y + 1
print(str(self.calVerticalityCount(item)))
if self.calVerticalityCount(item) == 4 :
if not len(list(filter(lambda x: x.x == prev_x and x.y == item.y, MainGame.ChessmanList))) :
return Chessman(MainGame.player2Color, prev_x, item.y)
elif not len(list(filter(lambda x: x.x == item.x+4 and x.y == item.y, MainGame.ChessmanList))) :
print("YYYY " +str(item.x)+" "+str(item.y))
return Chessman(MainGame.player2Color, item.x+4, item.y)
if self.calHorizontalCount(item) == 4:
if not len(list(filter(lambda x: x.x == item.x and x.y == item.y-1, MainGame.ChessmanList))):
return Chessman(MainGame.player2Color, prev_x, item.y-1)
elif not len(
list(filter(lambda x: x.x == item.x and x.y == item.y+ 4, MainGame.ChessmanList))):
return Chessman(MainGame.player2Color, item.x, item.y+ 4)
# 防守
for item in MainGame.ChessmanList:
if item.troops == MainGame.player1Color:
next_x = item.x + 1
next_y = item.y + 1
if self.calVerticalityCount(item) == 4 :
if not len(list(filter(lambda x: x.x == prev_x and x.y == item.y, MainGame.ChessmanList))) :
return Chessman(MainGame.player2Color, prev_x, item.y)
elif not len(list(filter(lambda x: x.x == item.x+4 and x.y == item.y, MainGame.ChessmanList))) :
return Chessman(MainGame.player2Color, item.x+4, item.y)
if self.calHorizontalCount(item) == 4:
if not len(list(filter(lambda x: x.x == item.x and x.y == item.y-1, MainGame.ChessmanList))):
return Chessman(MainGame.player2Color, prev_x, item.y-1)
elif not len(
list(filter(lambda x: x.x == item.x and x.y == item.y+ 4, MainGame.ChessmanList))):
return Chessman(MainGame.player2Color, item.x, item.y+ 4)
if next_x < 19 and len(list(filter(lambda x: x.x == next_x and x.y == item.y, MainGame.ChessmanList)))==0:
return Chessman(MainGame.player2Color, next_x, item.y)
if next_y < 19 and len(list(filter(lambda x: x.x == item.x and x.y == next_y, MainGame.ChessmanList)))==0:
return Chessman(MainGame.player2Color, item.x, next_y)
for i in range(0,18):
for j in range(0, 18):
fi= filter(lambda x: x.x == i and x.y == j, MainGame.ChessmanList)
if len(list(fi)) == 0 :
return Chessman(MainGame.player2Color, i, j)
return None
#判断游戏胜利
def VictoryOrDefeat(self):
for item in MainGame.ChessmanList:
if self.calHorizontalCount(item) == 5 or self.calVerticalityCount(item) == 5 or self.calBevelsUpCount(item) == 5 or self.calBevelsDownCount(item)==5:
txt =""
if item.troops == MainGame.player1Color :
txt = "玩家"
else:
txt = "电脑"
MainGame.window.blit(self.getTextSuface("%s获胜" % txt), (SCREEN_WIDTH-100, 200))
MainGame.Putdownflag = MainGame.overColor
return
def calHorizontalCount(self,chessman):
count = 1
for i in range(1, 5):
fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x and x.y == chessman.y + i,
MainGame.ChessmanList)
if len(list(fi)) == 1:
count += 1
else:
break
return count
def calVerticalityCount(self, chessman):
count = 1
for i in range(1, 5):
fi = filter(lambda x:x.troops==chessman.troops and x.x == chessman.x+ i and x.y == chessman.y , MainGame.ChessmanList)
if len(list(fi)) == 1:
count += 1
else:
break
return count
def calBevelsUpCount(self, chessman):
count = 1
for i in range(1, 5):
fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x + i and x.y == chessman.y - i,
MainGame.ChessmanList)
if len(list(fi)) == 1:
count += 1
else:
break
return count
def calBevelsDownCount(self, chessman):
count = 1
for i in range(1, 5):
fi = filter(lambda x: x.troops == chessman.troops and x.x == chessman.x + i and x.y == chessman.y + i,
MainGame.ChessmanList)
if len(list(fi)) == 1:
count += 1
else:
break
return count
def getTextSuface(self, text):
pygame.font.init()
# print(pygame.font.get_fonts())
font = pygame.font.SysFont('kaiti', 18)
txt = font.render(text, True, TEXT_COLOR)
return txt
def endGame(self):
print("exit")
exit()
class Chessman():
def __init__(self,t,x,y):
self.images = {
'B' : pygame.image.load("imgs/black.gif"),
'W' : pygame.image.load('imgs/white.gif'),
'N' : pygame.image.load("imgs/black.gif"),
}
self.troops = t
self.image = self.images[self.troops]
self.x = x
self.y = y
self.rect = self.image.get_rect()
self.rect.left = MainGame.Start_X + x * MainGame.Line_Span - MainGame.Line_Span/2
self.rect.top = MainGame.Start_Y + y * MainGame.Line_Span - MainGame.Line_Span/2
def displaychessman(self):
if self.troops != 'N':
self.image = self.images[self.troops]
MainGame.window.blit(self.image,self.rect)
if __name__ == '__main__':
MainGame().startGame()
本作品采用《CC 协议》,转载必须注明作者和本文链接
python五子棋_python 五子棋源码相关推荐
- python浪漫代码-python七夕浪漫表白源码
本文实例为大家分享了python七夕浪漫表白的具体代码,供大家参考,具体内容如下 from turtle import * from time import sleep def go_to(x, y) ...
- 照片背景底色更换工具二(python+flask网页版源码及打包)
照片背景底色更换工具二(python+flask网页版源码及打包) 所有源码 文件结构 static images huaman_src_test.jpg huaman_result_test.png ...
- Python个人网盘源码、云盘系统源程序,基于Django+Mysql
Python个人网盘源码.云盘系统源程序,基于Django+Mysql 1.安装依赖 pip install -r requirements.txt 2.检查配置文件,修改邮箱和数据库配置 # myc ...
- python爬虫教程:Scrapy框架爬取Boss直聘网Python职位信息的源码
今天小编就为大家分享一篇关于Scrapy框架爬取Boss直聘网Python职位信息的源码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧 分析 使用Crawl ...
- Python与Seo工具源码,全网搜录查询助手exe
Python与Seo工具源码,全网搜录查询助手exe 很多人都说,seo是一个玄学,诚然,一方面是排名,另一方面是收录,尤其是对于渣渣而言,当然收录和排名都需要去验证,去查询,乃至去监控,大批量的话, ...
- python金融实战 源代码_穆棱市seo总代直销python金融量化营业实战课程 python量化项目实战源码+课件+视频...
python金融量化生意实战课程 python量化项目实战源码+课件+视频 1. 自愿化生意综述 重要实质: 课程实质综述,自愿化/算法生意先容,python正在自愿生意中的使用简介 2. 量化生意体 ...
- 图片坐标提取软件/图片坐标点和像素点颜色提取软件/图片坐标获取工具/Python图片坐标获取源码/图片像素坐标获取软件/python tkinter 图片显示(完全开源)
该软件使用python写的,可以提取像素点的坐标还有也能获取像素点的16进制数据RGB565和RGB888(RGB888仅最新的源码才支持),可以单点坐标也可以按键坐标,甚至可以使用简单的左右键配合使 ...
- 反编译python 生成的exe源码
反编译python 生成的exe源码 记录反编译exe工具使用 工具准备 – pyinstxtractor.py – uncompyle6 – sublime Text(或者其他的二进制编辑工具) 一 ...
- python 3.10.0源码编译安装
python 3.10.0源码编译安装 文章目录 python 3.10.0源码编译安装 1. 安装编译依赖工具 2. 下载python 3.10.0 3. 编译安装 Python 4. 体验 1. ...
最新文章
- SAP WM LT15不能取消二步法确认场景中只做过第一步确认的TO单
- 什么是 “进程、线程、协程”?
- LYNC2013部署系列PART2:后端部署
- linux kernel 内存管理 感想总结(未完待续)
- 机器学习&AI之c++随笔(1)-配置tensorflow并运行第一个C++程序
- Win8下在Vmware11中安装使用苹果系统OS X 10.10
- OpenShift Origin中的Kubernetes Spark运算符(第1部分)
- idea springboot 发布webservice 发布服务_阿里云发布 Spring Boot 新脚手架,真香
- 倒叙输出 php,php foreach正序倒序输出示例代码
- 分布与并行计算—生命游戏(Java)
- 不规则多边形填充_花一分钟看一个案例,PPT中图片填充形状的应用
- OpenCV学习笔记:矩阵/向量处理
- 面试题——股票利益最大化
- 多个帐户都用root 来登录 怎么看另一个用户使用的那些命令
- Apple设备的列表中的手机却不能更新iOS 14怎么办
- centos 并发请求数_彻底理解 jmeter 的线程数与并发数之间的关系
- Ubuntu安装搜狗输入法只需四步
- 阿里云网盘内测_叫板百度网盘?阿里云网盘内测中,下载速度是亮点
- J2EE框架DDoS漏洞预警公告
- ORACLE 金额转大写中文