Python基于基于pygame实现单机版五子棋对战实现单机版五子棋对战
主要为大家详细介绍了Python基于pygame实现单机版五子棋对战,文中示例代码介绍的非常详细,具有一定的
参考价值,感兴趣的小伙伴们可以参考一下
python实现的五子棋,能够自动判断输赢,没有是实现电脑对战功能
源码下载:pygame五子棋
# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
initChessList = []
initRole = 1 # 代表白子下 2:代表当前是黑子下
resultFlag = 0
userFlag = True
class StornPoint():
def __init__(self, x, y, value = 0):
'''
:param x: 代表x轴坐标
:param y: 代表y轴坐标
:param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
'''
self.x = x
self.y = y
self.value = value
pass
def initChessSquare(x, y):
'''
初始化棋盘的坐标
:param x:
:param y:
:return:
'''
# 使用二维列表保存了棋盘是的坐标系,和每个落子点的数值
for i in range(15): # 每一行的交叉点坐标
rowList = []
for j in range(15): # 每一列的交叉点坐标
pointX = x + j*40
pointY = y + i*40
# value = 0
sp = StornPoint(pointX, pointY, 0)
rowList.append(sp)
pass
initChessList.append(rowList)
pass
# 处理事件
def eventHandler():
global userFlag
'''
监听各种事件
:return:
'''
for event in pygame.event.get():
global initRole
# 监听点积退出按钮事件
if event.type == QUIT:
pygame.quit()
sys.exit()
pass
# 监听鼠标点积事件
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos() #
print((x, y))
i = j = 0
for temp in initChessList:
for point in temp:
if x >= point.x - 15 and x <= point.x + 15 \
and y >= point.y - 15 and y <= point.y + 15:
# 当前区域没有棋子,并且是白子下