These days I saw a girl playing chess on Douyin is very good, my Chinese chess is not bad, go to be a… This Chinese chess is implemented with Python3+pygame

First, operation effect

! [] (

Second, the code

The following code used material (images folder) images, download address is as follows: www.itprojects.cn/detail.html…

Author: IT Project Example website: www.itprojects.cn ""

import sys

import pygame

Width and height of the window to display
WIDTH, HEIGHT = 750.667


class ClickBox(pygame.sprite.Sprite) :
    """ Select the piece object ""
    singleton = None

    def __new__(cls, *args, **kwargs) :
        if cls.singleton is None:
            cls.singleton = super().__new__(cls)
        return cls.singleton

    def __init__(self, screen, row, col, team) :
        super().__init__()
        self.image = pygame.image.load("images/r_box.png")
        self.rect = self.image.get_rect()
        self.row, self.col = row, col
        self.rect.topleft = (50 + self.col * 57.50 + self.row * 57)
        self.screen = screen
        self.team = team

    @classmethod
    def show(cls) :
        if cls.singleton:
            cls.singleton.screen.blit(cls.singleton.image, cls.singleton.rect)

    @classmethod
    def clean(cls) :
        """ Clean up the last object """
        cls.singleton = None


class Dot(pygame.sprite.Sprite) :
    """ Fallable chess pieces """
    group = list(a)def __init__(self, screen, position) :
        super().__init__()
        self.image = pygame.image.load("images/dot2.png")
        self.rect = self.image.get_rect()
        self.row, self.col = position  Unpack the tuple
        self.rect.topleft = (60 + self.col * 57.60 + self.row * 57)
        self.group.append(self)
        self.screen = screen

    @classmethod
    def show(cls) :
        for dot in cls.group:
            dot.screen.blit(dot.image, dot.rect)

    @classmethod
    def clean_last_postion(cls) :
        """ Clear the last position """
        cls.group.clear()

    @classmethod
    def click(cls) :
        """ Click on the piece """
        for dot in cls.group:
            if pygame.mouse.get_pressed()[0] and dot.rect.collidepoint(pygame.mouse.get_pos()):
                print("Was clicked on the 'playable' object.")
                return dot


class Chess(pygame.sprite.Sprite) :
    """ Chess pieces """

    def __init__(self, screen, chess_name, row, col) :
        self.screen = screen
        self.image = pygame.image.load("images/" + chess_name + ".png")
        self.rect = self.image.get_rect()
        self.rect.topleft = (50 + col * 57.50 + row * 57)
        self.team = chess_name[0]  # Team (Red R, black B)
        self.name = chess_name[2]  # Name (Cannon P, Horse M, etc.)
        self.row = row
        self.col = col

    def show(self) :
        self.screen.blit(self.image, self.rect)

    @staticmethod
    def click(player, chesses) :
        """ Click on the piece """
        for chess in chesses:
            if pygame.mouse.get_pressed()[0] and chess.rect.collidepoint(pygame.mouse.get_pos()):
                if player == chess.team:
                    print("Clicked")
                    return chess

    def update_postion(self, new_row, new_col) :
        """ Update the coordinates of the image to display """
        self.row = new_row
        self.col = new_col
        self.rect.topleft = (50 + new_col * 57.50 + new_row * 57)


class ChessBoard(object) :
    """ checkerboard """

    def __init__(self, screen) :
        self.screen = screen
        self.image = pygame.image.load("images/bg.png")
        self.topleft = (50.50)
        self.__create_default_chess()

    def __create_default_chess(self) :
        """ Create a default chess piece """
        self.map= [["b_c"."b_m"."b_x"."b_s"."b_j"."b_s"."b_x"."b_m"."b_c"],
            [""."".""."".""."".""."".""],
            [""."b_p".""."".""."".""."b_p".""],
            ["b_z".""."b_z".""."b_z".""."b_z".""."b_z"],
            [""."".""."".""."".""."".""],
            [""."".""."".""."".""."".""],
            ["r_z".""."r_z".""."r_z".""."r_z".""."r_z"],
            [""."r_p".""."".""."".""."r_p".""],
            [""."".""."".""."".""."".""],
            ["r_c"."r_m"."r_x"."r_s"."r_j"."r_s"."r_x"."r_m"."r_c"]]for row, line in enumerate(self.map) :for col, chess_name in enumerate(line):
                if chess_name:
                    # Add the created chess piece to the property map
                    self.map[row][col] = Chess(self.screen, chess_name, row, col)
                else:
                    self.map[row][col] = None

    def show(self) :
        # display checkerboard
        self.screen.blit(self.image, self.topleft)
        Display all the pieces on the board
        for line_chess in self.map:
            for chess in line_chess:
                if chess:
                    chess.show()

    def get_put_down_postion(self, clicked_chess) :
        """ Calculates the position that the current piece can move. ""
        # Store the position where the current piece can be placed
        all_position = list(a)Get the row and column of the current piece
        row, col = clicked_chess.row, clicked_chess.col
        # The team that gets the current piece, instant red r or black B
        team = clicked_chess.team

        # Count all possible positions of the currently selected piece
        if clicked_chess.name == "p":  # gun
            # a line
            direction_left_chess_num = 0
            direction_right_chess_num = 0
            for i in range(1.9) :# Calculate the position of the left and right pieces in the current row
                # Left position is not out of bounds
                if direction_left_chess_num >= 0 and col - i >= 0:
                    if not self.map[row][col - i] and direction_left_chess_num == 0:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row, col - i))
                    elif self.map[row][col - i]:
                        # If there is a piece in the current position, then determine whether it can be eaten
                        direction_left_chess_num += 1
                        if direction_left_chess_num == 2 and self.map[row][col - i].team ! = team: all_position.append((row, col - i)) direction_left_chess_num = -1  # make it impossible to judge again the next for loop
                # The right position is not out of bounds
                if direction_right_chess_num >= 0 and col + i <= 8:
                    if not self.map[row][col + i] and direction_right_chess_num == 0:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row, col + i))
                    elif self.map[row][col + i]:
                        # If there is a piece in the current position, then determine whether it can be eaten
                        direction_right_chess_num += 1
                        if direction_right_chess_num == 2 and self.map[row][col + i].team ! = team: all_position.append((row, col + i)) direction_right_chess_num = -1
            # a column
            direction_up_chess_num = 0
            direction_down_chess_num = 0
            for i in range(1.10) :So let's say I starts at 1 instead of 0
                # Calculate the position of the top and bottom pieces in the current column
                # The upper position is not out of bounds
                if direction_up_chess_num >= 0 and row - i >= 0:
                    if not self.map[row - i][col] and direction_up_chess_num == 0:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row - i, col))
                    elif self.map[row - i][col]:
                        # If there is a piece in the current position, then determine whether it can be eaten
                        direction_up_chess_num += 1
                        if direction_up_chess_num == 2 and self.map[row - i][col].team ! = team: all_position.append((row - i, col)) direction_up_chess_num = -1

                # The bottom position is not out of bounds
                if direction_down_chess_num >= 0 and row + i <= 9:
                    if not self.map[row + i][col] and direction_down_chess_num == 0:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row + i, col))
                    elif self.map[row + i][col]:
                        # If there is a piece in the current position, then determine whether it can be eaten
                        direction_down_chess_num += 1
                        if direction_down_chess_num == 2 and self.map[row + i][col].team ! = team: all_position.append((row + i, col)) direction_down_chess_num = -1
        elif clicked_chess.name == "z":  # pawn
            if team == "r":  # "red"
                if row - 1> =0:  # Can only move up
                    if not self.map[row - 1][col] or self.map[row - 1][col].team ! = team: all_position.append((row -1, col))
            else:  # black
                if row + 1< =9:  Can only move down
                    if not self.map[row + 1][col] or self.map[row + 1][col].team ! = team: all_position.append((row +1, col))
            # Left-right judgment
            if (team == "r" and 0 <= row <= 4) or (team == "b" and 5 <= row <= 9) :# Step left, step right
                # left
                if col - 1> =0 and (not self.map[row][col - 1] or self.map[row][col - 1].team ! = team): all_position.append((row, col -1))
                # right
                if col + 1< =8 and (not self.map[row][col + 1] or self.map[row][col + 1].team ! = team): all_position.append((row, col +1))
        elif clicked_chess.name == "c":  # the car
            # a line
            left_stop = False
            right_stop = False
            for i in range(1.9) :# Left position is not out of bounds and does not encounter any pieces
                if not left_stop and col - i >= 0:
                    if not self.map[row][col - i]:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row, col - i))
                    else:
                        left_stop = True
                        if self.map[row][col - i].team ! = team:# If there is a piece in the current position, then determine whether it can be eaten
                            all_position.append((row, col - i))
                # The right position is not out of bounds and does not encounter any pieces
                if not right_stop and col + i <= 8:
                    if not self.map[row][col + i]:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row, col + i))
                    else:
                        right_stop = True
                        if self.map[row][col + i].team ! = team:# If there is a piece in the current position, then determine whether it can be eaten
                            all_position.append((row, col + i))

            # a column
            up_stop = False
            down_stoop = False
            for i in range(1.10) :# The top position is not out of bounds and does not encounter any pieces
                if not up_stop and row - i >= 0:
                    if not self.map[row - i][col]:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row - i, col))
                    else:
                        up_stop = True
                        if self.map[row - i][col].team ! = team:# If there is a piece in the current position, then determine whether it can be eaten
                            all_position.append((row - i, col))
                # The position below is not out of bounds and does not encounter any pieces
                if not down_stoop and row + i <= 9:
                    if not self.map[row + i][col]:
                        # If there are no pieces, add the current position to the list as a tuple
                        all_position.append((row + i, col))
                    else:
                        down_stoop = True
                        if self.map[row + i][col].team ! = team:# If there is a piece in the current position, then determine whether it can be eaten
                            all_position.append((row + i, col))
        elif clicked_chess.name == "m":  # horse
            # There are 4 directions to judge, and 2 positions for each direction
            # above
            if row - 1> =0 and not self.map[row - 1][col]:  # If the current chessman is not lamed, then check the 2 positions in this direction
                # upper left
                if row - 2> =0 and col - 1> =0 and (not self.map[row - 2][col - 1] or self.map[row - 2][col - 1].team ! = team): all_position.append((row -2, col - 1))
                The upper right of the #
                if row - 2> =0 and col + 1< =8 and (not self.map[row - 2][col + 1] or self.map[row - 2][col + 1].team ! = team): all_position.append((row -2, col + 1))
            # below
            if row + 1< =9 and not self.map[row + 1][col]:  # If the current chessman is not lamed, then check the 2 positions in this direction
                # bottom left
                if row + 2> =0 and col - 1> =0 and (not self.map[row + 2][col - 1] or self.map[row + 2][col - 1].team ! = team): all_position.append((row +2, col - 1))
                # right
                if row + 2> =0 and col + 1< =8 and (not self.map[row + 2][col + 1] or self.map[row + 2][col + 1].team ! = team): all_position.append((row +2, col + 1))
            # the left
            if col - 1> =0 and not self.map[row][col - 1] :# If the current chessman is not lamed, then check the 2 positions in this direction
                # top left 2
                if row - 1> =0 and col - 2> =0 and (not self.map[row - 1][col - 2] or self.map[row - 1][col - 2].team ! = team): all_position.append((row -1, col - 2))
                The left lower # 2
                if row + 1< =9 and col - 2> =0 and (not self.map[row + 1][col - 2] or self.map[row + 1][col - 2].team ! = team): all_position.append((row +1, col - 2))
            # the right
            if col + 1< =8 and not self.map[row][col + 1] :# If the current chessman is not lamed, then check the 2 positions in this direction
                # upper right 2
                if row - 1> =0 and col + 2< =8 and (not self.map[row - 1][col + 2] or self.map[row - 1][col + 2].team ! = team): all_position.append((row -1, col + 2))
                # 2 at the lower right
                if row + 1< =9 and col + 2< =8 and (not self.map[row + 1][col + 2] or self.map[row + 1][col + 2].team ! = team): all_position.append((row +1, col + 2))
        elif clicked_chess.name == "x":  # like
            Since elephants cannot cross rivers, work out how far they can move
            row_start, row_stop = (0.4) if team == "b" else (5.9)
            # have 4 directions (no cross, no leg jump)
            if row - 2 >= row_start and col - 2> =0 and not self.map[row - 1][col - 1] :# upper left
                if not self.map[row - 2][col - 2] or self.map[row - 2][col - 2].team ! = team: all_position.append((row -2, col - 2))
            if row - 2 >= row_start and col + 2< =8 and not self.map[row - 1][col + 1] :The upper right of the #
                if not self.map[row - 2][col + 2] or self.map[row - 2][col + 2].team ! = team: all_position.append((row -2, col + 2))
            if row + 2 <= row_stop and col - 2> =0 and not self.map[row + 1][col - 1] :# bottom left
                if not self.map[row + 2][col - 2] or self.map[row + 2][col - 2].team ! = team: all_position.append((row +2, col - 2))
            if row + 2 <= row_stop and col + 2< =8 and not self.map[row + 1][col + 1] :# right
                if not self.map[row + 2][col + 2] or self.map[row + 2][col + 2].team ! = team: all_position.append((row +2, col + 2))
        elif clicked_chess.name == "s":  #"
            Figure out how far they can move because they can't cross the river
            row_start, row_stop = (0.2) if team == "b" else (7.9)
            if row - 1 >= row_start and col - 1> =3 and (not self.map[row - 1][col - 1] or self.map[row - 1][col - 1].team ! = team): all_position.append((row -1, col - 1))
            if row - 1 >= row_start and col + 1< =5 and (not self.map[row - 1][col + 1] or self.map[row - 1][col + 1].team ! = team): all_position.append((row -1, col + 1))
            if row + 1 <= row_stop and col - 1> =3 and (not self.map[row + 1][col - 1] or self.map[row + 1][col - 1].team ! = team): all_position.append((row +1, col - 1))
            if row + 1 <= row_stop and col + 1< =5 and (not self.map[row + 1][col + 1] or self.map[row + 1][col + 1].team ! = team): all_position.append((row +1, col + 1))
        elif clicked_chess.name == "j":  # will be
            Since "will" cannot cross the river, calculate the range of rows they can move
            row_start, row_stop = (0.2) if team == "b" else (7.9)
            # There are 4 directions to judge
            if row - 1 >= row_start and (not self.map[row - 1][col] or self.map[row - 1][col].team ! = team): all_position.append((row -1, col))
            if row + 1 <= row_stop and (not self.map[row + 1][col] or self.map[row + 1][col].team ! = team): all_position.append((row +1, col))
            if col - 1> =3 and (not self.map[row][col - 1] or self.map[row][col - 1].team ! = team): all_position.append((row, col -1))
            if col + 1< =5 and (not self.map[row][col + 1] or self.map[row][col + 1].team ! = team): all_position.append((row, col +1))

        all_position = self.judge_delete_position(all_position, clicked_chess)

        # return all positions where the child can be dropped
        return all_position

    def judge_delete_position(self, all_position, clicked_chess) :
        """ Delete position by "checkmate """
        # Define the list to delete
        deleting_position = list(a)# Determine if the position will result in checkmate, if so remove the position from the list
        for row, col in all_position:
            # 1. Backup
            # Backup the current chess position
            old_row, old_col = clicked_chess.row, clicked_chess.col
            # Back up the position of the piece to be placed (if None, None)
            position_chess_backup = self.map[row][col]
            # 2. Move
            # Move position
            self.map[row][col] = self.map[old_row][old_col]
            # Change the properties of the pieces
            self.map[row][col].update_postion(row, col)
            # clear the previous position is None
            self.map[old_row][old_col] = None
            # 3. Determine if they can initiate checkmate
            if self.judge_attack_general("b" if clicked_chess.team == "r" else "r"):
                deleting_position.append((row, col))
            # 4. Restore to previous position
            self.map[old_row][old_col] = self.map[row][col]
            self.map[old_row][old_col].update_postion(old_row, old_col)
            self.map[row][col] = position_chess_backup

        # 5. Delete unplayable positions
        all_position = list(set(all_position) - set(deleting_position))

        return all_position

    def move_chess(self, new_row, new_col) :
        "" "move later "" "
        # Get the position of the piece to be moved
        old_row, old_col = ClickBox.singleton.row, ClickBox.singleton.col
        print("Old location:", old_row, old_col, "New location:, new_row, new_col)
        # Move position
        self.map[new_row][new_col] = self.map[old_row][old_col]
        # Change the properties of the pieces
        self.map[new_row][new_col].update_postion(new_row, new_col)
        # clear the previous position is None
        self.map[old_row][old_col] = None

    def judge_attack_general(self, attact_player) :
        Attact_player determines whether the attact_player side has sent the other side's army.
        # 1. Find your opponent's position
        general_player = "r" if attact_player == "b" else "b"
        general_position = self.get_general_position(general_player)

        # 2. Go through all our pieces
        for row, line in enumerate(self.map) :for col, chess in enumerate(line):
                if chess and chess.team == attact_player:
                    if chess.name == "z":  # soldier
                        # pass 5 arguments (attacker's identity, attacker's row, attacker's col, attacker's row, attacker's col)
                        if self.judge_z_attack(chess.team, chess.row, chess.col, *general_position):
                            return True
                    elif chess.name == "p":  # gun
                        if self.judge_c_and_p_attack(chess.name, chess.row, chess.col, *general_position):
                            return True
                    elif chess.name == "c":  # the car
                        if self.judge_c_and_p_attack(chess.name, chess.row, chess.col, *general_position):
                            return True
                    elif chess.name == "m":  # horse
                        if self.judge_m_attack(chess.row, chess.col, *general_position):
                            return True
                    elif chess.name == "x":  # like
                        pass
                    elif chess.name == "s":  #"
                        pass
                    elif chess.name == "j":  # will be
                        if self.judge_j_attack(chess.row, chess.col, *general_position):
                            return True

    def judge_j_attack(self, attack_row, attack_col, general_row, general_col) :
        """ Determine if two will be relative """
        if attack_col == general_col:
            # in the same column
            min_row, max_row = (attack_row, general_row) if attack_row < general_row else (general_row, attack_row)

            chess_num = 0
            for i in range(min_row + 1, max_row):
                if self.map[i][general_col]:
                    chess_num += 1
            if chess_num == 0:
                return True

    def judge_m_attack(self, attack_row, attack_col, general_row, general_col) :
        """ Determine if the horse has attacked "will" """
        if attack_row == general_row or attack_col == general_col:
            return False
        else:
            If the horse can attack the general, then the sum of the squares of the two sides must be 5
            col_length = (attack_col - general_col) ** 2
            row_length = (attack_row - general_row) ** 2
            if col_length + row_length == 5:
                # Judge horse leg
                if col_length == 1:
                    if general_row < attack_row and not self.map[attack_row - 1][attack_col]:
                        return True
                    elif general_row > attack_row and not self.map[attack_row + 1][attack_col]:
                        return True
                elif col_length == 4:
                    if general_col < attack_col and not self.map[attack_row][attack_col - 1] :return True
                    elif general_col > attack_col and not self.map[attack_row][attack_col + 1] :return True

    def judge_c_and_p_attack(self, attack_chess_name, attack_row, attack_col, general_row, general_col) :
        """ Judge "" tank ", "gun" can attack the other side "will" """
        check_chess_num = 1 if attack_chess_name == "p" else 0
        chess_num = 0
        if attack_row == general_row:
            # on the same line
            min_col, max_col = (attack_col, general_col) if attack_col < general_col else (general_col, attack_col)
            for i in range(min_col + 1, max_col):
                if self.map[attack_row][i]:
                    chess_num += 1
            if chess_num == check_chess_num:
                return True
        elif attack_col == general_col:
            # in the same column
            min_row, max_row = (attack_row, general_row) if attack_row < general_row else (general_row, attack_row)
            for i in range(min_row + 1, max_row):
                if self.map[i][general_col]:
                    chess_num += 1
            if chess_num == check_chess_num:
                return True

    def judge_z_attack(self, attack_team, attack_row, attack_col, general_row, general_col) :
        """ Determine if pawns attack "will" """
        if attack_team == "r" and attack_row < general_row:
            return False
        elif attack_team == "b" and attack_row > general_row:
            return False
        elif (attack_row - general_row) ** 2 + (attack_col - general_col) ** 2= =1:
            return True

    def get_general_position(self, general_player) :
        """ Find the position of the general of the party to the general_player flag """
        for row, line in enumerate(self.map) :for col, chess in enumerate(line):
                if chess and chess.team == general_player and chess.name == "j":
                    return chess.row, chess.col

    def judge_win(self, attack_player) :
        """ To decide whether to win or not. ""
        # Check whether all the pieces of the attacked side are possible to block the attack
        for line_chesses in self.map:
            for chess in line_chesses:
                if chess andchess.team ! = attack_player: move_position_list = self.get_put_down_postion(chess)if move_position_list:  # As long as you find a position to move, it means there is no failure, there is still a chance
                        return False

        return True


class Game(object) :
    """ Games """

    def __init__(self, screen) :
        self.screen = screen
        self.player = "r"  # default move is red r
        self.player_tips_r_image = pygame.image.load("images/red.png")
        self.player_tips_r_image_topleft = (550.500)
        self.player_tips_b_image = pygame.image.load("images/black.png")
        self.player_tips_b_image_topleft = (550.100)
        self.show_attack = False
        self.show_attack_count = 0
        self.show_attack_time = 100
        self.attack_img = pygame.image.load("images/pk.png")
        self.show_win = False
        self.win_img = pygame.image.load("images/win.png")
        self.win_player = None

    def get_player(self) :
        """ Gets the current chess move """
        return self.player

    def exchange(self) :
        """ "Switch sides """
        self.player = "r" if self.player == "b" else "b"
        return self.get_player()

    def show(self) :
        if self.show_win:
            if self.win_player == "b":
                self.screen.blit(self.win_img, (550.100))
            else:
                self.screen.blit(self.win_img, (550.450))
            return

        Display "General" for a while and then disappear
        if self.show_attack:
            self.show_attack_count += 1
            if self.show_attack_count == self.show_attack_time:
                self.show_attack_count = 0
                self.show_attack = False

        if self.player == "r":
            self.screen.blit(self.player_tips_r_image, self.player_tips_r_image_topleft)
            # Show checkmate effect
            if self.show_attack:
                self.screen.blit(self.attack_img, (230.400))
        else:
            self.screen.blit(self.player_tips_b_image, self.player_tips_b_image_topleft)
            # Show checkmate effect
            if self.show_attack:
                self.screen.blit(self.attack_img, (230.100))

    def set_attack(self) :
        """ Mark "Checkmate effect ""
        self.show_attack = True

    def set_win(self, win_player) :
        """ Set the winning side """
        self.show_win = True
        self.win_player = win_player


def main() :
    Initialize PyGame
    pygame.init()
    Create an object to display the image.
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    # Game background images
    background_img = pygame.image.load("images/bg.jpg")
    Create a gameobject
    game = Game(screen)
    Create a game board object
    chess_board = ChessBoard(screen)
    # create timer
    clock = pygame.time.Clock()

    # main loop
    while True:
        # Event detection (e.g. keyboard, mouse, etc.)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()  # exit program

            # If the game has no winning side, the game continues, otherwise "win" is always displayed.
            if not game.show_win:
                Check if the "drop-able" object is clicked
                clicked_dot = Dot.click()
                if clicked_dot:
                    chess_board.move_chess(clicked_dot.row, clicked_dot.col)
                    # Clear "clickable object", "locatable object"
                    Dot.clean_last_postion()
                    ClickBox.clean()
                    # Check if this piece is "checkmate" after it has moved
                    if chess_board.judge_attack_general(game.get_player()):
                        # check if your opponent can save the game, if so, show "Checkmate", otherwise show "victory"
                        if chess_board.judge_win(game.get_player()):
                            game.set_win(game.get_player())
                        else:
                            # If you hit your opponent, the checkmate effect is displayed
                            game.set_attack()
                    # After a move, switch sides
                    game.exchange()
                Check if a piece is clicked
                clicked_chess = Chess.click(game.get_player(), [chess for line in chess_board.map for chess in line if chess])
                if clicked_chess:
                    # create the selected chess object
                    ClickBox(screen, clicked_chess.row, clicked_chess.col, clicked_chess.team)
                    # Clear all previous objects that can be dropped
                    Dot.clean_last_postion()
                    # If you really click on a piece, then calculate the position that the piece can move
                    all_position = chess_board.get_put_down_postion(clicked_chess)
                    if all_position:
                        # Empty the last playable object
                        Dot.clean_last_postion()
                        Create a drop-able child object
                        for position in all_position:
                            Dot(screen, position)

        # Display the game background
        screen.blit(background_img, (0.0))
        screen.blit(background_img, (0.270))
        screen.blit(background_img, (0.540))

        # display the board and the pieces on the board
        chess_board.show()

        # display the clicked pieces
        ClickBox.show()

        # Display removable child objects
        Dot.show()

        Display information about the game
        game.show()

        # Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
        pygame.display.update()

        # FPS (number of frames displayed per second)
        clock.tick(60)  # Through a certain delay, to achieve 1 second can cycle 60 times


if __name__ == '__main__':
    main()
Copy the code