#---第1步---导出模块--- import numpy as np import pygame import sys import traceback import copy from pygame.locals import * #---第2步---定义颜色--- bg=(240,255,240) #背景颜色=蜜露色,bg=background cb=(0,100,0) #cb=checkerboard=棋盘网格线颜色,darkgreen bc=(148,0,211) #按钮颜色=暗紫色,bc=buttoncolor lz=(0,0,0) #lz=落子文字颜色设置=black=黑色 dwd=(255,0,0) #棋盘中间的定位点颜色,dwd=定位点=red #---第3步---游戏初始化--- pygame.init() #用于控制顺序 t=True #用于结束游戏后阻止落子 running=True #---第4步---绘制棋盘--- def Draw_a_chessboard(screen): screen.fill(bg) #填充背景色=蜜露色,不耀眼 #画棋盘 for i in range(21): pygame.draw.line(screen, cb, (40*i+3, 3), (40*i+3, 803)) pygame.draw.line(screen, cb, (3, 40*i+3), (803, 40*i+3)) #画边线,四条边 pygame.draw.line(screen, cb, (3, 3), (803, 3),5) pygame.draw.line(screen, cb, (3, 3), (3, 803),5) pygame.draw.line(screen, cb, (803, 3), (803, 803),5) pygame.draw.line(screen, cb, (3, 803), (803, 803),5) #画定位点,dwd是red颜色,6为大小 pygame.draw.circle(screen, dwd, (163, 163), 6) pygame.draw.circle(screen, dwd, (163, 643), 6) pygame.draw.circle(screen, dwd, (643, 163), 6) pygame.draw.circle(screen, dwd, (643, 643), 6) pygame.draw.circle(screen, dwd, (403, 403), 6) #画‘悔棋按钮’、‘重新开始’和‘退出’按钮 #900=x,350,500,650=y,200和100是按钮框大小 pygame.draw.rect(screen,bc,[900,350,200,100],15) pygame.draw.rect(screen,bc,[900,500,200,100],15) pygame.draw.rect(screen,bc,[900,650,200,100],15) #字体是自己下载的hwfs=华文仿宋,放在根目录下 s_font=pygame.font.Font('hwfs.ttf',40) #按钮定义文字、颜色和位置 text1=s_font.render("悔棋按钮",True,bc) text2=s_font.render("重新开始",True,bc) text3=s_font.render("退出游戏",True,bc) screen.blit(text1,(920,370)) screen.blit(text2,(920,520)) screen.blit(text3,(920,670)) #---第5步---绘制棋子--- # (横坐标,纵坐标,屏幕,棋子颜色(1代表黑,2代表白)) def Draw_a_chessman(x,y,screen,color): #注意b.png=黑子,w.png=白子,大小=50×50,968B,可惜是正方形,不是圆形 #也是放在根目录下,我把图片也上传了,最好用改图软件修改成圆形,就完美了 if color==1: Black_chess=pygame.image.load("b.png").convert_alpha() screen.blit(Black_chess,(40*x+3-15,40*y+3-15)) if color==2: White_chess=pygame.image.load("w.png").convert_alpha() screen.blit(White_chess,(40*x+3-15,40*y+3-15)) #---第6步---绘制带有棋子的棋盘--- def Draw_a_chessboard_with_chessman(map,screen): screen.fill(bg) Draw_a_chessboard(screen) for i in range(24): for j in range(24): Draw_a_chessman(i+1,j+1,screen,map[i][j]) #---第7步---定义存储棋盘的列表,大一点没关系--- map=[] for i in range(24): #map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) #等同于下面的 map.append([0]*24) #---第8步---清零map列表--- def clear(): global map for i in range(24): for j in range(24): map[i][j]=0 #---第9步---判断是否胜利--- #---这是关键--- def win(i, j): k = map[i][j] p=[] for a in range(20): p.append(0) for i3 in range(i-4,i+5): for j3 in range(j-4,j+5): if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j): p[0]+=1 if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j): p[1]+=1 if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j): p[2]+=1 if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j): p[3]+=1 if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j): p[4]+=1 if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j): p[5]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j): p[6]+=1 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j): p[7]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[8]+=1 if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[9]+=1 if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[10]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[11]+=1 if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[12]+=1 if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[13]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3): p[14]+=1 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3): p[15]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[16]+=1 if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[17]+=1 if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[18]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[19]+=1 for b in range(20): if p[b]==5: return True return False #---第10步---提示器设置和显示文字 def text(s,screen,x): pygame.draw.rect(screen,bg,[850,100,1200,100]) s_font=pygame.font.Font('hwfs.ttf',x) #显示落子的文字提示:黑棋或白棋落子,及颜色lz=黑色 s_text=s_font.render(s,True,lz) screen.blit(s_text,(880,100)) #是不断刷新的,没点一次白子和黑子交替显示 pygame.display.flip() #---第11步---主函数--- def main(): #全局变量:t和running定义引用过来 #map和maps 前面已定义棋盘列表---用来悔棋存储 global t,map,running,maps clear() map2=copy.deepcopy(map) maps=[map2] #窗口大小设置宽1400×高900 screen = pygame.display.set_mode([1400,900]) #窗口标题 pygame.display.set_caption("五子棋v1.0") Draw_a_chessboard(screen) pygame.display.flip() clock=pygame.time.Clock() while True: #只有running为真才能落子,主要用于游戏结束后防止再次落子 if running: if t: color=1 text('黑棋落子',screen,54) else: color=2 text('白棋落子',screen,54) for event in pygame.event.get(): #在pygame中的while循环中这一步我觉得必须的 if event.type ==pygame.QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: if event.button == 1: x,y=event.pos[0],event.pos[1] for i in range(19): for j in range(19): #点击棋盘相应位置 if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running: #在棋盘相应位置落相应颜色棋子 Draw_a_chessman(i+1,j+1,screen,color) map[i][j]=color map3=copy.deepcopy(map) maps.append(map3) #判断落子后是否有五子一线 if win(i,j): if t: text('黑棋victory,请重新游戏',screen,40) else: text('白棋victory,请重新游戏',screen,40) #阻止再往棋盘落子 running=False pygame.display.flip() t=not t #如果点击‘重新开始’ if 900<x<1100 and 500<y<600: running=True main() #点击‘退出游戏’ elif 900<x<1100 and 650<y<750: pygame.quit() sys.exit() #点击‘悔棋按钮’ elif 900<x<1020 and 350<y<450 and len(maps)!=1: del maps[len(maps)-1] map=copy.deepcopy(maps[len(maps)-1]) #切换顺序 t=not t Draw_a_chessboard_with_chessman(map,screen) #悔棋完成,全部为0就阻止再次悔棋 x,y=0,0 clock.tick(60) #---第12步---模块main--- #当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。 #即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行 if __name__ == "__main__": main()