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,tmpColorgameover = TruetmpColor = color# 左斜对角验证
def L(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走x1 = x1-40y1 = y1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakprint("左上斜对角值:",n)while True:#往下走x = x+40y = y+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakprint("左下斜对角值:",n)if n>=4:success(color)def R(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走x1 = x1+40y1 = y1-40if x1<=800 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往下走x = x-40y = y+40if x>=0 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
def S(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走y1 = y1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往下走y = y+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
def H(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往左走x1 = x1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往右走x = x+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
# 画棋盘
def chess():start = 0for 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+40def E():global defaultColorfor e in pygame.event.get():if e.type == QUIT:pygame.quit()sys.exit()if e.type == MOUSEBUTTONDOWN:x,y = e.posprint(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 = Falsefor mark in circlebox:if xy[0]==mark[0] and xy[1]==mark[1]:f = Trueif 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()