python实现五子棋双人对弈

本文实例为大家分享了python实现五子棋双人对弈的具体代码,供大家参考,具体内容如下
我用的是pygame模块来制作窗口
代码如下:

# 1、引入pygame 和 pygame.localsimport pygamefrom pygame.locals import *import timeimport sys initChessList = []initRole = 2 # 代表白子下 2:代表当前是黑子下resultFlag= 0userFlag = True class StornPoint():def __init__(self, x, y, value = https://www.it610.com/article/0):''':param x: 代表x轴坐标:param y: 代表y轴坐标:param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子'''self.x = xself.y = yself.value = https://www.it610.com/article/valuepass def initChessSquare(x, y):'''初始化棋盘的坐标:param x::param y::return:'''# 使用二维列表保存了棋盘是的坐标系,和每个落子点的数值for i in range(15):# 每一行的交叉点坐标rowList = []for j in range(15):# 每一列的交叉点坐标pointX = x + j*40pointY = y + i*40# value= https://www.it610.com/article/0sp = StornPoint(pointX, pointY, 0)rowList.append(sp)passinitChessList.append(rowList)pass # 处理事件def eventHandler():global userFlag'''监听各种事件:return:'''for event in pygame.event.get(): globalinitRole# 监听点积退出按钮事件if event.type == QUIT:pygame.quit()sys.exit()pass# 监听鼠标点积事件if event.type == MOUSEBUTTONDOWN:x, y = pygame.mouse.get_pos() #print((x, y))i = j = 0for temp in initChessList:for point in temp:ifx >= point.x - 15 and x <= point.x + 15 \and y >= point.y - 15 and y <= point.y + 15:# 当前区域没有棋子,并且是白子下if point.value =https://www.it610.com/article/= 0 and initRole == 1 and userFlag:point.value = 1judgeResult(i, j, 1)initRole = 2# 切换棋子颜色passelif point.value == 0 and initRole == 2 and userFlag:point.value = 2judgeResult(i, j, 2)initRole = 1# 切换棋子颜色passbreakpassj += 1passi += 1j = 0passpass pass # 判断输赢函数def judgeResult(i, j, value):global resultFlag flag = False# 用于判断是否已经判决出输赢forx inrange(j - 4, j + 5):# 水平方向有没有出现5连if x>= 0 and x + 4 < 15 :if initChessList[i][x].value =https://www.it610.com/article/= value and /initChessList[i][x + 1].value == value and /initChessList[i][x + 2].value == value and /initChessList[i][x + 3].value == value and /initChessList[i][x + 4].value == value :flag = Truebreakpassfor x in range(i - 4, i + 5):# 垂直方向有没有出现5连if x>= 0 and x + 4 < 15:if initChessList[x][j].value =https://www.it610.com/article/= value and /initChessList[x + 1][j].value == value and /initChessList[x + 2][j].value == value and /initChessList[x + 3][j].value == value and /initChessList[x + 4][j].value == value:flag = Truebreakpass # 判断东北方向的对角线是否出现了5连for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):if x>= 0 and x+4< 15 and y + 4 >= 0 and y < 15:if initChessList[y][x].value =https://www.it610.com/article/= value and /initChessList[y - 1][x + 1].value == value and /initChessList[y - 2][x + 2].value == value and /initChessList[y - 3][x + 3].value == value and /initChessList[y - 4][x + 4].value == value:flag = Truebreakpasspasspass # 判断西北方向的对角是否出现了五连for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):if x>= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:if initChessList[y][x].value =https://www.it610.com/article/= value and /initChessList[y + 1][x + 1].value == value and /initChessList[y + 2][x + 2].value == value and /initChessList[y + 3][x + 3].value == value and /initChessList[y + 4][x + 4].value == value:flag = Truebreakpasspasspass if flag:resultFlag = valuepasspass # 加载素材def main():initRole = 2# 代表白子下 2:代表当前是黑子下global resultFlag, initChessListinitChessSquare(27, 27)# 初始化棋牌pygame.init()# 初始化游戏环境# 创建游戏窗口screen = pygame.display.set_mode((620,620), 0, 0) # 第一个参数是元组:窗口的长和宽# 添加游戏标题pygame.display.set_caption("五子棋小游戏")# 图片的加载background = pygame.image.load('images/bg.png')blackStorn = pygame.image.load('images/storn_black.png')whiteStorn = pygame.image.load('images/storn_white.png')winStornW = pygame.image.load('images/white.png')winStornB = pygame.image.load('images/black.png')rect = blackStorn.get_rect() while True:screen.blit(background, (0, 0))# 更新棋盘棋子for temp in initChessList:for point in temp:if point.value =https://www.it610.com/article/= 1:screen.blit(whiteStorn, (point.x - 18, point.y - 18))passelif point.value == 2:screen.blit(blackStorn, (point.x - 18, point.y - 18))passpasspass# 如果已经判决出输赢if resultFlag> 0:initChessList = []# 清空棋盘initChessSquare(27, 27) # 重新初始化棋盘if resultFlag == 1:screen.blit(winStornW, (50,100))else:screen.blit(winStornB, (50,100))passpygame.display.update() if resultFlag >0:time.sleep(3)resultFlag = 0passeventHandler()pass pass if __name__ == "__main__":main()pass

插图:放在同一目录下的images文件夹里
bg.png
python实现五子棋双人对弈
文章图片

storn_white.png
python实现五子棋双人对弈
文章图片

storn_black.png
python实现五子棋双人对弈
文章图片

【python实现五子棋双人对弈】white.png
python实现五子棋双人对弈
文章图片

black.png
python实现五子棋双人对弈
文章图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读