[Aide] Python : jeu d'echec
Résolu/Fermé
A voir également:
- Jeu d'echec python
- 94 jeu - Télécharger - Puzzle & Réflexion
- Mode sans echec - Guide
- Ps4 mode sans echec - Guide
- Jeu zuma - Télécharger - Jeux vidéo
- Citizen code python avis - Accueil - Outils
1 réponse
jee pee
Messages postés
40461
Date d'inscription
mercredi 2 mai 2007
Statut
Modérateur
Dernière intervention
19 novembre 2024
9 420
Modifié le 16 mars 2023 à 01:13
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()
16 mars 2023 à 03:43
merci beaucoup ça marche a la perfection !