
import pygame,sys
from pygame import *
pygame.init()
game = pygame.display.set_mode((600,600))
gameover = False
circlebox = []
# 棋盘坐标点存储
box = []
def xy():
    for x in range(0,800//40): 
         for y in range(0,800//40): 
             box.append((x*40,y*40))
xy()
defaultColor = 'white'
def gameWrite(word,size,color,x,y):
    # 创建一个帮我们写字的对象
    writer = pygame.font.SysFont('kaiti',size)
    # 将内容书写到给定的位置即可
    text = writer.render(word, True, color)
    # 将书写好的text传送到画布上
    game.blit(text,(x,y))
tmpColor = (255,255,255)
def success(color):
    global gameover,tmpColor
    
    gameover = True
    tmpColor = color
    
# 左斜对角验证
def L(x,y,color):
    n = 0# 如果斜对角能凑足4个则即为成功
    x1 = x
    y1 = y
    while True:#往上走
        x1 = x1-40
        y1 = y1-40
        if x1>=0 and y1>=0:
            f = False
            for i in circlebox:
                if i[0] == x1 and i[1] == y1 and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
    print("左上斜对角值:",n)
    while True:#往下走
        x = x+40
        y = y+40
        if x<=800 and y<=800:
            f = False
            for i in circlebox:
                if i[0] == x and i[1] == y and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
    print("左下斜对角值:",n)
    if n>=4:
        success(color)
        
        
def R(x,y,color):
    n = 0# 如果斜对角能凑足4个则即为成功
    x1 = x
    y1 = y
    while True:#往上走
        x1 = x1+40
        y1 = y1-40
        if x1<=800 and y1>=0:
            f = False
            for i in circlebox:
                if i[0] == x1 and i[1] == y1 and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
        
    while True:#往下走
        x = x-40
        y = y+40
        if x>=0 and y<=800:
            f = False
            for i in circlebox:
                if i[0] == x and i[1] == y and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
    if n==4:
        success(color)
def S(x,y,color):
    n = 0# 如果斜对角能凑足4个则即为成功
    x1 = x
    y1 = y
    while True:#往上走
        y1 = y1-40
        if x1>=0 and y1>=0:
            f = False
            for i in circlebox:
                if i[0] == x1 and i[1] == y1 and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
        
    while True:#往下走
        y = y+40
        if x<=800 and y<=800:
            f = False
            for i in circlebox:
                if i[0] == x and i[1] == y and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
    if n==4:
        success(color)
def H(x,y,color):
    n = 0# 如果斜对角能凑足4个则即为成功
    x1 = x
    y1 = y
    while True:#往左走
        x1 = x1-40
        if x1>=0 and y1>=0:
            f = False
            for i in circlebox:
                if i[0] == x1 and i[1] == y1 and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
        
    while True:#往右走
        x = x+40
        if x<=800 and y<=800:
            f = False
            for i in circlebox:
                if i[0] == x and i[1] == y and i[2] == color:
                    n = n+1
                    f = True
                    break
            if not f:
                break
        else:
            break
    if n==4:
        success(color)
# 画棋盘
def chess():
    start = 0
    for i in range(800//40): 
        pygame.draw.polygon(game,(255,255,255),[(0,start), (800,start)],1)
        pygame.draw.polygon(game,(255,255,255),[(start,0), (start,800)],1)
        start = start+40
def E():
    global defaultColor
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            sys.exit()
        if e.type == MOUSEBUTTONDOWN:
            x,y = e.pos
            print(e.button)
            if e.button == 1 and not gameover:# 
                for xy in box:
                    if abs(xy[0]-x)<20 and abs(xy[1]-y)<20 :
                        f = False
                        for mark in circlebox:
                            if xy[0]==mark[0] and xy[1]==mark[1]:
                                f = True
                        if not f:
                            if defaultColor=='white':
                                # 判断原来这个点是否已经存在
                                circlebox.append((xy[0],xy[1],(255,255,255)))
                                L(xy[0],xy[1],(255,255,255))
                                R(xy[0],xy[1],(255,255,255))
                                S(xy[0],xy[1],(255,255,255))
                                H(xy[0],xy[1],(255,255,255))
                                defaultColor = 'red'
                            else:
                                circlebox.append((xy[0],xy[1],(255,0,0)))
                                L(xy[0],xy[1],(255,0,0))
                                R(xy[0],xy[1],(255,0,0))
                                S(xy[0],xy[1],(255,0,0))
                                H(xy[0],xy[1],(255,0,0))
                                defaultColor = 'white'
           
def drawCircle():
    for i in circlebox:
        pygame.draw.circle(game,i[2],(i[0],i[1]),15)               
while True:
    chess()
    drawCircle()
    if gameover:
        if tmpColor == (255,255,255):
            gameWrite("恭喜白方获取胜利",60,(0,0,255),70,180)
        else:
            gameWrite("恭喜红方获取胜利",60,(0,0,255),70,180)
    E()
    pygame.display.update()



















