使用的库
pygame、pyautogui
流程简述
1.画棋盘
设置网格间隔40px ,留白 80 px ,与网格横竖线数量 ,初定19 × 19 。
2.鼠标点击
鼠标点击取得坐坐标(x0 , y0),再获得最近的网格上的点(x1 , y1),再将每次动作获得的(x1 , y1 )放入列表 chess_location 中 。
再通过:
chess_location_b = chess_location[0::2]
chess_location_w = chess_location[1::2]
分别获得黑棋和白棋所走过的坐标。
3.判断胜负
这一块网上有很多不同的方法,我为了让大家读懂尽量写的详细了。
首先 ,我们要知道连五有四个方向:竖直 ,水平 ,右上左下 , 右下左上 。
每次将新落下的子分别进行4个方向的判断,判断是否出现连五及以上。
我使用的方法是:
def result(x): # x 为 chess_location_b 或者 chess_location_w
# 竖直
score = []
for i in range(cell_num): #cell_num = 19
if [x[-1][0], i ] in x:
score.append([x[-1][0], i ])
if score.__len__() >= 5:
return 1
else:
score =[]
大概意思就是最新落下的(x1 , y1)中的竖直方向从上往下检查如果出现黑(白)棋 ,则将出现棋子的坐标加入列表 score 中 , 如果出现异色棋子或者没有棋子,则清空 score 中的元素 ,如果列表 score 中的元素数量大于等于5个 ,则分胜负 。
如果棋子填满棋盘但是仍没有分出胜负 ,则平局 。
代码及结果
代码
import pygame,pyautogui
from pygame.locals import *
# 初始参数
cell_size = 40
space = 80
cell_num = 19
grid_size = (cell_num - 1)*cell_size + space*2
screen = pygame.display.set_mode([grid_size,grid_size],0,32)
chess_location , chess_location_w , chess_location_b = [] , [] , []
# 画棋盘
def grid():
screen.fill([208,173,108])
font = pygame.font.SysFont("arial", 20)
i = 0
for x in range(0, cell_size * cell_num

最低0.47元/天 解锁文章
2442

被折叠的 条评论
为什么被折叠?



