毕设题目是用python做跳棋游戏,可怜我只找到了用python做的五子棋和用java做的跳棋,两边结合写出了python棋盘。
(小声叨叨:老师明明说网上可以找到…)
话不多说,上代码。
#数据定义
BOARDSIZE=600
CEN=BOARDSIZE/2
EDGE=30
RADIUS=CEN-EDGE
RATIO=0.87
HEIGHT=RADIUS/2
SIDE=HEIGHT/RATIO
EACHHEIGHT=RADIUS/8
EACHWIDTH=SIDE/8
RA=5
#行列坐标转化成像素坐标
def rcToXy(rc):
xy=[0,0]
xy[0]=(rc[0]-12)*EACHWIDTH+CEN
xy[1]=rc[1]*EACHHEIGHT+EDGE
return xy
#画线
def drawLine(start_rc,end_rc):
start_xy=rcToXy(start_rc)
end_xy=rcToXy(end_rc)
pygame.draw.aaline(screen,BLACK,start_xy,end_xy,1)
def boardPaint():
#背景色
screen.fill(WHITE)
#画不同区域,三角形从顶端开始顺时针排列
tan=[]
tan.append([(CEN-SIDE/2,EDGE+HEIGHT),(CEN+SIDE/2,EDGE+HEIGHT),(CEN+SIDE,CEN),(CEN+SIDE/2,BOARDSIZE-EDGE-HEIGHT),(CEN-SIDE/2,BOARDSIZE-EDGE-HEIGHT),(CEN-SIDE,CEN)]) #0 六边形
tan.append([(CEN,EDGE),(CEN-SIDE/2,EDGE+HEIGHT),(CEN+SIDE/2,EDGE+HEIGHT)]) #1
tan.append([(CEN+SIDE*3/2,EDGE+HEIGHT),(CEN+SIDE/2,EDGE+HEIGHT),(CEN+SIDE,CEN)]) #2
tan.append([(CEN+SIDE*3/2,BOARDSIZE-EDGE-HEIGHT),(CEN+SIDE,CEN),(CEN+SIDE/2,BOARDSIZE-EDGE-HEIGHT)]) #3
tan.append([(CEN,BOARDSIZE-EDGE),(CEN+SIDE/2,BOARDSIZE-EDGE-HEIGHT),(CEN-SIDE/2,BOARDSIZE-EDGE-HEIGHT)]) #4
tan.append([(CEN-SIDE*3/2,BOARDSIZE-EDGE-HEIGHT),(CEN-SIDE/2,BOARDSIZE-EDGE-HEIGHT),(CEN-SIDE,CEN)]) #5
tan.append([(CEN-SIDE*3/2,EDGE+HEIGHT),(CEN-SIDE,CEN),(CEN-SIDE/2,EDGE+HEIGHT)]) #6
#上色
for i in range(0,4):
#六边形填色
if i==0:
COLOR=YELLOW
pygame.draw.polygon(screen,COLOR,tan[i],0)
#三角形填色
else:
if i==1:
COLOR=GREEN
elif i==2:
COLOR=BLUE
elif i==3:
COLOR=RED
pygame.draw.polygon(screen,COLOR,tan[i],0)
pygame.draw.polygon(screen,COLOR,tan[i+3],0)
#画线
#1
start_rc=[12,0]
end_rc=[12,0]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=1
start_rc[1]+=1
end_rc[0]+=1
end_rc[1]+=1
#六边形上
start_rc=[0,4]
end_rc=[24,4]
for each in range(0,5):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=1
start_rc[1]+=1
end_rc[0]-=1
end_rc[1]+=1
#六边形下
start_rc=[4,8]
end_rc=[20,8]
for each in range(0,5):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=1
start_rc[1]+=1
end_rc[0]+=1
end_rc[1]+=1
#4
start_rc=[9,13]
end_rc=[15,13]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=1
start_rc[1]+=1
end_rc[0]-=1
end_rc[1]+=1
#2
start_rc=[24,4]
end_rc=[24,4]
for each in range(0,5):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=2
end_rc[0]-=1
end_rc[1]+=1
#右上
start_rc=[12,0]
end_rc=[24,12]
for each in range(0,5):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=1
start_rc[1]+=1
end_rc[0]-=2
#左下
start_rc=[6,4]
end_rc=[15,13]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=2
end_rc[0]-=1
end_rc[1]+=1
#5
start_rc=[3,9]
end_rc=[6,12]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]-=1
start_rc[1]+=1
end_rc[0]-=2
#6
start_rc=[0,4]
end_rc=[0,4]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=2
end_rc[0]+=1
end_rc[1]+=1
#左上
start_rc=[0,12]
end_rc=[12,0]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=2
end_rc[0]+=1
end_rc[1]+=1
#右下
start_rc=[8,12]
end_rc=[16,4]
for each in range(0,5):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=1
start_rc[1]+=1
end_rc[0]+=2
#3
start_rc=[21,9]
end_rc=[18,12]
for each in range(0,4):
drawLine(start_rc,end_rc)
#行列变换
start_rc[0]+=1
start_rc[1]+=1
end_rc[0]+=2
#画点
for row in range(0,17):
for col in range(POS[row][0],POS[row][1]+1,2):
cen_xy=rcToXy((col,row))
pygame.draw.circle(screen,BLACK,(int(cen_xy[0]),int(cen_xy[1])),int(2*RA),0)
pygame.display.flip()
小菜鸟写的代码,有一些冗余,我也没有处理,毕竟核心是要实现人机对战,后期有余力再做调整。