-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.py
More file actions
605 lines (507 loc) · 24.1 KB
/
chess.py
File metadata and controls
605 lines (507 loc) · 24.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import pygame
import sys
import time
import pygame
import sys
import time
import os
def loading_screen():
# Display the loading screen
screen.fill((0, 0, 0)) # Fill the screen with black (loading screen)
font = pygame.font.Font(None, 74)
text = font.render("Loading...", True, (255, 255, 255))
text_rect = text.get_rect(center=(screen.get_width() // 2, screen.get_height() // 2))
screen.blit(text, text_rect)
pygame.display.update()
def is_king_in_check(king_position, enemy_pieces, enemy_locations):
for i, piece in enumerate(enemy_pieces):
moves = check_valid_moves(piece, enemy_locations[i], 'enemy')
if king_position in moves:
return True
return False
def is_checkmate(king_position, current_pieces, current_locations, enemy_pieces, enemy_locations):
if not is_king_in_check(king_position, enemy_pieces, enemy_locations):
return False # Not in check
for i, piece in enumerate(current_pieces):
moves = check_valid_moves(piece, current_locations[i], 'current')
for move in moves:
original_position = current_locations[i]
current_locations[i] = move
if not is_king_in_check(king_position, enemy_pieces, enemy_locations):
current_locations[i] = original_position
return False
current_locations[i] = original_position
return True # No moves escape the check
pygame.init()
# Initialize game window
# Loading screen function
WIDTH = 1000
HEIGHT = 900
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption('Chess')
font = pygame.font.Font('freesansbold.ttf', 20)
medium_font = pygame.font.Font('freesansbold.ttf', 40)
big_font = pygame.font.Font('freesansbold.ttf', 50)
start_ticks = pygame.time.get_ticks() # Record the start time for the loading screen
loading_duration = 2000 # Duration of loading screen in milliseconds (2 seconds)
font = pygame.font.Font(None, 50)
def format_time(seconds):
minutes = seconds // 60
seconds = seconds % 60
return f"{minutes:02}:{seconds:02}"
white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),
(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]
captured_pieces_white = []
captured_pieces_black = []
turn_step = 0
selection = 100
valid_moves = []
# Initialize pygame
# Check if running in a bundled app (PyInstaller)
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS # PyInstaller stores temporary data here
else:
base_path = os.path.dirname(__file__) # For development mode
# Define the path to the images folder inside the app
image_path = os.path.join(base_path, 'images')
# Load images with the correct paths
black_queen = pygame.image.load(os.path.join(image_path, 'black queen.png'))
black_queen = pygame.transform.scale(black_queen, (80, 80))
black_queen_small = pygame.transform.scale(black_queen, (45, 45))
black_king = pygame.image.load(os.path.join(image_path, 'black king.png'))
black_king = pygame.transform.scale(black_king, (80, 80))
black_king_small = pygame.transform.scale(black_king, (45, 45))
black_rook = pygame.image.load(os.path.join(image_path, 'black rook.png'))
black_rook = pygame.transform.scale(black_rook, (80, 80))
black_rook_small = pygame.transform.scale(black_rook, (45, 45))
black_bishop = pygame.image.load(os.path.join(image_path, 'black bishop.png'))
black_bishop = pygame.transform.scale(black_bishop, (80, 80))
black_bishop_small = pygame.transform.scale(black_bishop, (45, 45))
black_knight = pygame.image.load(os.path.join(image_path, 'black knight.png'))
black_knight = pygame.transform.scale(black_knight, (80, 80))
black_knight_small = pygame.transform.scale(black_knight, (45, 45))
black_pawn = pygame.image.load(os.path.join(image_path, 'black pawn.png'))
black_pawn = pygame.transform.scale(black_pawn, (65, 65))
black_pawn_small = pygame.transform.scale(black_pawn, (45, 45))
white_queen = pygame.image.load(os.path.join(image_path, 'white queen.png'))
white_queen = pygame.transform.scale(white_queen, (80, 80))
white_queen_small = pygame.transform.scale(white_queen, (45, 45))
white_king = pygame.image.load(os.path.join(image_path, 'white king.png'))
white_king = pygame.transform.scale(white_king, (80, 80))
white_king_small = pygame.transform.scale(white_king, (45, 45))
white_rook = pygame.image.load(os.path.join(image_path, 'white rook.png'))
white_rook = pygame.transform.scale(white_rook, (80, 80))
white_rook_small = pygame.transform.scale(white_rook, (45, 45))
white_bishop = pygame.image.load(os.path.join(image_path, 'white bishop.png'))
white_bishop = pygame.transform.scale(white_bishop, (80, 80))
white_bishop_small = pygame.transform.scale(white_bishop, (45, 45))
white_knight = pygame.image.load(os.path.join(image_path, 'white knight.png'))
white_knight = pygame.transform.scale(white_knight, (80, 80))
white_knight_small = pygame.transform.scale(white_knight, (45, 45))
white_pawn = pygame.image.load(os.path.join(image_path, 'white pawn.png'))
white_pawn = pygame.transform.scale(white_pawn, (65, 65))
white_pawn_small = pygame.transform.scale(white_pawn, (45, 45))
# Grouping images
white_images = [white_pawn, white_queen, white_king, white_knight, white_rook, white_bishop]
small_white_images = [white_pawn_small, white_queen_small, white_king_small, white_knight_small,
white_rook_small, white_bishop_small]
black_images = [black_pawn, black_queen, black_king, black_knight, black_rook, black_bishop]
small_black_images = [black_pawn_small, black_queen_small, black_king_small, black_knight_small,
black_rook_small, black_bishop_small]
piece_list = ['pawn', 'queen', 'king', 'knight', 'rook', 'bishop']
counter = 0
winner = ''
game_over = False
def draw_board():
for i in range(32):
column = i % 4
row = i // 4
if row % 2 == 0:
pygame.draw.rect(screen, 'light gray', [600 - (column * 200), row * 100, 100, 100])
else:
pygame.draw.rect(screen, 'light gray', [700 - (column * 200), row * 100, 100, 100])
pygame.draw.rect(screen, 'gray', [0, 800, WIDTH, 100])
pygame.draw.rect(screen, 'gold', [0, 800, WIDTH, 100], 5)
pygame.draw.rect(screen, 'gold', [800, 0, 200, HEIGHT], 5)
status_text = ['White!', 'Move',
'Black', 'Move']
if turn_step == 0 or turn_step == 1: # White's turn
text_color = 'white'
else: # Black's turn
text_color = 'black'
screen.blit(big_font.render(status_text[turn_step], True, text_color), (810, 400))
for i in range(9):
pygame.draw.line(screen, 'black', (0, 100 * i), (800, 100 * i), 2)
pygame.draw.line(screen, 'black', (100 * i, 0), (100 * i, 800), 2)
screen.blit(medium_font.render('FORFEIT', True, 'black'), (810, 830))
def draw_pieces():
for i in range(len(white_pieces)):
index = piece_list.index(white_pieces[i])
if white_pieces[i] == 'pawn':
screen.blit(white_pawn, (white_locations[i][0] * 100 + 22, white_locations[i][1] * 100 + 30))
else:
screen.blit(white_images[index], (white_locations[i][0] * 100 + 10, white_locations[i][1] * 100 + 10))
if turn_step < 2:
if selection == i:
pygame.draw.rect(screen, 'red', [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 + 1,
100, 100], 2)
for i in range(len(black_pieces)):
index = piece_list.index(black_pieces[i])
if black_pieces[i] == 'pawn':
screen.blit(black_pawn, (black_locations[i][0] * 100 + 22, black_locations[i][1] * 100 + 30))
else:
screen.blit(black_images[index], (black_locations[i][0] * 100 + 10, black_locations[i][1] * 100 + 10))
if turn_step >= 2:
if selection == i:
pygame.draw.rect(screen, 'blue', [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 + 1,
100, 100], 2)
def check_options(pieces, locations, turn):
moves_list = []
all_moves_list = []
for i in range((len(pieces))):
location = locations[i]
piece = pieces[i]
if piece == 'pawn':
moves_list = check_pawn(location, turn)
elif piece == 'rook':
moves_list = check_rook(location, turn)
elif piece == 'knight':
moves_list = check_knight(location, turn)
elif piece == 'bishop':
moves_list = check_bishop(location, turn)
elif piece == 'queen':
moves_list = check_queen(location, turn)
elif piece == 'king':
moves_list = check_king(location, turn)
all_moves_list.append(moves_list)
return all_moves_list
def check_king(position, color):
moves_list = []
if color == 'white':
enemies_list = black_locations
friends_list = white_locations
else:
friends_list = black_locations
enemies_list = white_locations
targets = [(1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1), (0, 1), (0, -1)]
for i in range(8):
target = (position[0] + targets[i][0], position[1] + targets[i][1])
if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:
moves_list.append(target)
return moves_list
def check_queen(position, color):
moves_list = check_bishop(position, color)
second_list = check_rook(position, color)
for i in range(len(second_list)):
moves_list.append(second_list[i])
return moves_list
def check_bishop(position, color):
moves_list = []
if color == 'white':
enemies_list = black_locations
friends_list = white_locations
else:
friends_list = black_locations
enemies_list = white_locations
for i in range(4): # up-right, up-left, down-right, down-left
path = True
chain = 1
if i == 0:
x = 1
y = -1
elif i == 1:
x = -1
y = -1
elif i == 2:
x = 1
y = 1
else:
x = -1
y = 1
while path:
if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \
0 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7:
moves_list.append((position[0] + (chain * x), position[1] + (chain * y)))
if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list:
path = False
chain += 1
else:
path = False
return moves_list
def check_rook(position, color):
moves_list = []
if color == 'white':
enemies_list = black_locations
friends_list = white_locations
else:
friends_list = black_locations
enemies_list = white_locations
for i in range(4): # down, up, right, left
path = True
chain = 1
if i == 0:
x = 0
y = 1
elif i == 1:
x = 0
y = -1
elif i == 2:
x = 1
y = 0
else:
x = -1
y = 0
while path:
if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \
0 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7:
moves_list.append((position[0] + (chain * x), position[1] + (chain * y)))
if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list:
path = False
chain += 1
else:
path = False
return moves_list
def check_pawn(position, color):
moves_list = []
if color == 'white':
if (position[0], position[1] + 1) not in white_locations and \
(position[0], position[1] + 1) not in black_locations and position[1] < 7:
moves_list.append((position[0], position[1] + 1))
if (position[0], position[1] + 2) not in white_locations and \
(position[0], position[1] + 2) not in black_locations and position[1] == 1:
moves_list.append((position[0], position[1] + 2))
if (position[0] + 1, position[1] + 1) in black_locations:
moves_list.append((position[0] + 1, position[1] + 1))
if (position[0] - 1, position[1] + 1) in black_locations:
moves_list.append((position[0] - 1, position[1] + 1))
else:
if (position[0], position[1] - 1) not in white_locations and \
(position[0], position[1] - 1) not in black_locations and position[1] > 0:
moves_list.append((position[0], position[1] - 1))
if (position[0], position[1] - 2) not in white_locations and \
(position[0], position[1] - 2) not in black_locations and position[1] == 6:
moves_list.append((position[0], position[1] - 2))
if (position[0] + 1, position[1] - 1) in white_locations:
moves_list.append((position[0] + 1, position[1] - 1))
if (position[0] - 1, position[1] - 1) in white_locations:
moves_list.append((position[0] - 1, position[1] - 1))
return moves_list
def check_knight(position, color):
moves_list = []
if color == 'white':
enemies_list = black_locations
friends_list = white_locations
else:
friends_list = black_locations
enemies_list = white_locations
# 8 squares to check for knights, they can go two squares in one direction and one in another
targets = [(1, 2), (1, -2), (2, 1), (2, -1), (-1, 2), (-1, -2), (-2, 1), (-2, -1)]
for i in range(8):
target = (position[0] + targets[i][0], position[1] + targets[i][1])
if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:
moves_list.append(target)
return moves_list
def check_valid_moves():
if turn_step < 2:
options_list = white_options
else:
options_list = black_options
valid_options = options_list[selection]
return valid_options
def draw_valid(moves):
if turn_step < 2:
color = 'red'
else:
color = 'blue'
for i in range(len(moves)):
pygame.draw.circle(screen, color, (moves[i][0] * 100 + 50, moves[i][1] * 100 + 50), 5)
def draw_captured():
for i in range(len(captured_pieces_white)):
captured_piece = captured_pieces_white[i]
index = piece_list.index(captured_piece)
screen.blit(small_black_images[index], (825, 5 + 50 * i))
for i in range(len(captured_pieces_black)):
captured_piece = captured_pieces_black[i]
index = piece_list.index(captured_piece)
screen.blit(small_white_images[index], (925, 5 + 50 * i))
def draw_check():
if turn_step < 2:
if 'king' in white_pieces:
king_index = white_pieces.index('king')
king_location = white_locations[king_index]
for i in range(len(black_options)):
if king_location in black_options[i]:
if counter < 15:
pygame.draw.rect(screen, 'dark red', [white_locations[king_index][0] * 100 + 1,
white_locations[king_index][1] * 100 + 1, 100, 100], 5)
else:
if 'king' in black_pieces:
king_index = black_pieces.index('king')
king_location = black_locations[king_index]
for i in range(len(white_options)):
if king_location in white_options[i]:
if counter < 15:
pygame.draw.rect(screen, 'dark blue', [black_locations[king_index][0] * 100 + 1,
black_locations[king_index][1] * 100 + 1, 100, 100], 5)
def draw_game_over():
pygame.draw.rect(screen, 'black', [200, 200, 400, 70])
screen.blit(font.render(f'{winner} won the game!', True, 'white'), (210, 210))
screen.blit(font.render(f'Press ENTER to Restart!', True, 'white'), (210, 240))
black_options = check_options(black_pieces, black_locations, 'black')
white_options = check_options(white_pieces, white_locations, 'white')
run = True
# Timer variables
player_1_time = 600 # 10 minutes for Player 1
player_2_time = 600 # 10 minutes for Player 2
current_player = 1 # Player 1 starts
turn_start_time = pygame.time.get_ticks()
loading_displayed = False
start_time = pygame.time.get_ticks() # Initialize start_time here
# Main game loop
while run:
# Timer variables
player_1_time = 600 # 10 minutes for Player 1
player_2_time = 600 # 10 minutes for Player 2
current_player = 1 # Player 1 starts
turn_start_time = pygame.time.get_ticks()
timer = pygame.time.Clock()
fps = 60
timer_x = 850
timer_y_player1 = 20
timer_y_player2 = HEIGHT - 140
turn_step = 0
loading_duration = 2000
start_ticks = pygame.time.get_ticks()
loading_shown = False
while run:
timer.tick(fps)
if not loading_shown:
loading_screen()
if pygame.time.get_ticks() - start_ticks >= loading_duration:
loading_shown = True
# Now proceed to initialize the game after loading screen
player_1_time = 600 # 10 minutes for Player 1
player_2_time = 600 # 10 minutes for Player 2
current_player = 1 # Player 1 starts
turn_start_time = pygame.time.get_ticks()
# Update elapsed time
elapsed_ticks = (pygame.time.get_ticks() - turn_start_time) // 1000
if current_player == 1:
time_left = max(player_1_time - elapsed_ticks, 0)
else:
time_left = max(player_2_time - elapsed_ticks, 0)
# Check for time out
if time_left == 0:
winner = "Black" if current_player == 1 else "White"
game_over = True
# Draw the game board and timers
screen.fill('dark gray')
draw_board()
draw_pieces()
draw_captured()
draw_check()
# Display the timer
if current_player == 1:
text_color = "White"
if current_player == 2:
text_color = "Black"
player_1_timer = font.render(f" {player_1_time // 60}:{player_1_time % 60:02}", True, text_color)
player_2_timer = font.render(f" {player_2_time // 60}:{player_2_time % 60:02}", True, text_color)
# Blit the timer texts to the right side of the screen
screen.blit(player_1_timer, (timer_x, timer_y_player1)) # White timer at top right
screen.blit(player_2_timer, (timer_x, timer_y_player2)) # Black time
if selection != 100:
valid_moves = check_valid_moves()
draw_valid(valid_moves)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:
x_coord = event.pos[0] // 100
y_coord = event.pos[1] // 100
click_coords = (x_coord, y_coord)
if turn_step <= 1: # Player 1's turn
if click_coords in white_locations:
selection = white_locations.index(click_coords)
if turn_step == 0:
turn_step = 1
if click_coords in valid_moves and selection != 100:
white_locations[selection] = click_coords
if click_coords in black_locations:
black_piece = black_locations.index(click_coords)
captured_pieces_white.append(black_pieces[black_piece])
if black_pieces[black_piece] == 'king':
winner = 'White'
game_over = True
black_pieces.pop(black_piece)
black_locations.pop(black_piece)
black_options = check_options(black_pieces, black_locations, 'black')
white_options = check_options(white_pieces, white_locations, 'white')
king_position = black_locations[black_pieces.index('king')]
player_1_time -= elapsed_ticks
current_player = 2
turn_start_time = pygame.time.get_ticks()
turn_step = 2
selection = 100
valid_moves = []
elif turn_step > 1: # Player 2's turn
if click_coords in black_locations:
selection = black_locations.index(click_coords)
if turn_step == 2:
turn_step = 3
if click_coords in valid_moves and selection != 100:
black_locations[selection] = click_coords
if click_coords in white_locations:
white_piece = white_locations.index(click_coords)
captured_pieces_black.append(white_pieces[white_piece])
if white_pieces[white_piece] == 'king':
winner = 'Black'
game_over = True
white_pieces.pop(white_piece)
white_locations.pop(white_piece)
black_options = check_options(black_pieces, black_locations, 'black')
white_options = check_options(white_pieces, white_locations, 'white')
king_position = white_locations[white_pieces.index('king')]
player_2_time -= elapsed_ticks
current_player = 1
turn_start_time = pygame.time.get_ticks()
turn_step = 0
selection = 100
valid_moves = []
if event.type == pygame.KEYDOWN and game_over:
if event.key == pygame.K_RETURN:
# Reset game and timers
game_over = False
winner = ''
player_1_time = 600
player_2_time = 600
current_player = 1
turn_start_time = pygame.time.get_ticks()
# Reset game state
white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),
(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]
captured_pieces_white = []
captured_pieces_black = []
turn_step = 0
selection = 100
valid_moves = []
black_options = check_options(black_pieces, black_locations, 'black')
white_options = check_options(white_pieces, white_locations, 'white')
if winner != '':
game_over = True
draw_game_over()
pygame.display.flip()
pygame.quit()