前言520后,来玩五子棋陶冶情操。拿着这个和你女朋友一起去巷子里。(分手后不要来找我)。话不多说,直接进入正题。游戏规则:p1为黑子,p2为白子,黑子先走,五颗棋子连在一起,一方获胜。动态演示源码分享cheapboard.py定义了黑白棋子、落子位置和获胜规则。fromcollectionsimportnamedtupleChessman=namedtuple('Chessman','NameValueColor')Point=namedtuple('Point','XY')BLACK_CHESSMAN=Chessman('黑子',1,(45,45,45))WHITE_CHESSMAN=Chessman('白子',2,(219,219,219))offset=[(1,0),(0,1),(1,1),(1,-1)]classCheckerboard:def__init__(self,line_points):self._line_points=line_pointsself._checkerboard=[[0]*line_pointsfor_inrange(line_points)]def_get_checkerboard(self):returnself._checkerboardcheckerboard=property(_get_checkerboard)#判断是否可以放置defcan_drop(self,point):returnself._checkerboard[point.Y][point.X]==0defdrop(self,chessman,point):"""chessman:paramchessman::parampoint:positionofchessman:return:如果孩子落下后能赢,则返回赢家,否则返回None"""print(f'{chessman.Name}({point.X},{point.Y})')self._checkerboard[point.Y][point.X]=chessman.Value如果self._win(point):print(f'{chessman.Name}获胜')returnchessman#判断是否赢了def_win(self,point):cur_value=self._checkerboard[point.Y][point.X]forosinoffset:ifself._get_count_on_direction(point,cur_value,os[0],os[1]):returnTruedef_get_count_on_direction(self,point,value,x_offset,y_offset):count=1forstepinrange(1,5):x=point.X+step*x_offsety=point.Y+step*y_offsetif0<=x=5Everyonevs.pyimportmodule如果出现模块错误,在pycharm终端输入如下命令pipinstall对应模块-ihttps://pypi.douban.com/simpleimportsysimportpygamefrompygame.localsimport*importpygame.gfxdrawfromminigame.backgammon.checkerboardimportCheckerboard,BLACK_CHESSMAN,WHITE_CHESSMAN,Point设置棋盘和棋子参数SIZE=30#棋盘每个点的时间间隔Line_Points=19#棋盘每行/列的点数Outer_Width=20#棋盘的外宽度Border_Width=4#边框的宽度Inside_Width=4#间隔边框与实际棋盘之间Border_Length=SIZE*(Line_Points-1)+Inside_Width*2+Border_Width#边框线的长度Start_X=Start_Y=Outer_Width+int(Border_Width/2)+Inside_Width#网格线起点(左上角)角)坐标SCREEN_HEIGHT=SIZE*(Line_Points-1)+Outer_Width*2+Border_Width+Inside_Width*2#游戏屏幕高度SCREEN_WIDTH=SCREEN_HEIGHT+200#游戏屏幕宽度Stone_Radius=SIZE//2-3#方格半径Stone_Radius2=SIZE//2+3Checkerboard_Color=(0xE3,0x92,0x65)#棋盘颜色BLACK_COLOR=(0,0,0)WHITE_COLOR=(255,255,255)RED_COLOR=(200,30,30)BLUE_COLOR=(30,30,200)RIGHT_INFO_POS_X=SCREEN_HEIGHT+Stone_Radius2*2+10个内部字体集defprint_text(screen,字体,x,y,文本,fcolor=(255,255,255)):imgText=font.render(text,True,fcolor)screen.blit(imgText,(x,y))defmain():pygame.init()screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))pygame.display.set_caption('五子棋')font1=pygame.font.SysFont('SimHei',32)font2=pygame.font.SysFont('SimHei',72)fwidth,fheight=font2.size('黑方赢')checkerboard=Checkerboard(Line_Points)cur_runner=BLACK_CHESSMANwinner=Nonecomputer=AI(Line_Points,WHITE_CHESSMAN)black_win_count=0white_win_count=0落子失败体whileTrue:foreventinpygame.event.get():ifevent.type==QUIT:sys.exit()elifevent.type==KEYDOWN:ifevent.key==K_RETURN:如果获胜者不是无:winner=Nonecur_runner=BLACK_CHESSMANcheckerboard=Checkerboard(Line_Points)computer=AI(Line_Points,WHITE_CHESSMAN)elifevent.type==MOUSEBUTTONDOWN:如果获胜者为None:pressed_array=pygame.mouse.get_pressed()如果pressed_array[0]:mouse_pos=pygame.mouse.get_pos()click_point=_get_clickpoint(mouse_pos)如果click_point不是None:如果checkerboard.can_drop(click_point):winner=checkerboard.drop(cur_runner,click_point)如果winner是None:cur_runner=_get_next(cur_runner)computer.get_opponent_drop(click_point)AI_point=computer.AI_drop()赢家=checkerboard.drop(cur_runner,AI_point)如果获胜者不是None:white_win_count+=1cur_runner=_get_next(cur_runner)else:black_win_count+=1else:print('outofcheckerboardarea')drawcheckerboarddef_draw_checkerboard(screen):#fillcheckerboardbackgroundcolor(Colorard.fillscreen)#在棋盘的网格线外绘制边框pygame.draw.rect(screen,BLACK_COLOR,(Outer_Width,Outer_Width,Border_Length,Border_Length),Border_Width)#绘制网格线foriinrange(Line_Points):pygame.draw.line(screen,BLACK_COLOR,(Start_Y,Start_Y+SIZE*i),(Start_Y+SIZE*(Line_Points-1),Start_Y+SIZE*i),1)forjinrange(Line_Points):pygame.draw.line(屏幕,BLACK_COLOR,(Start_X+SIZE*j,Start_X),(Start_X+SIZE*j,Start_X+SIZE*(Line_Points-1)),1)#画星位和天元foriin(3,9,15):forjin(3,9,15):ifi==j==9:radius=5else:radius=3#pygame.draw.circle(screen,BLACK,(Start_X+SIZE*i,Start_Y+SIZE*j),radius)pygame.gfxdraw.aacircle(screen,Start_X+SIZE*i,Start_Y+SIZE*j,radius,BLACK_COLOR)pygame.gfxdraw.filled_circle(screen,Start_X+SIZE*i,Start_Y+SIZE*j,radius,BLACK_COLOR)画棋子def_draw_chessman(screen,point,stone_color):#pygame.draw.圆(屏幕,stone_color,(Start_X+SIZE*point.X,Start_Y+SIZE*point.Y),Stone_Radius)pygame.gfxdraw.aacircle(屏幕,Start_X+SIZE*point.X,Start_Y+SIZE*point.Y,Stone_Radius,stone_color)pygame.gfxdraw.filled_circle(screen,Start_X+SIZE*point.X,Start_Y+SIZE*point.Y,Stone_Radius,stone_color)def_draw_chessman_pos(screen,pos,stone_color):pygame.gfxdraw.aacircle(screen,pos[0],pos[1],Stone_Radius2,stone_color)pygame.gfxdraw.filled_circle(screen,pos[0],pos[1],Stone_Radius2,stone_color)运行框返回点击点坐标def_get_clickpoint(click_pos):pos_x=click_pos[0]-Start_Xpos_y=click_pos[1]-Start_Yifpos_x<-Inside_Widthorpos_y<-Inside_Width:returnNonex=pos_x//SIZEy=pos_y//SIZEifpos_x%SIZE>Stone_Radius:x+=1ifpos_y%SIZE>Stone_Radius:y+=1ifx>=Line_Pointsory>=Line_Points:returnNonereturnPoint(x,y)executefileif__name__=='__main__':main()人机对战动态演示如果觉得文章有趣,可以前往公众号【Python编程学习圈】学习更多有趣的干货技术,还有很多学习资料免费提供,欢迎了解!