1 import pygame 2 from pygame.locals import * 3 pygame.init()#导入,初始化 4 #-------------------------------------一些初始化的东西 5 ScreenSize=(800,800) 6 font=pygame.font.SysFont(‘ubuntumono‘,30)#这个字体建议改成自己有的例如下面: 7 #font=pygame.font.Font(‘字体路径/msyh.ttf‘,30) 8 #font=pygame.font.SysFont(‘consolas‘,30) 9 10 bw=font.render(‘black is win!‘,True,(0,0,0))#黑棋胜利白旗胜利black_win,write_win 11 ww=font.render(‘write is win!‘,True,(255,255,255)) 12 13 shuoming=font.render(‘press o to restart!‘,True,(0,255,0))#按下o键,重新开始游戏 14 15 smb=font.render(‘is time!‘,True,(0,0,0))#说明黑,说明白,就是该谁走了,花哨一点 16 smw=font.render(‘is time!‘,True,(255,255,255)) 17 18 screen=pygame.display.set_mode(ScreenSize,0,32) 19 20 21 #--------------------------------------一些功能,落子,判定输赢 22 23 #棋子,items【0】是黑棋棋子,items【1】是白棋棋子,例如items[0].append([x,y,0]),就是在x,y处下了一个黑棋 24 items=[[],[]] 25 player=0#该谁走了,0是黑,1是白。当前下棋人。 26 27 def Plane(x,y,linesNum,interval,lineWidth=2):#画五子棋棋盘,x,y是棋盘左上角坐标,linesnum是行数,interval是行间距,linewidth是线宽 28 mx=x 29 my=y 30 w=mx+(linesNum-1)*interval 31 h=my+(linesNum-1)*interval 32 for i in range(linesNum): 33 pygame.draw.line(screen,(90,140,26),(mx,my),(w,my),lineWidth) 34 my+=interval 35 for i in range(linesNum): 36 pygame.draw.line(screen,(90,140,26),(x,y),(x,h),lineWidth) 37 x+=interval 38 39 def JudgePos(x,y):#这个是判定落点,根据鼠标位置判定落到哪个交点上。ms,x,z,y是上下左右。正方形内某个点距离四周多少来判断落到哪个点。 40 ms,mx,mz,my=0,0,0,0 41 ms=y%50 42 mx=50-ms 43 mz=x%50 44 my=50-mz 45 if ms<=mx and mz>=my: 46 return (x//50*50+50,y//50*50) 47 if ms<=mx and mz<my: 48 return (x//50*50,y//50*50) 49 if ms>mx and mz>=my: 50 return (x//50*50+50,y//50*50+50) 51 if ms>mx and mz<my: 52 return (x//50*50,y//50*50+50) 53 def DumpHand():#落子,就是把items里的画一遍 54 if len(items)!=0: 55 for li in items: 56 for i in li: 57 if i[2]==0: 58 pygame.draw.circle(screen,(0,0,0),(i[0],i[1]),22) 59 if i[2]==1: 60 pygame.draw.circle(screen,(255,255,255),(i[0],i[1]),22) 61 else: 62 pass 63 64 def Jugg1(x,y,pl):#jugg1~4是五子棋四种胜利方式:横,竖,左斜,右斜 65 tem=1 66 for i in range(1,5): 67 if [x+i*50,y,pl] in items[pl]: 68 tem+=1 69 if tem==5: 70 return 1 71 else : 72 return 0 73 def Jugg2(x,y,pl): 74 tem=1 75 for i in range(1,5): 76 if [x+i*50,y+i*50,pl] in items[pl]: 77 tem+=1 78 if tem==5: 79 return 1 80 else : 81 return 0 82 def Jugg3(x,y,pl): 83 tem=1 84 for i in range(1,5): 85 if [x,y+i*50,pl] in items[pl]: 86 tem+=1 87 if tem==5: 88 return 1 89 else : 90 return 0 91 def Jugg4(x,y,pl): 92 tem=1 93 for i in range(1,5): 94 if [x-i*50,y+i*50,pl] in items[pl]: 95 tem+=1 96 if tem==5: 97 return 1 98 else : 99 return 0 100 101 def Judge():#把上面四个用一下,这里jugg几个是我为了省事写的。判断五子棋输赢用回溯也可以反正能用就行 102 for li in items: 103 for i in li: 104 if Jugg1(i[0],i[1],i[2]): 105 return 1 106 if Jugg2(i[0],i[1],i[2]): 107 return 1 108 if Jugg3(i[0],i[1],i[2]): 109 return 1 110 if Jugg4(i[0],i[1],i[2]): 111 return 1 112 113 114 #------------------------------开始主体 115 116 x,y=-100,-100#int一个坐标 117 screen.fill((50,120,99))#初始化屏幕,打印棋盘 118 Plane(50,50,(ScreenSize[0]-100)//50,50) 119 120 scene=0#控制场景,我就写了两个。0是游戏,1是胜利。 121 122 123 124 while 1:#一个循环让游戏不断进行,用scene进行场景控制 125 126 if scene==0: 127 while 1: 128 for e in pygame.event.get(): 129 if e.type==MOUSEBUTTONDOWN: 130 if e.button==1: 131 x,y=e.pos 132 x,y=JudgePos(x,y)#落子并且不能覆盖之前的棋子 133 if [x,y,0] not in items[0] and [x,y,1] not in items[1]: 134 items[player].append([x,y,player]) 135 player+=1#换人走了 136 if player>1: 137 player=0 138 139 140 #该谁走了,其实没啥用 141 if player==1: 142 pygame.draw.circle(screen,(255,255,255),(200,25),22) 143 screen.blit(smw,(130,12)) 144 else: 145 pygame.draw.circle(screen,(0,0,0),(200,25),22) 146 screen.blit(smb,(130,12)) 147 148 #画棋子,blit可以渲染一小部分,或者surface的设置剪辑set_clip()来优化,我直接全画一遍 149 DumpHand() 150 151 #判断输赢 152 if Judge(): 153 scene=1 154 break 155 156 pygame.display.update() 157 158 if scene==1: 159 while 1: 160 161 screen.fill((50,120,99)) 162 #一些说明 163 if player==0: 164 screen.blit(ww,(50,50)) 165 else: 166 screen.blit(bw,(50,50)) 167 screen.blit(shuoming,(350,150)) 168 #按下o重新开始 169 for e in pygame.event.get(): 170 if e.type==KEYDOWN: 171 if e.key==K_o: 172 screen.fill((50,120,99)) 173 items=[[],[]] 174 Plane(50,50,(ScreenSize[0]-100)//50,50) 175 scene=0 176 if scene==0: 177 break 178 pygame.display.update()
原文:https://www.cnblogs.com/fluentsc/p/13193445.html