-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.py
More file actions
283 lines (243 loc) · 10.4 KB
/
Copy pathchess.py
File metadata and controls
283 lines (243 loc) · 10.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import pygame
import sys
pygame.init()
# Constants
WIDTH, HEIGHT = 640, 640
BOARD_SIZE = 8
SQUARE_SIZE = WIDTH // BOARD_SIZE
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_SQUARE = (240, 217, 181)
DARK_SQUARE = (181, 136, 99)
HIGHLIGHT = (247, 247, 105, 150)
MOVE_HIGHLIGHT = (106, 168, 79, 150)
# display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Python Chess")
clock = pygame.time.Clock()
# Load piece images
def load_pieces():
pieces = {}
for color in ['w', 'b']:
for piece in ['p', 'r', 'n', 'b', 'q', 'k']:
img = pygame.image.load(f"chess_pieces/{color}{piece}.png")
img = pygame.transform.scale(img, (SQUARE_SIZE, SQUARE_SIZE))
pieces[f"{color}{piece}"] = img
return pieces
# colored circles as placeholders for pieces
pieces_img = {}
for color in ['w', 'b']:
for piece in ['p', 'r', 'n', 'b', 'q', 'k']:
surf = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
font = pygame.font.SysFont('Arial', 36)
text = font.render(piece.upper(), True, WHITE if color == 'b' else BLACK)
text_rect = text.get_rect(center=(SQUARE_SIZE//2, SQUARE_SIZE//2))
pygame.draw.circle(surf, BLACK if color == 'w' else WHITE, (SQUARE_SIZE//2, SQUARE_SIZE//2), SQUARE_SIZE//2 - 5)
pygame.draw.circle(surf, WHITE if color == 'w' else BLACK, (SQUARE_SIZE//2, SQUARE_SIZE//2), SQUARE_SIZE//2 - 8)
surf.blit(text, text_rect)
pieces_img[f"{color}{piece}"] = surf
# board initialization
def init_board():
board = [['' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
# Set up pawns
for col in range(BOARD_SIZE):
board[1][col] = 'bp'
board[6][col] = 'wp'
# Set up other pieces
back_row = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
for col in range(BOARD_SIZE):
board[0][col] = 'b' + back_row[col]
board[7][col] = 'w' + back_row[col]
return board
# Draw the board
def draw_board():
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
color = LIGHT_SQUARE if (row + col) % 2 == 0 else DARK_SQUARE
pygame.draw.rect(screen, color, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
# Draw the pieces on the board
def draw_pieces(board):
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
piece = board[row][col]
if piece:
screen.blit(pieces_img[piece], (col * SQUARE_SIZE, row * SQUARE_SIZE))
# Highlight selected piece and possible moves
def draw_highlights(selected, valid_moves):
if selected:
row, col = selected
highlight = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
highlight.fill(HIGHLIGHT)
screen.blit(highlight, (col * SQUARE_SIZE, row * SQUARE_SIZE))
for move_row, move_col in valid_moves:
highlight = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE), pygame.SRCALPHA)
highlight.fill(MOVE_HIGHLIGHT)
screen.blit(highlight, (move_col * SQUARE_SIZE, move_row * SQUARE_SIZE))
# Check if the king is still alive
def is_king_alive(board, color):
king = color + 'k'
for row in board:
if king in row:
return True
return False
def draw_winner_message(message):
font = pygame.font.SysFont('Arial', 64, bold=True)
text = font.render(message, True, (255, 0, 0))
rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
screen.blit(text, rect)
pygame.display.flip()
pygame.time.wait(10000) # Wait 10 seconds before closing
# Get valid moves for a piece
def get_valid_moves(board, row, col, turn):
piece = board[row][col]
if not piece or piece[0] != turn:
return []
piece_type = piece[1]
moves = []
# Pawn moves
if piece_type == 'p':
direction = -1 if turn == 'w' else 1
# Move forward one square
if 0 <= row + direction < BOARD_SIZE and not board[row + direction][col]:
moves.append((row + direction, col))
# Move forward two squares from starting position
if (turn == 'w' and row == 6) or (turn == 'b' and row == 1):
if not board[row + 2*direction][col]:
moves.append((row + 2*direction, col))
# Capture diagonally
for c_offset in [-1, 1]:
if 0 <= col + c_offset < BOARD_SIZE and 0 <= row + direction < BOARD_SIZE:
if board[row + direction][col + c_offset] and board[row + direction][col + c_offset][0] != turn:
moves.append((row + direction, col + c_offset))
# Rook moves
elif piece_type == 'r':
for dr, dc in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
for i in range(1, BOARD_SIZE):
r, c = row + i*dr, col + i*dc
if not (0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE):
break
if not board[r][c]:
moves.append((r, c))
else:
if board[r][c][0] != turn:
moves.append((r, c))
break
# Knight moves
elif piece_type == 'n':
for dr, dc in [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]:
r, c = row + dr, col + dc
if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:
if not board[r][c] or board[r][c][0] != turn:
moves.append((r, c))
# Bishop moves
elif piece_type == 'b':
for dr, dc in [(1, 1), (1, -1), (-1, 1), (-1, -1)]:
for i in range(1, BOARD_SIZE):
r, c = row + i*dr, col + i*dc
if not (0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE):
break
if not board[r][c]:
moves.append((r, c))
else:
if board[r][c][0] != turn:
moves.append((r, c))
break
# Queen moves
elif piece_type == 'q':
# Rook-like moves
for dr, dc in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
for i in range(1, BOARD_SIZE):
r, c = row + i*dr, col + i*dc
if not (0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE):
break
if not board[r][c]:
moves.append((r, c))
else:
if board[r][c][0] != turn:
moves.append((r, c))
break
# Bishop-like moves
for dr, dc in [(1, 1), (1, -1), (-1, 1), (-1, -1)]:
for i in range(1, BOARD_SIZE):
r, c = row + i*dr, col + i*dc
if not (0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE):
break
if not board[r][c]:
moves.append((r, c))
else:
if board[r][c][0] != turn:
moves.append((r, c))
break
# King moves
elif piece_type == 'k':
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if dr == 0 and dc == 0:
continue
r, c = row + dr, col + dc
if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:
if not board[r][c] or board[r][c][0] != turn:
moves.append((r, c))
return moves
# Main game loop
def main():
board = init_board()
selected = None
valid_moves = []
turn = 'w' # White goes first
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
col = event.pos[0] // SQUARE_SIZE
row = event.pos[1] // SQUARE_SIZE
if selected:
if (row, col) in valid_moves:
# Move the piece
board[row][col] = board[selected[0]][selected[1]]
board[selected[0]][selected[1]] = ''
# pawn promotion
moved_piece = board[row][col]
if moved_piece[1] == 'p' and (row == 0 or row == 7):
board[row][col] = moved_piece[0] + 'q'
# Check for win condition
if not is_king_alive(board, 'b'):
draw_winner_message("White Wins!")
running = False
elif not is_king_alive(board, 'w'):
draw_winner_message("Black Wins!")
running = False
break
# Switch turns
turn = 'b' if turn == 'w' else 'w'
# Reset selection
selected = None
valid_moves = []
else:
# Select a new piece
if board[row][col] and board[row][col][0] == turn:
selected = (row, col)
valid_moves = get_valid_moves(board, row, col, turn)
else:
selected = None
valid_moves = []
else:
# Select a piece if it's the correct turn
if board[row][col] and board[row][col][0] == turn:
selected = (row, col)
valid_moves = get_valid_moves(board, row, col, turn)
# Draw everything
draw_board()
draw_highlights(selected, valid_moves)
draw_pieces(board)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()