Untitled
unknown
plain_text
a year ago
1.7 kB
11
Indexable
import pygame
import chess
import chess.svg
# Initialize pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 800
SQUARE_SIZE = WIDTH // 8
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Load chess images
def load_images():
pieces = ['r', 'n', 'b', 'q', 'k', 'p']
images = {}
for piece in pieces:
for color in ['w', 'b']:
images[color + piece] = pygame.transform.scale(
pygame.image.load(f'images/{color + piece}.png'), (SQUARE_SIZE, SQUARE_SIZE)
)
return images
# Draw the board
def draw_board(screen):
colors = [WHITE, BLACK]
for r in range(8):
for c in range(8):
pygame.draw.rect(screen, colors[(r + c) % 2], pygame.Rect(c * SQUARE_SIZE, r * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
# Draw the pieces
def draw_pieces(screen, board, images):
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
row, col = divmod(square, 8)
screen.blit(images[piece.symbol()], pygame.Rect(col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
# Main loop
def main():
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Chess Game')
clock = pygame.time.Clock()
board = chess.Board()
images = load_images()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLACK)
draw_board(screen)
draw_pieces(screen, board, images)
pygame.display.flip()
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
main()
pip install python-chess pygame
Editor is loading...
Leave a Comment