import pygame import chess import chess.engine Initialize Pygame pygame.init() Constants…
- User
import pygame import chess import chess.engine
Initialize Pygame
pygame.init()
Constants
WIDTH, HEIGHT = 900, 600 SQ_SIZE = 60 EVAL_BAR_WIDTH = 60 BUTTON_HEIGHT = 40 BUTTON_WIDTH = 80 IMAGES = {} MOVE_DELAY_TIME = 1000 # Delay in milliseconds
Colors
LIGHT_SQ_COLOR = pygame.Color("#F0D9B5") DARK_SQ_COLOR = pygame.Color("#B58863") HIGHLIGHT_COLOR = pygame.Color("yellow") PLAYER_MOVE_COLOR = pygame.Color("blue") ENGINE_MOVE_COLOR = pygame.Color("red") TOP_LINE_COLOR = pygame.Color((255, 255, 0, 100))
def load_images(): pieces = ['wp', 'wN', 'wB', 'wR', 'wQ', 'wK', 'bp', 'bN', 'bB', 'bR', 'bQ', 'bK'] for piece in pieces: try: image_path = f"F:/Chess Piece Images/{piece}.png" IMAGES[piece] = pygame.transform.scale(pygame.image.load(image_path), (SQ_SIZE, SQ_SIZE)) except pygame.error: print(f"Error loading image: {piece}.png from {image_path}")
def draw_board(screen): colors = [LIGHT_SQ_COLOR, DARK_SQ_COLOR] for r in range(8): for c in range(8): color = colors[(r + c) % 2] pygame.draw.rect(screen, color, pygame.Rect(EVAL_BAR_WIDTH + c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
def highlight_square(screen, square, color, width=5): if square is not None: row, col = divmod(square, 8) pygame.draw.rect(screen, color, pygame.Rect(EVAL_BAR_WIDTH + col * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE), width)
def draw_pieces(screen, board, moves=[], pv_moves=[]): piece_mapping = { 'P': 'wp', 'N': 'wN', 'B': 'wB', 'R': 'wR', 'Q': 'wQ', 'K': 'wK', 'p': 'bp', 'n': 'bN', 'b': 'bB', 'r': 'bR', 'q': 'bQ', 'k': 'bK' } for sq in chess.SQUARES: piece = board.piece_at(sq) if piece: piece_image = piece_mapping[piece.symbol()] screen.blit(IMAGES[piece_image], (EVAL_BAR_WIDTH + chess.square_file(sq) * SQ_SIZE, chess.square_rank(sq) * SQ_SIZE))
for move in moves: highlight_square(screen, move.from_square, PLAYER_MOVE_COLOR if move in moves else ENGINE_MOVE_COLOR) highlight_square(screen, move.to_square, PLAYER_MOVE_COLOR if move in moves else ENGINE_MOVE_COLOR) for move in pv_moves: highlight_square(screen, move.from_square, TOP_LINE_COLOR, width=2) highlight_square(screen, move.to_square, TOP_LINE_COLOR, width=2)def draw_button(screen): pygame.draw.rect(screen, pygame.Color("gray"), pygame.Rect(720, 50, BUTTON_WIDTH, BUTTON_HEIGHT)) font = pygame.font.Font(None, 36) label = font.render("Show PV", True, pygame.Color("white")) screen.blit(label, (720 + (BUTTON_WIDTH - label.get_width()) // 2, 50 + (BUTTON_HEIGHT - label.get_height()) // 2))
def handle_button_click(mouse_pos, top_lines): if pygame.Rect(720, 50, BUTTON_WIDTH, BUTTON_HEIGHT).collidepoint(mouse_pos): return top_lines return []
def show_pv_line(screen, board, pv_line): for move in pv_line: board.push(move) draw_board(screen) draw_pieces(screen, board) pygame.display.flip() pygame.time.delay(MOVE_DELAY_TIME // 2) for _ in pv_line: board.pop()
def main(): screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Chess GUI") clock = pygame.time.Clock() load_images()
- Assistant
- User
pygame 2.6.0 (SDL 2.28.4, Python 3.12.2) Hello from the pygame community. https://www.pygame.org/contribute.html <UciProtocol (pid=10444)>: stderr >> _ <UciProtocol (pid=10444)>: stderr >> | _ | | <UciProtocol (pid=10444)>: stderr >> |_ |_ || v0.31.0 built Jun 16 2024 <UciProtocol (pid=10444)>: stderr >> Found pb network file: G:\LcZero\lc0-v0.31.0-windows-cpu-dnnl/791556.pb.gz <UciProtocol (pid=10444)>: stderr >> Creating backend [blas]... <UciProtocol (pid=10444)>: stderr >> Detected 2 core(s) and 4 thread(s) in 1 group(s). <UciProtocol (pid=10444)>: stderr >> Group 0 has 2 core(s) and 4 thread(s). <UciProtocol (pid=10444)>: stderr >> BLAS functions from DNNL version 1.5.0 <UciProtocol (pid=10444)>: stderr >> BLAS max batch size is 256. Traceback (most recent call last): File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach\inference.py", line 192, in main() File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach\inference.py", line 182, in main show_selected_pv_move(screen, board, pv_moves, current_pv_index) File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach\inference.py", line 101, in show_selected_pv_move board_copy.push(pv_moves[i]) File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach.venv\Lib\site-packages\chess_.py", line 2259, in push assert piece_type is not None, f"push() expects move to be pseudo-legal, but got {move} in {self.board_fen()}" AssertionError: push() expects move to be pseudo-legal, but got e2e4 in rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR
- Assistant
- User
the code is working fine. When ever a piece moves those squares should be highlighted just like in chess.com implement this in code. The PV line should be shown on board before engine make its move .
- Assistant
- User
- Assistant
- User
at Replay button section PV line should loaded for current move after each move.
- Assistant
- User
The PV line Squares are highlighting but i want pieces should move to visualize
- Assistant
- User
pygame 2.6.0 (SDL 2.28.4, Python 3.12.2) Hello from the pygame community. https://www.pygame.org/contribute.html <UciProtocol (pid=5200)>: stderr >> _ <UciProtocol (pid=5200)>: stderr >> | _ | | <UciProtocol (pid=5200)>: stderr >> |_ |_ |_| v0.31.0 built Jun 16 2024 <UciProtocol (pid=5200)>: stderr >> Found pb network file: G:\LcZero\lc0-v0.31.0-windows-cpu-dnnl/791556.pb.gz <UciProtocol (pid=5200)>: stderr >> Creating backend [blas]... <UciProtocol (pid=5200)>: stderr >> Detected 2 core(s) and 4 thread(s) in 1 group(s). <UciProtocol (pid=5200)>: stderr >> Group 0 has 2 core(s) and 4 thread(s). <UciProtocol (pid=5200)>: stderr >> BLAS functions from DNNL version 1.5.0 <UciProtocol (pid=5200)>: stderr >> BLAS max batch size is 256. Traceback (most recent call last): File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach\inference.py", line 199, in main() File "C:\Users\rajes\PycharmProjects\Project_Chess-A.I_Coach\inference.py", line 178, in main current_pv_index, pv_moves = handle_pv_navigation(event, current_pv_index, len(pv_moves), pv_moves, board, engine) ^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot unpack non-iterable int object
- Assistant
- User
when i click on show replay the pygame window is not responding after few moves
- Assistant