您现在的位置是:首页 >技术杂谈 >pygame写中国象棋网站首页技术杂谈

pygame写中国象棋

墨炘辰 2025-07-23 00:01:02
简介pygame写中国象棋
import pygame
import sys
import torch

chessboard = torch.zeros(10, 9)
flag = 0


def chessboard_init():
    chessboard[0] = 1
    chessboard[9] = 1
    chessboard[2][1] = 1
    chessboard[2][7] = 1
    chessboard[7][1] = 1
    chessboard[7][7] = 1
    chessboard[3][0] = 1
    chessboard[3][2] = 1
    chessboard[3][4] = 1
    chessboard[3][6] = 1
    chessboard[3][8] = 1
    chessboard[6][0] = 1
    chessboard[6][2] = 1
    chessboard[6][4] = 1
    chessboard[6][6] = 1
    chessboard[6][8] = 1


def chessboard_step(chessboard):
    for i in range(10):
        chessboard[i] = 0
    for qi in chess_all:
        if qi.life:
            chessboard[(qi.high - 60) // 90][(qi.wide - 60) // 90] = 1


class Chess:
    def __init__(self, red_name, black_name, color):
        self.red_name = red_name
        self.black_name = black_name
        self.life = True
        self.color = color
        self.wide = 0
        self.high = 0
        self.clicked = False
        self.step = True

    def draw(self):
        if self.life:
            if self.color:
                draw_chess(self.wide, self.high, self.red_name, self.color)
            else:
                draw_chess(self.wide, self.high, self.black_name, self.color)

    def draw_rectangle(self):
        if self.life:
            pygame.draw.rect(screen, [0, 255, 0], [self.wide - 40, self.high - 40, 80, 80], width=3)


class ChessCar(Chess):
    def __init__(self, wide, high, color):
        super(ChessCar, self).__init__('车', '车', color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if x == self.wide and y != self.high:

            for i in chess_all:
                if i.wide == self.wide and i.high != self.high and i.color == self.color and abs(y - i.high) + abs(
                        self.high - i.high) == abs(y - self.high):
                    return False
            return True
        elif x != self.wide and y == self.high:
            for i in chess_all:
                if i.high == self.high and i.wide != self.wide and i.color == self.color and abs(x - i.wide) + abs(
                        self.wide - i.wide) == abs((x - self.wide)):
                    return False
            return True
        else:
            return False


class ChessHouse(Chess):
    def __init__(self, wide, high, color):
        super(ChessHouse, self).__init__('马', '马', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if abs(x - self.wide) == 2 * 90 and abs(y - self.high) == 90:
            for i in chess_all:
                if abs(i.wide - self.wide) == 90 and i.high == self.high and abs(x - i.wide) == 90 and abs(
                        y - i.high) == 90:
                    return False
            return True
        elif abs(x - self.wide) == 90 and abs(y - self.high) == 2 * 90:
            for i in chess_all:
                if abs(i.high - self.high) == 90 and i.wide == self.wide and abs(x - i.wide) == 90 and abs(
                        y - i.high) == 90:
                    return False
            return True
        else:
            return False


class Chess_xiang(Chess):
    def __init__(self, wide, high, color):
        super(Chess_xiang, self).__init__('相', '象', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if abs(x - self.wide) == 180 and abs(y - self.high) == 180:
            if self.color and y <= 420:
                return False
            if self.color == False and y > 420:
                return False
            for i in chess_all:
                if abs(i.wide - x) == 90 and abs(i.high - y) == 90 and abs(self.wide - i.wide) == 90 and abs(
                        self.high - i.high) == 90:
                    return False
            return True
        else:
            return False


class Chess_shi(Chess):
    def __init__(self, wide, high, color):
        super(Chess_shi, self).__init__('仕', '士', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if abs(x - self.wide) == 90 and abs(
                y - self.high) == 90 and self.color and x >= 330 and x <= 510 and y >= 690 and y <= 870:
            return True
        if abs(x - self.wide) == 90 and abs(
                y - self.high) == 90 and self.color == False and x >= 330 and x <= 510 and y >= 60 and y <= 240:
            return True
        else:
            return False


class Chess_pao(Chess):
    def __init__(self, wide, high, color):
        super(Chess_pao, self).__init__('炮', '炮', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        count1 = 0
        count2 = 0
        if x == self.wide and y != self.high:
            for i in chess_all:
                if i.wide == self.wide and i.high != self.high and abs(y - i.high) + abs(
                        self.high - i.high) == abs(y - self.high):
                    count1 += 1
            for i in chess_all:
                if i.wide == self.wide and i.high != self.high and abs(y - i.high) + abs(
                        self.high - i.high) == abs(y - self.high):
                    for j in chess_all:
                        if j.wide == x and j.high == y and j.color != self.color and count1 == 2:
                            return True
                    return False
            return True
        elif x != self.wide and y == self.high:
            for i in chess_all:
                if i.high == self.high and i.wide != self.wide and abs(x - i.wide) + abs(
                        self.wide - i.wide) == abs((x - self.wide)):
                    count2 += 1
            for i in chess_all:
                if i.high == self.high and i.wide != self.wide and abs(x - i.wide) + abs(
                        self.wide - i.wide) == abs((x - self.wide)):
                    for j in chess_all:
                        if j.wide == x and j.high == y and j.color != self.color and count2 == 2:
                            return True
                    return False
            return True
        else:
            return False


class Chess_bing(Chess):
    def __init__(self, wide, high, color):
        super(Chess_bing, self).__init__('兵', '卒', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if (self.color and y > self.high) or (self.color == False and y < self.high):
            return False
        if (self.color and y <= 420) or (self.color == False and y > 420):
            if abs(x - self.wide) == 90 and y == self.high:
                return True
            if abs(y - self.high) == 90 and x == self.wide:
                return True
            return False
        if (self.color and y > 420) or (self.color == False and y <= 420):
            if abs(y - self.high) == 90 and x == self.wide:
                return True
            return False
        return False


class Chess_jiang(Chess):
    def __init__(self, wide, high, color):
        super(Chess_jiang, self).__init__('帅', '将', color=color)
        self.wide = wide
        self.high = high

    def judgment(self, x, y):
        if self.color and x >= 330 and x <= 510 and y >= 690 and y <= 870:
            if abs(x - self.wide) == 90 and y == self.high:
                return True
            if abs(y - self.high) == 90 and x == self.wide:
                return True
            return False
        if self.color == False and x >= 330 and x <= 510 and y >= 60 and y <= 240:
            if abs(x - self.wide) == 90 and y == self.high:
                return True
            if abs(y - self.high) == 90 and x == self.wide:
                return True
            return False
        else:
            return False


'''生成棋子'''
black_car1 = ChessCar(60, 60, color=False)
black_car2 = ChessCar(780, 60, color=False)
red_car1 = ChessCar(780, 870, color=True)
red_car2 = ChessCar(60, 870, color=True)
black_house1 = ChessHouse(150, 60, color=False)
black_house2 = ChessHouse(690, 60, color=False)
red_house1 = ChessHouse(150, 870, color=True)
red_house2 = ChessHouse(690, 870, color=True)
black_el1 = Chess_xiang(240, 60, color=False)
black_el2 = Chess_xiang(600, 60, color=False)
red_el1 = Chess_xiang(240, 870, color=True)
red_el2 = Chess_xiang(600, 870, color=True)
black_shi1 = Chess_shi(330, 60, color=False)
black_shi2 = Chess_shi(510, 60, color=False)
red_shi1 = Chess_shi(330, 870, color=True)
red_shi2 = Chess_shi(510, 870, color=True)
jiang = Chess_jiang(420, 60, color=False)
shuai = Chess_jiang(420, 870, color=True)
black_pao1 = Chess_pao(150, 240, color=False)
black_pao2 = Chess_pao(690, 240, color=False)
red_pao1 = Chess_pao(150, 690, color=True)
red_pao2 = Chess_pao(690, 690, color=True)
red_bing1 = Chess_bing(60, 60 + 6 * 90, color=True)
red_bing2 = Chess_bing(60 + 180, 60 + 6 * 90, color=True)
red_bing3 = Chess_bing(240 + 180, 60 + 6 * 90, color=True)
red_bing4 = Chess_bing(240 + 360, 60 + 6 * 90, color=True)
red_bing5 = Chess_bing(240 + 540, 60 + 6 * 90, color=True)
black_bing1 = Chess_bing(60, 60 + 3 * 90, color=False)
black_bing2 = Chess_bing(60 + 180, 60 + 3 * 90, color=False)
black_bing3 = Chess_bing(60 + 360, 60 + 3 * 90, color=False)
black_bing4 = Chess_bing(60 + 540, 60 + 3 * 90, color=False)
black_bing5 = Chess_bing(60 + 720, 60 + 3 * 90, color=False)

chess_all = [black_car1, black_car2, red_car1, red_car2, black_house1, black_house2, red_house1, red_house2, black_el1,
             black_el2, red_el1, red_el2,
             black_shi1, black_shi2, red_shi1, red_shi2, jiang, shuai, black_pao1, black_pao2, red_pao1, red_pao2,
             red_bing1, red_bing2, red_bing3, red_bing4, red_bing5,
             black_bing1, black_bing2, black_bing3, black_bing4, black_bing5]


def draw_chessboard():
    """画长线"""
    pygame.draw.line(screen, (255, 0, 0), (18 + 30, 20 + 30), (760 + 30, 20 + 30), 6)
    pygame.draw.line(screen, (255, 0, 0), (20 + 30, 18 + 30), (20 + 30, 850 + 30), 6)
    pygame.draw.line(screen, (255, 0, 0), (18 + 30, 850 + 30), (762 + 30, 850 + 30), 6)
    pygame.draw.line(screen, (255, 0, 0), (760 + 30, 18 + 30), (760 + 30, 852 + 30), 6)

    pygame.draw.line(screen, (255, 0, 0), (30 + 30, 30 + 30), (750 + 30, 30 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (30 + 30, 30 + 30), (30 + 30, 840 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (30 + 30, 840 + 30), (750 + 30, 840 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (750 + 30, 30 + 30), (750 + 30, 840 + 30), 2)
    for i in range(9):
        pygame.draw.line(screen, (255, 0, 0), (30 + 30, 30 + i * 90 + 30), (750 + 30, 30 + 30 + i * 90), 2)
    for j in range(9):
        pygame.draw.line(screen, (255, 0, 0), (30 + 30 + j * 90, 30 + 30), (30 + 30 + j * 90, 390 + 30), 2)
    for j in range(9):
        pygame.draw.line(screen, (255, 0, 0), (30 + 30 + j * 90, 480 + 30), (30 + 30 + j * 90, 840 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (300 + 30, 30 + 30), (480 + 30, 210 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (300 + 30, 210 + 30), (480 + 30, 30 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (300 + 30, 660 + 30), (480 + 30, 840 + 30), 2)
    pygame.draw.line(screen, (255, 0, 0), (300 + 30, 840 + 30), (480 + 30, 660 + 30), 2)
    '''画小短线'''
    for i in range(2):
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 20, 210 + 30 + 5),
                         (120 + 30 + i * 540 - 5, 210 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 20, 210 + 30 - 5),
                         (120 + 30 + i * 540 - 5, 210 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 210 + 30 + 5),
                         (120 + 30 + i * 540 + 20, 210 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 210 + 30 - 5),
                         (120 + 30 + i * 540 + 20, 210 + 30 - 5), 2)

        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 210 + 30 + 5),
                         (120 + 30 + i * 540 + 5, 210 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 5, 210 + 30 + 5),
                         (120 + 30 + i * 540 - 5, 210 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 210 + 30 - 20),
                         (120 + 30 + i * 540 + 5, 210 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 5, 210 + 30 - 20),
                         (120 + 30 + i * 540 - 5, 210 + 30 - 5), 2)
    for i in range(3):
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 20, 300 + 30 + 5),
                         (210 + 30 + i * 180 - 5, 300 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 20, 300 + 30 - 5),
                         (210 + 30 + i * 180 - 5, 300 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 300 + 30 + 5),
                         (210 + 30 + i * 180 + 20, 300 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 300 + 30 - 5),
                         (210 + 30 + i * 180 + 20, 300 + 30 - 5), 2)

        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 300 + 30 + 5),
                         (210 + 30 + i * 180 + 5, 300 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 5, 300 + 30 + 5),
                         (210 + 30 + i * 180 - 5, 300 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 300 + 30 - 20),
                         (210 + 30 + i * 180 + 5, 300 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 5, 300 + 30 - 20),
                         (210 + 30 + i * 180 - 5, 300 + 30 - 5), 2)

    for i in range(2):
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 20, 660 + 30 + 5),
                         (120 + 30 + i * 540 - 5, 660 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 20, 660 + 30 - 5),
                         (120 + 30 + i * 540 - 5, 660 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 660 + 30 + 5),
                         (120 + 30 + i * 540 + 20, 660 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 660 + 30 - 5),
                         (120 + 30 + i * 540 + 20, 660 + 30 - 5), 2)

        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 660 + 30 + 5),
                         (120 + 30 + i * 540 + 5, 660 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 5, 660 + 30 + 5),
                         (120 + 30 + i * 540 - 5, 660 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 + 5, 660 + 30 - 20),
                         (120 + 30 + i * 540 + 5, 660 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (120 + 30 + i * 540 - 5, 660 + 30 - 20),
                         (120 + 30 + i * 540 - 5, 660 + 30 - 5), 2)
    for i in range(3):
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 20, 570 + 30 + 5),
                         (210 + 30 + i * 180 - 5, 570 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 20, 570 + 30 - 5),
                         (210 + 30 + i * 180 - 5, 570 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 570 + 30 + 5),
                         (210 + 30 + i * 180 + 20, 570 + 30 + 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 570 + 30 - 5),
                         (210 + 30 + i * 180 + 20, 570 + 30 - 5), 2)

        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 570 + 30 + 5),
                         (210 + 30 + i * 180 + 5, 570 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 5, 570 + 30 + 5),
                         (210 + 30 + i * 180 - 5, 570 + 30 + 20), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 + 5, 570 + 30 - 20),
                         (210 + 30 + i * 180 + 5, 570 + 30 - 5), 2)
        pygame.draw.line(screen, (255, 0, 0), (210 + 30 + i * 180 - 5, 570 + 30 - 20),
                         (210 + 30 + i * 180 - 5, 570 + 30 - 5), 2)

    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 20, 570 + 30 + 5),
                     (210 + 30 + 3 * 180 - 5, 570 + 30 + 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 20, 570 + 30 - 5),
                     (210 + 30 + 3 * 180 - 5, 570 + 30 - 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 5, 570 + 30 + 5),
                     (210 + 30 + 3 * 180 - 5, 570 + 30 + 20), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 5, 570 + 30 - 20),
                     (210 + 30 + 3 * 180 - 5, 570 + 30 - 5), 2)

    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 20, 300 + 30 + 5),
                     (210 + 30 + 3 * 180 - 5, 300 + 30 + 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 20, 300 + 30 - 5),
                     (210 + 30 + 3 * 180 - 5, 300 + 30 - 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 5, 300 + 30 + 5),
                     (210 + 30 + 3 * 180 - 5, 300 + 30 + 20), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 + 3 * 180 - 5, 300 + 30 - 20),
                     (210 + 30 + 3 * 180 - 5, 300 + 30 - 5), 2)

    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 300 + 30 + 5),
                     (210 + 30 - 1 * 180 + 20, 300 + 30 + 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 300 + 30 - 5),
                     (210 + 30 - 1 * 180 + 20, 300 + 30 - 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 300 + 30 + 5),
                     (210 + 30 - 1 * 180 + 5, 300 + 30 + 20), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 300 + 30 - 20),
                     (210 + 30 - 1 * 180 + 5, 300 + 30 - 5), 2)

    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 570 + 30 + 5),
                     (210 + 30 - 1 * 180 + 20, 570 + 30 + 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 570 + 30 - 5),
                     (210 + 30 - 1 * 180 + 20, 570 + 30 - 5), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 570 + 30 + 5),
                     (210 + 30 - 1 * 180 + 5, 570 + 30 + 20), 2)
    pygame.draw.line(screen, (255, 0, 0), (210 + 30 - 1 * 180 + 5, 570 + 30 - 20),
                     (210 + 30 - 1 * 180 + 5, 570 + 30 - 5), 2)
    draw_word(word_style='隶书', word='楚 河      汉 界', wide=150, high=430, size=68, color=True,sc=screen)


def draw_word(word_style, word, wide, high, size, color,sc):
    """设置选择的字体与大小"""
    ft = pygame.font.SysFont(word_style, size)
    '''设置写的内容与颜色'''
    if color:
        word = ft.render(word, True, (255, 0, 0))
    else:
        word = ft.render(word, True, (0, 0, 0))
    '''将字体写在屏幕的某一个位置'''
    sc.blit(word, (wide, high))


def draw_chess(wide, high, name, color):
    if color:
        pygame.draw.circle(screen, (255, 0, 0), (wide, high), 40, width=4)
        pygame.draw.circle(screen, (238, 239, 241), (wide, high), 40 - 3)
        pygame.draw.circle(screen, (255, 0, 0), (wide, high), 40 - 8, width=4)
    else:
        pygame.draw.circle(screen, (0, 0, 0), (wide, high), 40, width=4)
        pygame.draw.circle(screen, (238, 239, 241), (wide, high), 40 - 3)
        pygame.draw.circle(screen, (0, 0, 0), (wide, high), 40 - 8, width=4)
    draw_word('华文行楷', name, wide - 25, high - 25, 50, color,sc=screen)


def chess_repeat_draw():
    red_car1.draw()
    red_car2.draw()
    black_car1.draw()
    black_car2.draw()
    black_house1.draw()
    black_house2.draw()
    red_house1.draw()
    red_house2.draw()
    black_el1.draw()
    black_el2.draw()
    red_el1.draw()
    red_el2.draw()
    black_shi1.draw()
    black_shi2.draw()
    red_shi1.draw()
    red_shi2.draw()
    jiang.draw()
    shuai.draw()
    black_pao1.draw()
    black_pao2.draw()
    red_pao1.draw()
    red_pao2.draw()
    red_bing1.draw()
    red_bing2.draw()
    red_bing3.draw()
    red_bing4.draw()
    red_bing5.draw()
    black_bing1.draw()
    black_bing2.draw()
    black_bing3.draw()
    black_bing4.draw()
    black_bing5.draw()
    chessboard_step(chessboard)


def mouse_rectangle(x, y):
    if x >= 30 and x <= 810 and y >= 30 and y <= 900:
        out_x = (x - 60) // 90
        out_y = (y - 60) // 90
        if abs(x - (out_x * 90 + 60)) < 45 and abs(y - (out_y * 90 + 60)) < 45:
            outX = out_x * 90 + 60
            outY = out_y * 90 + 60
            return outX, outY
        if abs(x - ((out_x + 1) * 90 + 60)) < 45 and abs(y - ((out_y + 1) * 90 + 60)) < 45:
            outX = (out_x + 1) * 90 + 60
            outY = (out_y + 1) * 90 + 60
            return outX, outY
        if abs(x - (out_x * 90 + 60)) < 45 and abs(y - ((out_y + 1) * 90 + 60)) < 45:
            outX = out_x * 90 + 60
            outY = (out_y + 1) * 90 + 60
            return outX, outY
        if abs(x - ((out_x + 1) * 90 + 60)) < 45 and abs(y - (out_y * 90 + 60)) < 45:
            outX = (out_x + 1) * 90 + 60
            outY = out_y * 90 + 60
            return outX, outY
        else:
            return 0, 0
    else:
        return 0, 0


pygame.init()
chessboard_init()
print(chessboard)
screen = pygame.display.set_mode((840, 930))
pygame.display.set_caption('中国象棋')

X, Y = 0, 0
running = True
while running:
    for event in pygame.event.get():  # 监听用户事件
        if event.type == pygame.QUIT:  # 判断用户是否点击了关闭按钮
            sys.exit()  # 用户退出
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_presses = pygame.mouse.get_pressed()
            if mouse_presses[0]:
                x, y = pygame.mouse.get_pos()
                X, Y = mouse_rectangle(x, y)
                for chess in chess_all:
                    if chess.step == False and chess.clicked and chess.life and chess.judgment(X, Y):
                        if flag:
                            flag = 0
                        elif flag == 0:
                            flag = 1
                        chess.step = True
                    else:
                        chess.clicked = False

            if mouse_presses[1]:
                pass
            if mouse_presses[2]:
                pass
    screen.fill((238, 239, 241))
    draw_chessboard()
    chess_repeat_draw()

    if X and Y:
        if flag == 0:
            for chess in chess_all:
                if chess.wide == X and chess.high == Y and chess.life and chess.color:
                    chess.draw_rectangle()
                    chess.clicked = True
                    chess.step = False
        elif flag == 1:
            for chess in chess_all:
                if chess.wide == X and chess.high == Y and chess.life and chess.color == False:
                    chess.draw_rectangle()
                    chess.clicked = True
                    chess.step = False

        for chess in chess_all:
            if chess.clicked and chess.step and chessboard[(Y - 60) // 90][(X - 60) // 90] == 0 and chess.life:
                chess.wide = X
                chess.high = Y
                chess.clicked = False
                print(chessboard)
                X = 0
                Y = 0

            if chess.clicked and chess.step and chessboard[(Y - 60) // 90][(X - 60) // 90] == 1 and chess.life:
                for i in chess_all:
                    if i.wide == X and i.high == Y and i.color == chess.color and i.life:
                        chess.clicked = False
                    elif i.wide == X and i.high == Y and i.color != chess.color and i.life:
                        i.life = False
                        i.wide = 0
                        i.high = 0
                        chess.wide = X
                        chess.high = Y
                        chess.clicked = False
                        X = 0
                        Y = 0
    for chess in chess_all:

        if chess.color and chess.red_name == '帅' and chess.life == False:
            screen2 = pygame.display.set_mode((200, 200))
            screen2.fill((255,255,255))
            draw_word('华文楷体','黑棋胜利',15,15,20,(0,0,0),sc=screen2)
        if chess.color == False and chess.black_name == '将' and chess.life == False:
            screen2 = pygame.display.set_mode((200, 200))
            screen2.fill((255, 255, 255))
            draw_word('华文楷体','红棋胜利',15,15,20,(0,0,0),sc=screen2)
    '''刷新,否则不会显示'''
    pygame.display.update()
    pygame.display.flip()

写的很不规范,也很繁琐,交替走棋操作有时会有点BUG,其它除了有点简陋还行,如果发现BUG原因,望指出,希望对你有帮助。

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。