Layout and code analysis of Chinese Gobang in pygame of Python 3

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Today, I will explain the python3 of Gobang with pygame settings. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---Step 10: prompt setting and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of the chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

This article refers to the original- http://bjbsair.com/2020-03-25/tech-info/6246/ 1. Gobang

2. Let's talk about the python3 setting of Gobang with pygame. Pay attention to the size of black and white. In this article, 50 × 50968b

The picture is as follows: bug: it's a square, not a circle. You can change and play by yourself.

sunspot

White seed

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

★ detailed explanation, comments in the code ★

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

3.python's pygame format is basically fixed. Step 1:

#---Step 1 - export module---  
import numpy as np  
import pygame  
import sys  
import traceback  
import copy  
from pygame.locals import *

4. second steps:

#---Step 2 - define color---  
bg=(240,255,240)  #Background color = honeydew color, bg=background    
cb=(0,100,0)  #cb=checkerboard = checkerboard gridline color, darkgreen  
bc=(148,0,211)  #Button color = dark purple, bc=buttoncolor  
lz=(0,0,0)  #lz = drop text color setting = black = black  
dwd=(255,0,0)  #Color of locating point in the middle of chessboard, dwd = locating point = red

5. third steps:

#---Step 3 - game initialization---  
pygame.init()  
#Used to control sequence  
t=True  
#Used to stop falling after ending the game  
running=True

6. fourth steps:

#---Step 4 - draw chessboard---  
def Draw_a_chessboard(screen):    
    screen.fill(bg) #Fill background color = honeydew color, not dazzling  
    #Chessboard  
    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))  
    #Draw four edges  
    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)   
    #Draw anchor point, dwd is red color, 6 is size  
    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)   
    #Draw "regret", "restart" and "exit" buttons  
    #900 = x, 350500650 = y, 200 and 100 are button box sizes  
    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)  
    #The font is hwfs = Chinese imitation song, put it in the root directory  
    s_font=pygame.font.Font('hwfs.ttf',40)  
  
    #Buttons define text, color, and location  
    text1=s_font.render("Regret button",True,bc)  
    text2=s_font.render("Restart",True,bc)  
    text3=s_font.render("Quit game",True,bc)  
    screen.blit(text1,(920,370))  
    screen.blit(text2,(920,520))  
    screen.blit(text3,(920,670))

7. fifth steps:

#---Step 5 - draw chess pieces---  
# (abscissa, ordinate, screen, chess color (1 for black, 2 for white))  
def Draw_a_chessman(x,y,screen,color):   
    #Note that b.png = black, w.png = white, size = 50 × 50968b, but it's a square, not a circle  
    #It's also placed in the root directory. I uploaded the pictures. It's better to use the map modification software to change it into a circle. It's perfect  
    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. sixth steps:

#---Step 6 - draw a board with pieces---  
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. Step 7: there is a knowledge point: representation of list and repeating list

#---Step 7: define the list of stored chessboards. It doesn't matter if it's a little bigger---  
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])  
    #Equivalent to  
    map.append([0]*24)

10. Step 8:

#---Step 8 - clear map list---  
def clear():  
    global map  
    for i in range(24):  
        for j in range(24):  
            map[i][j]=0

11. Step 9:

#---Step 9 - judge whether to win---  
#---This is the key---  
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. Step 10:

#---The tenth step --- prompt settings and display text  
def text(s,screen,x):  
    pygame.draw.rect(screen,bg,[850,100,1200,100])  
    s_font=pygame.font.Font('hwfs.ttf',x)  
    #Display the text prompt of drop: black or white drop, and color lz = black  
    s_text=s_font.render(s,True,lz)  
    screen.blit(s_text,(880,100))  
    #It's constantly refreshing. It doesn't show white and black alternately  
    pygame.display.flip()

13. Step 11:

#---Step 11 - main function---  
def main():  
    #Global variables: t and running definitions are referenced  
    #The checkerboard list has been defined in front of map and maps -- used for repentance storage  
    global t,map,running,maps  
    clear()  
    map2=copy.deepcopy(map)  
    maps=[map2]  
    #Window size setting width 1400 × height 900  
    screen = pygame.display.set_mode([1400,900])  
    #Window title  
    pygame.display.set_caption("Gobang v1.0")  
    Draw_a_chessboard(screen)  
    pygame.display.flip()  
    clock=pygame.time.Clock()  
    while True:  
        #Only when running is true can you stop playing. It is mainly used to prevent falling again at the end of the game  
        if running:  
            if t:  
                color=1  
                text('Black chess',screen,54)  
            else:  
                color=2  
                text('White chess',screen,54)  
          
        for event in pygame.event.get():  
            #In the while loop of pygame, I think this step is necessary  
            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):  
                            #Click the corresponding position of chessboard  
                            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:  
                                #Drop the corresponding color pieces on the corresponding position of the chessboard  
                                Draw_a_chessman(i+1,j+1,screen,color)  
                                map[i][j]=color  
                                map3=copy.deepcopy(map)  
                                maps.append(map3)  
                                #Judge whether there are five sub lines after the settlement  
                                if win(i,j):  
                                    if t:  
                                        text('black victory,Please play again',screen,40)  
                                    else:  
                                        text('White chess victory,Please play again',screen,40)  
                                    #Stop falling on the board again  
                                    running=False  
                                pygame.display.flip()  
                                t=not t  
                    #If you click "restart"  
                    if 900<x<1100 and 500<y<600:  
                        running=True  
                        main()  
                    #Click "exit game"  
                    elif 900<x<1100 and 650<y<750:  
                        pygame.quit()  
                        sys.exit()  
                    #Click "repentance"  
                    elif 900<x<1020 and 350<y<450 and len(maps)!=1:  
                        del maps[len(maps)-1]   
                        map=copy.deepcopy(maps[len(maps)-1])  
                        #relay sequence  
                        t=not t  
                        Draw_a_chessboard_with_chessman(map,screen)  
                        #Repentance is completed, if all are 0, then repentance will be prevented again  
                        x,y=0,0  
        clock.tick(60)

14. Step 12: the following format interpretation and analysis of python code

When the module is run directly, the following code block will be run. When the module is imported, the code block will not be run.

That is: selectively execute code, which cannot be run when it is imported as a module; only run when it is directly run as a script file

#---Step 12 - module main---  
if __name__ == "__main__":  
    main()  

15. effect picture

Tags: Programming Python

Posted on Wed, 25 Mar 2020 21:05:04 -0400 by SCRUBBIE1