import pygame import chess import sys from src.constants import * pygame.init() screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("Ajedrez Buenos Aires - Fase 1 ♟️") clock = pygame.time.Clock() board = chess.Board() selected_square = None last_move = None running = True font = pygame.font.SysFont("Segoe UI", 32, bold=True) small_font = pygame.font.SysFont("Segoe UI", 20) # Cargar piezas piece_images = {} piece_files = { 'P': 'w_pawn', 'N': 'w_knight', 'B': 'w_bishop', 'R': 'w_rook', 'Q': 'w_queen', 'K': 'w_king', 'p': 'b_pawn', 'n': 'b_knight', 'b': 'b_bishop', 'r': 'b_rook', 'q': 'b_queen', 'k': 'b_king' } try: for symbol, fname in piece_files.items(): img = pygame.image.load(f"assets/pieces/{fname}.png") piece_images[symbol] = pygame.transform.smoothscale(img, (SQUARE_SIZE, SQUARE_SIZE)) print("✅ Piezas cburnett cargadas perfectamente!") except Exception as e: print("⚠️ Modo fallback (letras). Descarga las piezas para verlas lindas.") def draw_board(): for rank in range(8): for file in range(8): color = LIGHT_SQUARE if (rank + file) % 2 == 0 else DARK_SQUARE x = BOARD_LEFT + file * SQUARE_SIZE y = BOARD_TOP + rank * SQUARE_SIZE pygame.draw.rect(screen, color, (x, y, SQUARE_SIZE, SQUARE_SIZE)) # Coordenadas if file == 0: txt = small_font.render(str(8 - rank), True, (80,80,80)) screen.blit(txt, (BOARD_LEFT - 25, y + 5)) if rank == 7: txt = small_font.render(chr(97 + file), True, (80,80,80)) screen.blit(txt, (x + 8, BOARD_TOP + BOARD_SIZE + 8)) def get_square_from_mouse(pos): x, y = pos file = (x - BOARD_LEFT) // SQUARE_SIZE rank = (y - BOARD_TOP) // SQUARE_SIZE if 0 <= file < 8 and 0 <= rank < 8: return chess.square(file, 7 - rank) return None def draw_pieces(): for sq in chess.SQUARES: piece = board.piece_at(sq) if piece: symbol = piece.symbol() file = chess.square_file(sq) rank = 7 - chess.square_rank(sq) x = BOARD_LEFT + file * SQUARE_SIZE y = BOARD_TOP + rank * SQUARE_SIZE if symbol in piece_images: screen.blit(piece_images[symbol], (x, y)) else: # fallback color = (255, 255, 255) if piece.color else (0, 0, 0) txt = font.render(piece.symbol(), True, color) screen.blit(txt, (x + 18, y + 12)) def draw_highlights(): # Último movimiento if last_move: for sq in (last_move.from_square, last_move.to_square): file = chess.square_file(sq) rank = 7 - chess.square_rank(sq) x = BOARD_LEFT + file * SQUARE_SIZE y = BOARD_TOP + rank * SQUARE_SIZE s = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA) s.fill(LAST_MOVE_COLOR) screen.blit(s, (x, y)) # Casilla seleccionada if selected_square is not None: file = chess.square_file(selected_square) rank = 7 - chess.square_rank(selected_square) x = BOARD_LEFT + file * SQUARE_SIZE y = BOARD_TOP + rank * SQUARE_SIZE s = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA) s.fill(SELECT_COLOR) screen.blit(s, (x, y)) # Movimientos legales if selected_square is not None: for move in board.legal_moves: if move.from_square == selected_square: file = chess.square_file(move.to_square) rank = 7 - chess.square_rank(move.to_square) x = BOARD_LEFT + file * SQUARE_SIZE + SQUARE_SIZE//2 y = BOARD_TOP + rank * SQUARE_SIZE + SQUARE_SIZE//2 pygame.draw.circle(screen, (50, 255, 50), (x, y), 18) # ====================== LOOP PRINCIPAL ====================== while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: sq = get_square_from_mouse(event.pos) if sq is None: continue if selected_square is None: piece = board.piece_at(sq) if piece and piece.color == board.turn: selected_square = sq else: move = chess.Move(selected_square, sq) # Promoción automática a reina por ahora (después haremos popup lindo) if board.piece_at(selected_square).piece_type == chess.PAWN and sq in [chess.A8, chess.H8, chess.A1, chess.H1]: move.promotion = chess.QUEEN if move in board.legal_moves: board.push(move) last_move = move selected_square = None else: # Cambiar selección piece = board.piece_at(sq) if piece and piece.color == board.turn: selected_square = sq else: selected_square = None screen.fill((28, 28, 35)) draw_board() draw_highlights() draw_pieces() # Info turn_txt = font.render(f"Turno: {'Blancas ⚪' if board.turn else 'Negras ⚫'}", True, (255, 255, 255)) screen.blit(turn_txt, (50, 10)) if board.is_checkmate(): winner = "Negras" if board.turn else "Blancas" txt = font.render(f"¡JAQUE MATE! Ganaron las {winner} 🏆", True, (255, 60, 60)) screen.blit(txt, (BOARD_LEFT + 30, 10)) elif board.is_game_over(): txt = font.render("¡Tablas! 🤝", True, (255, 220, 60)) screen.blit(txt, (BOARD_LEFT + 120, 10)) pygame.display.flip() clock.tick(FPS) pygame.quit() sys.exit()