五子棋纯python手写,需要的拿去

 

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gjanuary

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值