[Aide] Python : jeu d'echec

Résolu
Smoodie - Modifié le 15 mars 2023 à 21:43
 Smoodie - 16 mars 2023 à 03:43

Bonjour,

J'aimerais créer un jeu d'echec en python (seulement avec pygame/sdl2 sans python-chess),

mais malheureuseent j suis deja coincé dans le positionnement des pieces, je n'arrive pas a faire en sorte de bien positionner les pieces selon la cases, par exemple e4 serait a x = ... et y = ..

mais j'ai du mal a situer avec pygame, surtout au niveau de x, voila le resultat actuel :
 

voila le code (ne faite ps attention a la variable turn elle est censé instauré la notion de tours, par exemple turn = 0 le tour des blanc, mais si turn = 1 alors ceux des noirs, ne faites egalement pas très attention au classes, elle sont pour l'instant pas trop utilisé pour quoi ils ont été)

import pygame


class Board:
    def __init__(self, size=8):
        self.size = size
        self.grid = [['' for _ in range(size)] for _ in range(size)]

    def get_square(self, x, y):
        return self.grid[y][x]

    def set_square(self, x, y, piece):
        self.grid[y][x] = piece

    def is_square_empty(self, x, y):
        return self.get_square(x, y) == ''

    def is_valid_square(self, x, y):
        return 0 <= x < self.size and 0 <= y < self.size

    def clear(self):
        self.grid = [['' for _ in range(self.size)] for _ in range(self.size)]


class Piece:
    def __init__(self, image, position):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x, self.rect.y = position


class Pawn(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Rook(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Knight(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Bishop(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Queen(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class King(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


def load():
    board = pygame.image.load('assets/board.png')
    window.blit(board, (0, 0))
    white_pieces = {}
    black_pieces = {}
    turn = 0

    # Créer les pions blancs
    for i in range(8):
        name = f"pawn_{i}_white"
        image = pygame.image.load("assets/white/pawn.png")
        position = ((i + 0.5) * 75, 510)  # calculer la position centrale de la case
        white_pieces[name] = Pawn(image, position)

    # Créer les pions noirs
    for i in range(8):
        name = f"pawn_{i}_black"
        image = pygame.image.load("assets/black/pawn.png")
        position = ((i + 0.5) * 70,  87)  # calculer la position centrale de la case
        black_pieces[name] = Pawn(image, position)

    # Créer les autres pièces blanches
    white_pieces["rook_0_white"] = Rook(pygame.image.load("assets/white/rook.png"), (0, 560))
    white_pieces["rook_1_white"] = Rook(pygame.image.load("assets/white/rook.png"), (560, 560))
    white_pieces["knight_0_white"] = Knight(pygame.image.load("assets/white/knight.png"), (70, 560))
    white_pieces["knight_1_white"] = Knight(pygame.image.load("assets/white/knight.png"), (490, 560))
    white_pieces["bishop_0_white"] = Bishop(pygame.image.load("assets/white/bishop.png"), (140, 560))
    white_pieces["bishop_1_white"] = Bishop(pygame.image.load("assets/white/bishop.png"), (420, 560))
    white_pieces["queen_white"] = Queen(pygame.image.load("assets/white/queen.png"), (280, 560))
    white_pieces["king_white"] = King(pygame.image.load("assets/white/king.png"), (210, 560))

    # Créer les autres pièces noires
    black_pieces["rook_0_black"] = Rook(pygame.image.load("assets/black/rook.png"), (0, 0))
    black_pieces["rook_1_black"] = Rook(pygame.image.load("assets/black/rook.png"), (560, 0))
    black_pieces["knight_0_black"] = Knight(pygame.image.load("assets/black/knight.png"), (70, 0))
    black_pieces["knight_1_black"] = Knight(pygame.image.load("assets/black/knight.png"), (490, 0))
    black_pieces["bishop_0_black"] = Bishop(pygame.image.load("assets/black/bishop.png"), (140, 0))
    black_pieces["bishop_1_black"] = Bishop(pygame.image.load("assets/black/bishop.png"), (420, 0))
    black_pieces["queen_black"] = Queen(pygame.image.load("assets/black/queen.png"), (280, 0))
    black_pieces["king_black"] = King(pygame.image.load("assets/black/king.png"), (210, 0))

    return board, white_pieces, black_pieces, turn


def display_pieces(white_pieces, black_pieces):
    for piece in white_pieces.values():
        window.blit(piece.image, piece.rect)
    for piece in black_pieces.values():
        window.blit(piece.image, piece.rect)


def main():
    board, white_pieces, black_pieces, turn = load()
    display_pieces(white_pieces, black_pieces)
    pygame.display.flip()
    # Boucle principale
    running = True
    while running:
        # Gestion des événements
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # Affichage du plateau et des pièces
        window.blit(board, (0, 0))
        for piece in white_pieces.values():
            window.blit(piece.image, piece.rect)
        for piece in black_pieces.values():
            window.blit(piece.image, piece.rect)

        pygame.display.flip()

    pygame.quit()


if __name__ == '__main__':
    pygame.init()
    window_size = (670, 670)
    window = pygame.display.set_mode(window_size)
    pygame.display.set_caption("pyChess")
    main()
A voir également:

1 réponse

jee pee Messages postés 39349 Date d'inscription mercredi 2 mai 2007 Statut Modérateur Dernière intervention 29 février 2024 9 193
Modifié le 16 mars 2023 à 01:13

Bonjour,

Ton plateau est constitué de carrés de 84X84 pixels, donc l'image de fond et la window doivent faire 672X672. Tu fais des images des pièces faisant toutes aussi 84x84 et dans ton programme tu positionnes les pièces sur des multiples de 84, en x et y

import pygame


class Board:
    def __init__(self, size=8):
        self.size = size
        self.grid = [['' for _ in range(size)] for _ in range(size)]

    def get_square(self, x, y):
        return self.grid[y][x]

    def set_square(self, x, y, piece):
        self.grid[y][x] = piece

    def is_square_empty(self, x, y):
        return self.get_square(x, y) == ''

    def is_valid_square(self, x, y):
        return 0 <= x < self.size and 0 <= y < self.size

    def clear(self):
        self.grid = [['' for _ in range(self.size)] for _ in range(self.size)]


class Piece:
    def __init__(self, image, position):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x, self.rect.y = position


class Pawn(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Rook(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Knight(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Bishop(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class Queen(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


class King(Piece):
    def __init__(self, image, position):
        super().__init__(image, position)


def load():
    board = pygame.image.load('assets/board.png')
    window.blit(board, (0, 0))
    white_pieces = {}
    black_pieces = {}
    turn = 0

    # Créer les pions blancs
    for i in range(8):
        name = f"pawn_{i}_white"
        image = pygame.image.load("assets/white/pawn.png")
        position = (i * 84, 6*84)  
        white_pieces[name] = Pawn(image, position)

    # Créer les pions noirs
    for i in range(8):
        name = f"pawn_{i}_black"
        image = pygame.image.load("assets/black/pawn.png")
        position = (i * 84, 84)  
        black_pieces[name] = Pawn(image, position)

    # Créer les autres pièces blanches
    white_pieces["rook_0_white"] = Rook(pygame.image.load("assets/white/rook.png"), (0*84, 7*84))
    white_pieces["rook_1_white"] = Rook(pygame.image.load("assets/white/rook.png"), (7*84, 7*84))
    white_pieces["knight_0_white"] = Knight(pygame.image.load("assets/white/knight.png"), (1*84, 7*84))
    white_pieces["knight_1_white"] = Knight(pygame.image.load("assets/white/knight.png"), (6*84, 7*84))
    white_pieces["bishop_0_white"] = Bishop(pygame.image.load("assets/white/bishop.png"), (2*84, 7*84))
    white_pieces["bishop_1_white"] = Bishop(pygame.image.load("assets/white/bishop.png"), (5*84, 7*84))
    white_pieces["queen_white"] = Queen(pygame.image.load("assets/white/queen.png"), (3*84, 7*84))
    white_pieces["king_white"] = King(pygame.image.load("assets/white/king.png"), (4*84, 7*84))

    # Créer les autres pièces noires
    black_pieces["rook_0_black"] = Rook(pygame.image.load("assets/black/rook.png"), (0*84, 0))
    black_pieces["rook_1_black"] = Rook(pygame.image.load("assets/black/rook.png"), (7*84, 0))
    black_pieces["knight_0_black"] = Knight(pygame.image.load("assets/black/knight.png"), (1*84, 0))
    black_pieces["knight_1_black"] = Knight(pygame.image.load("assets/black/knight.png"), (6*84, 0))
    black_pieces["bishop_0_black"] = Bishop(pygame.image.load("assets/black/bishop.png"), (2*84, 0))
    black_pieces["bishop_1_black"] = Bishop(pygame.image.load("assets/black/bishop.png"), (5*84, 0))
    black_pieces["queen_black"] = Queen(pygame.image.load("assets/black/queen.png"), (3*84, 0))
    black_pieces["king_black"] = King(pygame.image.load("assets/black/king.png"), (4*84, 0))

    return board, white_pieces, black_pieces, turn


def display_pieces(white_pieces, black_pieces):
    for piece in white_pieces.values():
        window.blit(piece.image, piece.rect)
    for piece in black_pieces.values():
        window.blit(piece.image, piece.rect)


def main():
    board, white_pieces, black_pieces, turn = load()
    display_pieces(white_pieces, black_pieces)
    pygame.display.flip()
    # Boucle principale
    running = True
    while running:
        # Gestion des événements
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # Affichage du plateau et des pièces
        window.blit(board, (0, 0))
        for piece in white_pieces.values():
            window.blit(piece.image, piece.rect)
        for piece in black_pieces.values():
            window.blit(piece.image, piece.rect)

        pygame.display.flip()

    pygame.quit()


if __name__ == '__main__':
    pygame.init()
    window_size = (670, 670)
    window = pygame.display.set_mode(window_size)
    pygame.display.set_caption("pyChess")
    main()

0

merci beaucoup ça marche a la perfection ! 

0