zoukankan      html  css  js  c++  java
  • python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246.html
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

    本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6246/
    1.五子棋

    python3的pygame的五子棋布局设置和代码详细分析

    2.今天来讲解五子棋的python3用pygame设置,注意黑子和白子的大小,本文中50×50,968B

    图如下:小bug:是正方形,不是圆形,可以自己改一改玩。

    python3的pygame的五子棋布局设置和代码详细分析

    黑子

    python3的pygame的五子棋布局设置和代码详细分析

    白子

    =======================

    ★详细讲解,代码里有注释★

    =======================

    3.python的pygame格式基本固定,第1步:

    #---第1步---导出模块---  
    import numpy as np  
    import pygame  
    import sys  
    import traceback  
    import copy  
    from pygame.locals import *
    

    4.第2步:

    #---第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
    

    5.第3步:

    #---第3步---游戏初始化---  
    pygame.init()  
    #用于控制顺序  
    t=True  
    #用于结束游戏后阻止落子  
    running=True
    

    6.第4步:

    #---第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))
    

    7.第5步:

    #---第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))
    

    8.第6步:

    #---第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])
    

    9.第7步:有一个知识点:列表和重复列表的表示方法

    #---第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)
    

    10.第8步:

    #---第8步---清零map列表---  
    def clear():  
        global map  
        for i in range(24):  
            for j in range(24):  
                map[i][j]=0
    

    11.第9步:

    #---第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
    

    12.第10步:

    #---第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()
    

    13.第11步:

    #---第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)
    

    14.第12步:python的代码以下的格式解读和分析

    当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。

    即:选择性地执行代码,作为模块被导入时不能运行;只有作为脚本文件直接被运行时才运行

    #---第12步---模块main---  
    if __name__ == "__main__":  
        main()  
    
    

    15.效果图

    python3的pygame的五子棋布局设置和代码详细分析

  • 相关阅读:
    django模板使用
    django视图的定义
    字符串逆序
    Django 中Admin站点的配置
    Django模型Model的定义
    Django安装部署
    Linux常用命令
    深拷贝,浅拷贝
    lambda 表达式 (匿名函数)
    生成器与迭代器
  • 原文地址:https://www.cnblogs.com/lihanlin/p/12571855.html
Copyright © 2011-2022 走看看