ahira-justice / chess-board

A chess board library for presenting game positions
https://pypi.org/project/chess-board
GNU General Public License v3.0
19 stars 12 forks source link

Update added overlay to allow more easily modify the code. #8

Open Aphexus opened 1 year ago

Aphexus commented 1 year ago

def update(fen, game_board, overlay = None): check_for_quit() game_board.update_pieces(fen) if not overlay is None: overlay(game_board.display_surf) pygame.display.update() fps_clock.tick(FPS)

It would be nice if the board could be dynamically resized

maybe I will modify that later.

Aphexus commented 1 year ago

from chessboard.constants import IMAGE_DIR, FPS, STARTING_FEN, WINDOW_CAPTION, WINDOW_WIDTH, WINDOW_HEIGHT from chessboard.pieces import Piece, PieceColor, PieceType from chessboard.fenparser import FenParser from chessboard.utils import is_odd

class Board: board_rect = [[(100+50i,100+50j) for j in range(8)] for i in range(8)]

b_tile = pygame.image.load(os.path.join(IMAGE_DIR, 'btile.png'))
w_tile = pygame.image.load(os.path.join(IMAGE_DIR, 'wtile.png'))

def __init__(self, bg_color, display_surf):
    self.bg_color = bg_color
    self.display_surf = display_surf

    self.piece_rect = []

    original_cell_size = 50
    new_cell_size = 100
    r = new_cell_size/original_cell_size;

    Board.board_rect = [[(100+new_cell_size*j,100+new_cell_size*i) for j in range(8)] for i in range(8)] 
    global WINDOW_WIDTH
    global WINDOW_HEIGHT        

    WINDOW_WIDTH = r*WINDOW_WIDTH
    WINDOW_HEIGHT = r*WINDOW_HEIGHT
    Board.b_tile = pygame.transform.smoothscale_by(Board.b_tile, r)
    Board.w_tile = pygame.transform.smoothscale_by(Board.w_tile, r)
    images = ["b_bishop", "b_king", "b_knight", "b_pawn", "b_queen", "b_rook", "w_bishop", "w_king", "w_knight", "w_pawn", "w_queen", "w_rook"]
    for i in range(0, len(images)):            
        s = "Piece." + images[i] + " = pygame.transform.smoothscale_by(Piece." + images[i] + ", " + str(r) + ")"
        print(s)
        exec(s)

This will allow one to scale the board by adjusting new_cell_size.

I'm not able to set the window width and height in proportion for some reason(I haven't used python in many years so a bit rusty