-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew_pygame.py
More file actions
189 lines (160 loc) · 8.36 KB
/
Copy pathNew_pygame.py
File metadata and controls
189 lines (160 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pygame
import os
from move_gen import get_valid_moves
# Constants
pygame.init()
FPS = 60
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = 850
WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
CLOCK = pygame.time.Clock()
STARTING_POSTIONS = [ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"]
MID_OF_SQUARES = [(37.5, 37.5),(112.5, 37.5),(187.5, 37.5),(262.5, 37.5),(337.5, 37.5),(412.5, 37.5),(487.5, 37.5),(562.5, 37.5),(37.5, 112.5),(112.5, 112.5),(187.5, 112.5),(262.5, 112.5),(337.5, 112.5),(412.5, 112.5),(487.5, 112.5),(562.5, 112.5),(37.5, 187.5),(112.5, 187.5),(187.5, 187.5),(262.5, 187.5),(337.5, 187.5),(412.5, 187.5),(487.5, 187.5),(562.5, 187.5),(37.5, 262.5),(112.5, 262.5),(187.5, 262.5),(262.5, 262.5),(337.5, 262.5),(412.5, 262.5),(487.5, 262.5),(562.5, 262.5),(37.5, 337.5),(112.5, 337.5),(187.5, 337.5),(262.5, 337.5),(337.5, 337.5),(412.5, 337.5),(487.5, 337.5),(562.5, 337.5),(37.5, 412.5),(112.5, 412.5),(187.5, 412.5),(262.5, 412.5),(337.5, 412.5),(412.5, 412.5),(487.5, 412.5),(562.5, 412.5),(37.5, 487.5),(112.5, 487.5),(187.5, 487.5),(262.5, 487.5),(337.5, 487.5),(412.5, 487.5),(487.5, 487.5),(562.5, 487.5),(37.5, 562.5),(112.5, 562.5),(187.5, 562.5),(262.5, 562.5),(337.5, 562.5),(412.5, 562.5),(487.5, 562.5),(562.5, 562.5)]
SQUARE_SIZE = min(WINDOW_WIDTH // 2, WINDOW_HEIGHT) // 8
running = True
# Constants: Pieces # FOR SIZE CHANGE GO TO https://www.shutterstock.com/image-resizer?c3apidt=&gad_source=1&gclid=CjwKCAjwt-OwBhBnEiwAgwzrUgwi70N7tIRmT0xeUDt_DHljvXD5qifqYuvsM2rliDfAjuR8b78PWRoCKygQAvD_BwE&gclsrc=aw.ds&kw=
p = pygame.image.load("ChessPyGame Assets\\blackPawn.png")
P = pygame.image.load('ChessPyGame Assets\\whitePawn.png')
n = pygame.image.load('ChessPyGame Assets\\blackNight.png')
N = pygame.image.load('ChessPyGame Assets\\whiteNight.png')
b = pygame.image.load('ChessPyGame Assets\\blackBISHOP.png')
B = pygame.image.load('ChessPyGame Assets\\whiteBISHOP.png')
r = pygame.image.load('ChessPyGame Assets\\blackRook.png')
R = pygame.image.load('ChessPyGame Assets\\whiteRook.png')
q = pygame.image.load('ChessPyGame Assets\\blackQueen.png')
Q = pygame.image.load('ChessPyGame Assets\\whiteQueen.png')
k = pygame.image.load('ChessPyGame Assets\\blackKING.png')
K = pygame.image.load('ChessPyGame Assets\\whiteKING.png')
YELLOW_RECT = pygame.Rect(0, 0, 75, 75)
GREEN = (115, 149, 82)
WHITE = (235, 236, 208)
BOARD_WIDTH = 8 * SQUARE_SIZE
BOARD_HEIGHT = 8 * SQUARE_SIZE
BOARD_START_X = (WINDOW_WIDTH - BOARD_WIDTH) // 2
BOARD_START_Y = (WINDOW_HEIGHT - BOARD_HEIGHT) // 2
FONT = pygame.font.Font(None, 36)
def get_piece_from_square(square):
return reverse_chessboard(current_Board)[square[1] * 8 + square[0]]
def get_square_from_mouse(pos):
x, y = pos
if BOARD_START_X <= x <= BOARD_START_X + BOARD_WIDTH and BOARD_START_Y <= y <= BOARD_START_Y + BOARD_HEIGHT:
col = (x - BOARD_START_X) // SQUARE_SIZE
row = (y - BOARD_START_Y) // SQUARE_SIZE
row = 7 - (y - BOARD_START_Y) // SQUARE_SIZE # Flip the row index
return int(col), int(row)
return None, None
def reverse_chessboard(chessboard):
# Validate that the input is 64 elements long
if len(chessboard) != 64:
raise ValueError("The chessboard must have exactly 64 elements.")
# Split the flat list into 8 rows
rows = [chessboard[i:i+8] for i in range(0, len(chessboard), 8)]
# Reverse the order of the rows
revised_board = rows[::-1]
# Flatten the revised board back into a 1D list
flattened_board = [piece for row in revised_board for piece in row]
return flattened_board
def fen_to_Chess_board(board):
parts = board.split(' ')
ChessBoard = []
copy_board = parts[0]
copy_board = [i for i in copy_board if i != '/']
turn = parts[1]
castles = parts[2]
en_passant = parts[3]
half_moves = parts[4]
full_moves = parts[5]
for notion in copy_board:
if notion.isnumeric():
for i in range(int(notion)):
ChessBoard.append('0')
else:
ChessBoard.append(notion)
return ChessBoard
def chess_board_pos_to_fen_pos(chess_board):
fen, empty_count = '', 0
for i in range(0, 64, 8):
row = chess_board[i:i + 8]
for piece in row:
if piece == '0':
empty_count += 1
else:
if empty_count > 0:
fen += str(empty_count)
empty_count = 0
fen += piece
if empty_count > 0:
fen += str(empty_count)
empty_count = 0
fen += '/'
return fen[:-1]
def DrawBoard(real_board):
game_board = real_board.copy()
WINDOW.fill("aqua")
mouse_pos = pygame.mouse.get_pos()
col, row = get_square_from_mouse(mouse_pos)
# Update label with the current column and row
col_row_text = f'cols and rows: {col+1}, {row+1}' if col is not None and row is not None else 'N/A'
WINDOW.blit(FONT.render(f'Mouse pos: {mouse_pos}', True, "darkgray"), (WINDOW_WIDTH - 500, 0))
WINDOW.blit(FONT.render(col_row_text, True, "darkgray"), (WINDOW_WIDTH - 300, 200))
if selected_square is not None:
WINDOW.blit(FONT.render(f'{get_piece_from_square(selected_square)}', True, "darkgray"), (WINDOW_WIDTH - 300, 300))
WINDOW.blit(FONT.render(f"{moves_text}", True, "darkgray"), (WINDOW_WIDTH - 300, 400))
for row in range(8):
for col in range(8):
square_number = row * 8 + col
currentRect = BOARD_START_X + col * SQUARE_SIZE, BOARD_START_Y + row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE
# Draw Squares
if (row + col) % 2 == 0: # Check if the square is even
pygame.draw.rect(WINDOW, GREEN, currentRect) # Draw a green square
else:
pygame.draw.rect(WINDOW, WHITE, currentRect) # Draw a white square
if selected_square != (None, None) and selected_square != None:
if selected_square[1] * 8 + selected_square[0] == (7 - row) * 8 + col:
pygame.draw.rect(WINDOW, "red", currentRect)
# Handle Pieces
if game_board[square_number] != "0":
piece_dict = {'K': K, 'k': k, 'P': P, "p": p, 'R': R, 'r': r, 'N': N, 'n': n, 'B': B, 'b': b, 'Q': Q, 'q': q}
WINDOW.blit(piece_dict[game_board[square_number]], currentRect)
def MainGameLoop():
global selected_square
global current_Board
global moves_text
has_selected = False
start_end_squares = [None,None]
while True:
CLOCK.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button click
# If nothing is selected yet, handle the first click
if get_square_from_mouse(pygame.mouse.get_pos()) != (None, None) and not has_selected:
selected_square = get_square_from_mouse(pygame.mouse.get_pos())
start_end_squares[0] = selected_square
has_selected = True
piece = get_piece_from_square(selected_square)
valid_moves = get_valid_moves(current_Board,castles,en_passant,piece,selected_square)
moves_text = f'From: {selected_square} TO: {valid_moves}'
# If something is already selected, handle the second click
elif has_selected and get_square_from_mouse(pygame.mouse.get_pos()) != (None, None):
start_end_squares[1] = get_square_from_mouse(pygame.mouse.get_pos())
has_selected = False
DrawBoard(current_Board) # Call the DrawBoard function to draw the chessboard
pygame.display.flip() # Update the display
# RUN THE GAME
moves_text = "Moves: "
current_Board = fen_to_Chess_board(STARTING_POSTIONS[0])
turn = 'w'
castles = "KQkq"
en_passant = "-"
half_moves = 0
full_moves = 1
selected_square = None
MainGameLoop()