hcm444 / chess

0 stars 0 forks source link

promotion to other options then queen #3

Closed hcm444 closed 7 months ago

hcm444 commented 7 months ago

https://en.wikipedia.org/wiki/Promotion_(chess)

hcm444 commented 7 months ago
def pawn_promotion(self, row, col, win):
    self.valid_moves = {}  # Reset valid_moves dictionary
    promotion_options = {"queen": 5, "knight": 2, "bishop": 3, "rook": 4}  # Piece name to integer mapping
    promotion_index = 1  # Start at the first promotion option

    # Display piece options (queen, knight, bishop, rook)
    piece_images = {
        "queen": pygame.transform.scale(pygame.image.load("static/white_queen.png"), (SQUARE_SIZE, SQUARE_SIZE)),
        "knight": pygame.transform.scale(pygame.image.load("static/white_knight.png"), (SQUARE_SIZE, SQUARE_SIZE)),
        "bishop": pygame.transform.scale(pygame.image.load("static/white_bishop.png"), (SQUARE_SIZE, SQUARE_SIZE)),
        "rook": pygame.transform.scale(pygame.image.load("static/white_rook.png"), (SQUARE_SIZE, SQUARE_SIZE)),
    }

    run = True
    while run:
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    promotion_index = (promotion_index - 1) % len(promotion_options)
                elif event.key == pygame.K_RIGHT:
                    promotion_index = (promotion_index + 1) % len(promotion_options)
                elif event.key == pygame.K_RETURN:
                    promotion_piece = list(promotion_options.values())[promotion_index]
                    self.board[row * 8 + col] = promotion_piece
                    run = False

        # Redraw the board after promotion
        self.draw_board(win)
        self.draw_pieces(win)

        # Redraw the square with the appropriate background color
        if (row + col) % 2 == 0:
            pygame.draw.rect(win, GREY, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
        else:
            pygame.draw.rect(win, WHITE, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))

        # Display the selected promotion piece with the proper background color
        selected_piece = list(promotion_options.keys())[promotion_index]
        selected_piece_img = pygame.transform.scale(
            pygame.image.load(f"static/white_{selected_piece}.png"),
            (SQUARE_SIZE, SQUARE_SIZE))
        win.blit(selected_piece_img, (col * SQUARE_SIZE, row * SQUARE_SIZE))

        pygame.display.update()
hcm444 commented 7 months ago

issue fixed, use right and left arrows to select proper piece