Bonjour, je suis en grosse galere et je n'arrive pas a finaliser min code de jeu d'echecs,je recherche de l'aide desperement qlqn peut m'aider svp :
import pygame
from .Pieces import *
from .constant import *
class newBoard:
def __init__(self, Width, Height, Rows, Cols, Square, win):
self.Width = Width
self.Height = Height
self.Square = Square
self.win = win
self.Rows = Rows
self.Cols = Cols
self.Board = []
self.create_Board()
def create_Board(self):
for row in range(self.Rows):
self.Board.append([0 for i in range(self.Cols)])
for col in range(self.Cols):
if row == 1:
self.Board[row][col] = Pawn(self.Square, Black_Pawn, Black, "Pawn", row, col)
if row == 6:
self.Board[row][col] = Pawn(self.Square, White_Pawn, Black, "Pawn", row, col)
if row == 0:
if col == 0 or col == 7:
self.Board[row][col] = Rook(self.Square, Black_Rook, Black, "Rook", row, col)
if col == 6 or col == 1:
self.Board[row][col] = Knight(self.Square, Black_Knight, Black, "Knight", row, col)
if col == 5 or col == 2:
self.Board[row][col] = Bishop(self.Square, Black_Bishop, Black, "Bishop", row, col)
if col == 3:
self.Board[row][col] = Queen(self.Square, Black_Queen, Black, "Queen", row, col)
if col == 4:
self.Board[row][col] = King(self.Square, Black_King, Black, "King", row, col)
if row == 7:
if col == 0 or col == 7:
self.Board[row][col] = Rook(self.Square, White_Rook, White, "Rook", row, col)
if col == 6 or col == 1:
self.Board[row][col] = Knight(self.Square, White_Knight, White, "Knight", row, col)
if col == 5 or col == 2:
self.Board[row][col] = Bishop(self.Square, White_Bishop, White, "Bishop", row, col)
if col == 3:
self.Board[row][col] = Queen(self.Square, White_Queen, White, "Queen", row, col)
if col == 4:
self.Board[row][col] = King(self.Square, White_King, White, "King", row, col)
def get_piece(self, row, col):
return self.Board[row][col]
def moove(self, piece, row, col):
self.Board[piece.row][piece.col], self.Board[row][col] = self.Board[row][col], self.Board[piece.row][piece.col]
piece.piece_moove(row, col)
if piece.type == "Pawn":
if piece.first_moove:
piece.first_moove = False
def draw_Board(self, win):
win.fill(Brown)
for row in range(self.Rows):
for col in range(self.Cols):
color = White if (row + col) % 2 == 0 else Brown
pygame.draw.rect(win, color, (col * self.Square, row * self.Square, self.Square, self.Square))
for row in range(self.Rows):
for col in range(self.Cols):
if self.Board[row][col] != 0:
piece = self.Board[row][col]
x, y = col * self.Square, row * self.Square
piece.draw_piece(win, x, y)
def draw_pieces(self):
for row in range(self.Rows):
for col in range(self.Cols):
if self.Board[row][col] != 0:
piece = self.Board[row][col]
piece.draw_piece(self.win)
</code>
<code>import pygame
import os
Width, Height= 760, 760
Rows, Cols = 8, 8
Square = Width//Rows
path = "Chess_game\chess_image"
Brown = (87,16,16)
White = (255,255,255)
Green = (0,255,0)
Black = (0,0,0)
#piece_noir
Black_Knight = pygame.transform.scale(pygame.image.load(os.path.join(path, "cheval.png")),(Square, Square))
Black_King = pygame.transform.scale(pygame.image.load(os.path.join(path, "king.png")),(Square, Square))
Black_Bishop = pygame.transform.scale(pygame.image.load(os.path.join(path, "fou.png")),(Square, Square))
Black_Pawn = pygame.transform.scale(pygame.image.load(os.path.join(path, "petit_pion.png")),(Square, Square))
Black_Rook = pygame.transform.scale(pygame.image.load(os.path.join(path, "tour.png")),(Square, Square))
Black_Queen = pygame.transform.scale(pygame.image.load(os.path.join(path, "queen.png")),(Square, Square))
#piece blanche
White_Knight = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_cheval.png")),(Square, Square))
White_King = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_king.png")),(Square, Square))
White_Bishop = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_fou.png")),(Square, Square))
White_Pawn = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_pion.png")),(Square, Square))
White_Rook = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_tour.png")),(Square, Square))
White_Queen = pygame.transform.scale(pygame.image.load(os.path.join(path, "white_queen.png")),(Square, Square))</code>
<code>import pygame
from .Board import *
from .constant import *
from copy import deepcopy
class Game:
def __init__(self, Width, Height, Rows, Cols, Square, win):
self.win = win
self.Board = newBoard(Width, Height, Rows, Cols, Square, win)
self.Square = Square
self.selected = None
self.turn = White
self.valid_moves = []
self.Black_pieces_left = 16
self.White_pieces_left = 16
def update_window(self):
self.Board.draw_Board(self.win)
self.Board.draw_pieces()
self.draw_available_moves()
pygame.display.update()
def check_game(self):
if self.checkmate(self.Board):
if self.turn == White:
print("Black Wins")
return True
else:
print("White wins")
return True
def enemmies_moves(self, piece, Board):
enemmies_moves = []
for r in range(len(Board)):
for c in range(len(Board[r])):
if Board[r][c] != 0:
if Board[r][c].color != piece.color:
moves = Board[r][c].get_available_moves(r,c, Board)
for move in moves:
enemmies_moves.append(move)
return enemmies_moves
def get_King_pos(self, Board):
for r in range(len(Board)):
for c in range(len(Board)):
if Board[r][c] != 0:
if Board[r][c].type == "King" and Board[r][c].color == self.turn:
return (r,c)
def possible_moves(self, Board):
possible_moves = []
for r in range(len(Board)):
for c in range(len(Board[r])):
if Board[r][c] != 0:
if Board[r][c].color == self.turn and Board[r][c].type != "King":
moves = Board[r][c].get_available_moves(r,c, Board)
for move in moves:
possible_moves.append(move)
return possible_moves
def checkmate(self, Board):
king_pos = self.get_King_pos(Board.Board)
get_King = Board.get_piece(king_pos[0], king_pos[1])
king_available_moves = set(get_King.get_available_moves(king_pos[0], king_pos[1], Board.Board))
enemies_moves_set = set(self.enemmies_moves(get_King, Board.Board))
king_moves = king_available_moves - enemies_moves_set
set1 = king_available_moves.intersection(enemies_moves_set)
possible_moves_to_def = set1.intersection(self.possible_moves(Board.Board))
if not king_moves and king_available_moves and not possible_moves_to_def:
return True
return False
def simulate_move(self, piece, row, col):
piece_row, piece_col = piece.row, piece.col
save_piece = self.Board.Board[row][col]
if self.Board.Board[row][col] != 0:
self.Board.Board[row][col] = 0
self.Board.Board[piece.row][piece.col], self.Board.Board[row][col] = self.Board.Board[row][col], self.Board.Board[piece.row][piece.col]
king_pos = self.get_King_pos(self.Board.Board)
if king_pos in self.enemies_moves(piece, self.Board):
piece.row, piece.col = piece.row, piece_col
self.Board[piece_row][piece_col] = piece
self.Board.Board[row][col] = save_piece
return False
piece.row, piece.col = piece_row, piece_col
self.Board.Board[piece_row][piece_col] = piece
self.Board.Board[row][col] = save_piece
return True
def reset(self):
self.Board = newBoard(Width, Height, Rows, Cols, Square, win)
self.selected = None
self.Black_pieces_left = 16
self.White_pieces_left = 16
self.valid_moves = []
def change_turn(self):
if self.turn == White:
self.turn = Black
else:
self.turn = White
def select(self, row,col):
if self.selected:
move = self._move(row, col)
if not move:
self.selected = None
self.selected(row, col)
piece = self.Board.get_piece(row, col)
if piece != 0 and self.turn == piece.color:
self.selected = piece
self.valid_moves = piece.get_available_moves(row, col, self.Board.Board)
def _move(self, row, col):
piece = self.Board.get_piece(row, col)
if self.selected and (row, col) in self.valid_moves:
if piece == 0 or piece.color != self.selected.color:
if self.simulate_move(self.selected, row, col):
self.remove(self.selected, row, col)
self.moove(piece, row, col)
self.change_turn()
self.valid_moves = []
self.selected = None
return True
return False
return False
def remove(self, Board, piece, row, col):
if piece != 0:
Board[row][col] = 0
if piece.color == White:
self.White_pieces_left -= 1
else:
self.Black_pieces_left -= 1
def draw_available_moves(self):
if len(self.valid_moves) > 0:
for pos in self.valid_moves:
row, col = pos[0], pos[1]
pygame.draw.circle(self.win, Green, (col*self,Square + self.Square//2), self.Square//8)
</code>
<code>import pygame
from .constant import *
from .Board import *
class Piece:
def __init__(self, Square, image,color, type, row,col):
self.Square = Square
self.image = image
self.color = color
self.row = row
self.col = col
self.type = type
self.x = 0
self.y = 0
self.availables_moves = []
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def piece_moove(self,row,col):
self.row = row
self.col = col
self.calc_pos()
def calc_pos(self):
self.x = self.col*self.Square
self.y = self.row*self.Square
def clear_available_moves(self):
if len(self.availables_moves) > 0:
self.availables_moves = []
class Pawn(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
self.first_moove = True
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
if self.color == White:
if row-1 >= 0:
if Board[row-1][col] == 0:
self.availables_moves.append((row-1, col))
if self.first_moove:
if Board[row-1][col] == 0:
self.availables_moves.append((row-2, col))
if col-1 >= 0:
if Board[row+1][col-1] != 0:
piece = Board[row+2][col-1]
if piece.color != self.color:
self.availables_moves.append((row+1, col-1))
if col+1 < len(Board):
if Board[row+1][col+1] != 0:
piece = Board[row+1][col+1]
if piece.color != self.color:
self.availables_moves.append((row+1,col+1))
return self.availables_moves
class Rook(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
for i in range(row+1,8):
if Board[i][col] == 0:
self.availables_moves.append((i,col))
else:
if Board[i][col].color != self.color:
self.availables_moves.append((i,col))
break
else:
break
for j in range(row-1,-1,-1):
if Board[j][col] == 0:
self.availables_moves.append((j,col))
else:
if Board[j][col].color != self.color:
self.availables_moves.append((j,col))
break
else:
break
for i in range(col+1,8):
if Board[row][i] == 0:
self.availables_moves.append((row,i))
else:
if Board[row][i].color:
self.availables_moves.append((row,i))
break
else:
break
for i in range(col-1,-1,-1):
if Board[row][i] == 0:
self.availables_moves.append((row,i))
else:
if Board[row][i].color:
self.availables_moves.append((row,i))
break
else:
break
return self.availables_moves
class Bishop(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
row_i = row+1
col_i = col+1
while row_i <= 7 and col_i <= 7:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i += 1
col_i += 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row-1
col_i = col-1
while row_i >= 0 and col_i >= 0:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i -= 1
col_i -= 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row+1
col_i = col-1
while row_i <= 7 and col_i >=0:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i += 1
col_i -= 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row-1
col_i = col+1
while row_i >= 0 and col_i <= 7:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i -= 1
col_i += 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
return self.availables_moves
class Knight(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
if row-2 >= 0 and col+1 < len(Board):
if Board[row-2][col+1] == 0 or Board[row-2][col+1].color != self.color:
self.availables_moves.append((row-2, col+1))
if row-1 >= 0 and col+2 < len(Board[0]):
if Board[row-1][col+2] == 0 or Board[row-1][col+2].color != self.color:
self.availables_moves.append((row-1, col+2))
if row+1 < len(Board) and col+2 < len(Board):
if Board[row+1][col+2] == 0 or Board[row+1][col+2].color != self.color:
self.availables_moves.append((row+1, col+2))
if row+2 < len(Board) and col+1 < len(Board):
if Board[row+2][col+1] == 0 or Board[row+2][col+1].color != self.color:
self.availables_moves.append((row+2, col+1))
if row+2 < len(Board) and col-1 >= 0:
if Board[row+2][col-1] == 0 or Board[row+2][col-1].color != self.color:
self.availables_moves.append((row+2, col-1))
if row+1 < len(Board) and col-2 >= 0:
if Board[row+1][col-2] == 0 or Board[row+1][col-2].color != self.color:
self.availables_moves.append((row+1, col-2))
if row-1 >= 0 and col-2 >= 0:
if Board[row-1][col-2] == 0 or Board[row-1][col-2].color != self.color:
self.availables_moves.append((row-1, col-2))
if row-2 >=0 and col-1 >= 0:
if Board[row-2][col-1] == 0 or Board[row-2][col-1].color != self.color:
self.availables_moves.append((row-2, col-1))
return self.availables_moves
class Queen(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
row_i = row+1
col_i = col+1
while row_i <= 7 and col_i <= 7:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i += 1
col_i += 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row-1
col_i = col-1
while row_i >= 0 and col_i >= 0:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i -= 1
col_i -= 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row+1
col_i = col-1
while row_i <= 7 and col_i >=0:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i += 1
col_i -= 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
row_i = row-1
col_i = col+1
while row_i >= 0 and col_i <= 7:
if Board[row_i][col_i] == 0:
self.availables_moves.append((row_i,col_i))
row_i -= 1
col_i += 1
else:
if Board[row_i][col_i].color != self.color:
self.availables_moves.append((row_i,col_i))
break
else:
break
for i in range(row+1,8):
if Board[i][col] == 0:
self.availables_moves.append((i,col))
else:
if Board[i][col].color != self.color:
self.availables_moves.append((i,col))
break
else:
break
for j in range(row-1,-1,-1):
if Board[j][col] == 0:
self.availables_moves.append((j,col))
else:
if Board[j][col].color != self.color:
self.availables_moves.append((j,col))
break
else:
break
for i in range(col+1,8):
if Board[row][i] == 0:
self.availables_moves.append((row,i))
else:
if Board[row][i].color:
self.availables_moves.append((row,i))
break
else:
break
for i in range(col-1,-1,-1):
if Board[row][i] == 0:
self.availables_moves.append((row,i))
else:
if Board[row][i].color:
self.availables_moves.append((row,i))
break
else:
break
return self.availables_moves
class King(Piece):
def __init__(self, Square, image, color, type, row, col):
super().__init__(Square, image, color, type, row, col)
def draw_piece(self, win):
x = self.col * self.Square
y = self.row * self.Square
win.blit(self.image, (x, y))
def get_available_moves(self, row, col, Board):
self.clear_available_moves()
if row-1 >= 0:
if Board[row-1][col] == 0 or Board[row-1][col].color != self.color:
self.availables_moves.append((row-1,col))
if row-1 >= 0 and col+1 < len(Board):
if Board[row-1][col+1] == 0 or Board[row-1][col+1].color != self.color:
self.availables_moves.append((row-1,col+1))
if col+1 < len(Board):
if Board[row][col+1] == 0 or Board[row][col+1].color != self.color:
self.availables_moves.append((row,col+1))
if row+1 <len(Board) and col+1 < len(Board):
if Board[row+1][col+1] == 0 or Board[row+1][col+1].color != self.color:
self.availables_moves.append((row+1,col+1))
if row+1 < len(Board):
if Board[row+1][col] == 0 or Board[row+1][col].color != self.color:
self.availables_moves.append((row+1,col))
if row+1 <len(Board) and col-1 >= 0:
if Board[row+1][col-1] == 0 or Board[row+1][col-1].color != self.color:
self.availables_moves.append((row+1,col-1))
if col-1 >=0:
if Board[row][col-1] == 0 or Board[row][col-1].color != self.color:
self.availables_moves.append((row,col-1))
if row-1 >= 0 and col-1 >= 0:
if Board[row-1][col-1] == 0 or Board[row-1][col-1].color != self.color:
self.availables_moves.append((row-1,col-1))
return self.availables_moves
</code>
<code>import pygame
from Chess_game.constant import *
from Chess_game.Game import Game
from Chess_game.Board import newBoard
pygame.init()
clock = pygame.time.Clock()
win = pygame.display.set_mode((Width, Height))
def get_position(x, y):
row = y // Square
col = x // Square
return row, col
def main():
run = True
FPS = 60
Cols = 8
Rows = 8
game = Game(Width, Height, Rows, Cols, Square, win)
game_over = False
while run:
clock.tick(FPS)
game.update_window()
if game.check_game():
game_over = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and game_over:
game.reset()
game_over = False # Réinitialisez le drapeau game_over
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
if pygame.mouse.get_pressed()[0]:
location = pygame.mouse.get_pos()
row, col = get_position(location[0], location[1])
game.select(row, col)
game._move(row, col)
main()
Windows / Chrome 121.0.0.0
Afficher la suite
6 févr. 2024 à 18:58
desole je n'ai pas vraiment realiser, je n'en pouvais plus de voir ce projet presque fini et de ne pas pouvoir trouver une bete erreur qui n'est surment pas tres compliquer a modifier