这里只想简单演示下一个简单的五子棋界面和落子等操作,主要为了后面设计AI对战方便演示,AI算法后面设计吧,最近事太多了,,,,。
希望本文有助于你制作自己期望的简单的五子棋。
一、pygame初始化画布界面
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[255,255,255]
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
pygame.display.update()
然后呢上面代码的运行结果如下:
背景是白色的,当然我们常见的棋盘应该是棕黄色的,于是我们查找颜色的数值表。点击这里RGB颜色表。
于是找到这个颜色和对应的RGB:
我觉得这个Tan2的颜色还行,于是选择这个的RGB值替换[255,255,255]
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
pygame.display.update()
运行结果如下: 现在看起来还行了哈,接下来开始画棋盘。
二、画棋盘
首先棋盘需要直线,先抛出画直线的函数。
line_color = [0,0,0]
pygame.draw.line(screen,line_color,[0,0],[670,670],2)
参数解释为screen表示在当前这个窗口上作画,line_color表示线条颜色,[0,0]表示起始位置,[670,670]表示终止位置,2表示线条粗细,数值越大线条越粗。
我们五子棋一般棋盘大小为15x15,我们初始化的窗口大小为670x670,因为每个棋子之间要存在一定间隔这样显得好看些。
于是我们定义每个棋子的半径大小为20,然后每颗棋子间隔为2,也就是相邻的两条横线或者竖线之间间隔为22 * 2 = 44。
然后因为670 - 44 * (15-1) = 54
然后54 / 2 = 27。也就是最边缘的两条线和棋盘边缘距离应该为27。
所以我们利用循环开始画15条棋盘的线,应该从27开始,循环间隔为44
for i in range(27,670,44):
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
希望方便理解上面的代码,完整代码如下:
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]
line_color = [0,0,0]
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
for i in range(27,670,44):
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
pygame.display.update()
运行结果如下 有点意思了哈~
再考虑下细节,我们应该在棋盘中心位置打一个标记点,一个黑色实心小圆。
画实心圆代码如下:
pygame.draw.circle(screen, line_color,[27+44*7,27+44*7], 8,0)
screen和line_color上面解释过了,[27+447,27+447]为圆心位置,这里是棋盘正中心,8为圆的半径大小,0表示实心圆,如果设置为1输出空心圆。
加进去后运行结果如下:
这样更好看些~
三、获取鼠标信息下棋子
现在得根据鼠标信息下棋了,我们点击鼠标左键然后落子,为了让效果看起来更好,我们应该时实获取鼠标位置信息,然后显示出当前应该下棋的位置。
抛出获取鼠标左键的代码如下:
x,y = pygame.mouse.get_pos()
我们获取鼠标坐标信息后,我们需要找到这个坐标具体落入哪个位置,所以需要判断一下:
def find_pos(x,y):
for i in range(27,670,44):
for j in range(27,670,44):
L1=i-22
L2=i+22
R1=j-22
R2=j+22
if x>=L1 and x<=L2 and y>=R1 and y<=R2:
return i,j
return x,y
返回可以落子的位置后,我们需要显示出来,我们用一个正方形框出来,抛出这个正方形框的代码如下:
x,y=find_pos(x,y)
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
和刚才画圆画直线的函数基本一样,[0 ,229 ,238 ]是颜色的RGB,我找到是一个荧光绿色类似的颜色,然后[x-22,y-22,44,44]表示从位置(x-22,y-22)开始向右向下分别延长44的长度,得到一个正方形,2表示线条的粗细程度,1表示空心的正方形,和画圆那里一样。
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]
line_color = [0,0,0]
def find_pos(x,y):
for i in range(27,670,44):
for j in range(27,670,44):
L1=i-22
L2=i+22
R1=j-22
R2=j+22
if x>=L1 and x<=L2 and y>=R1 and y<=R2:
return i,j
return x,y
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
for i in range(27,670,44):
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
pygame.draw.circle(screen, line_color,[27+44*7,27+44*7], 8,0)
x,y = pygame.mouse.get_pos()
x,y=find_pos(x,y)
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
pygame.display.update()
运行结果如下:
这里直接手机拍照了,不太好截屏。。。。。
可以看到鼠标移动到哪儿就会有个对应的正方形光圈,表示这个地方可以落子。
现在就是落子了,这里就简单了,因为我们已经得到了具体的位置(x,y),获取鼠标左键信息,如果按下左键就落子,然后用画圆的函数在这个位置画圆。
抛出代码如下:
keys_pressed = pygame.mouse.get_pressed()
if keys_pressed[0]==True:
print('表示按下了鼠标左键')
因为我们落子后这个位置就不能再落子,所以是需要储存下已经落子的位置,定义over_pos表示已经落子的位置。
代码如下:
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]
line_color = [0,0,0]
def find_pos(x,y):
for i in range(27,670,44):
for j in range(27,670,44):
L1=i-22
L2=i+22
R1=j-22
R2=j+22
if x>=L1 and x<=L2 and y>=R1 and y<=R2:
return i,j
return x,y
def check_over_pos(x,y,over_pos):
for val in over_pos:
if val[0][0]==x and val[0][1]==y:
return False
return True
flag=False
tim=0
over_pos=[]
white_color=[255,255,255]
black_color=[0,0,0]
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
for i in range(27,670,44):
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
pygame.draw.circle(screen, line_color,[27+44*7,27+44*7], 8,0)
x,y = pygame.mouse.get_pos()
x,y=find_pos(x,y)
if check_over_pos(x,y,over_pos):
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
keys_pressed = pygame.mouse.get_pressed()
if keys_pressed[0] and tim==0:
flag=True
if check_over_pos(x,y,over_pos):
if len(over_pos)%2==0:
over_pos.append([[x,y],black_color])
else:
over_pos.append([[x,y],white_color])
if flag:
tim+=1
if tim%200==0:
flag=False
tim=0
for val in over_pos:
pygame.draw.circle(screen, val[1],val[0], 20,0)
pygame.display.update()
运行效果如下: 现在就差个判断函数,判断五子连心。
def check_win(over_pos):
mp=np.zeros([15,15],dtype=int)
for val in over_pos:
x=int((val[0][0]-27)/44)
y=int((val[0][1]-27)/44)
if val[1]==white_color:
mp[x][y]=2
else:
mp[x][y]=1
for i in range(15):
pos1=[]
pos2=[]
for j in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for j in range(15):
pos1=[]
pos2=[]
for i in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j+k>=15:
break
if mp[i+k][j+k]==1:
pos1.append([i+k,j+k])
else:
pos1=[]
if mp[i+k][j+k]==2:
pos2.append([i+k,j+k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j-k<0:
break
if mp[i+k][j-k]==1:
pos1.append([i+k,j-k])
else:
pos1=[]
if mp[i+k][j-k]==2:
pos2.append([i+k,j-k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
return [0,[]]
上面代码返回五子连心的坐标。
四、完整代码
import pygame
import sys
from pygame.locals import QUIT,KEYDOWN
import numpy as np
pygame.init()
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]
line_color = [0,0,0]
def check_win(over_pos):
mp=np.zeros([15,15],dtype=int)
for val in over_pos:
x=int((val[0][0]-27)/44)
y=int((val[0][1]-27)/44)
if val[1]==white_color:
mp[x][y]=2
else:
mp[x][y]=1
for i in range(15):
pos1=[]
pos2=[]
for j in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for j in range(15):
pos1=[]
pos2=[]
for i in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j+k>=15:
break
if mp[i+k][j+k]==1:
pos1.append([i+k,j+k])
else:
pos1=[]
if mp[i+k][j+k]==2:
pos2.append([i+k,j+k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j-k<0:
break
if mp[i+k][j-k]==1:
pos1.append([i+k,j-k])
else:
pos1=[]
if mp[i+k][j-k]==2:
pos2.append([i+k,j-k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
return [0,[]]
def find_pos(x,y):
for i in range(27,670,44):
for j in range(27,670,44):
L1=i-22
L2=i+22
R1=j-22
R2=j+22
if x>=L1 and x<=L2 and y>=R1 and y<=R2:
return i,j
return x,y
def check_over_pos(x,y,over_pos):
for val in over_pos:
if val[0][0]==x and val[0][1]==y:
return False
return True
flag=False
tim=0
over_pos=[]
white_color=[255,255,255]
black_color=[0,0,0]
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)
for i in range(27,670,44):
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
if i==27 or i==670-27:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
pygame.draw.circle(screen, line_color,[27+44*7,27+44*7], 8,0)
for val in over_pos:
pygame.draw.circle(screen, val[1],val[0], 20,0)
res=check_win(over_pos)
if res[0]!=0:
for pos in res[1]:
pygame.draw.rect(screen,[238,48,167],[pos[0]*44+27-22,pos[1]*44+27-22,44,44],2,1)
pygame.display.update()
continue
x,y = pygame.mouse.get_pos()
x,y=find_pos(x,y)
if check_over_pos(x,y,over_pos):
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
keys_pressed = pygame.mouse.get_pressed()
if keys_pressed[0] and tim==0:
flag=True
if check_over_pos(x,y,over_pos):
if len(over_pos)%2==0:
over_pos.append([[x,y],black_color])
else:
over_pos.append([[x,y],white_color])
if flag:
tim+=1
if tim%50==0:
flag=False
tim=0
pygame.display.update()
因为没有添加AI对战,所以只能两个人轮流对战,后面空闲了会设计一个AI加入里面。
希望我的分享对你的学习有所帮助,如果有问题请及时指出,谢谢~
|