-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1004 lines (833 loc) · 47.6 KB
/
main.py
File metadata and controls
1004 lines (833 loc) · 47.6 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
import asyncio
# Game logic uses 1920x1080, but thats too big for most browser windows.
VIRTUAL_RES = (1920, 1080)
REAL_RES = (1280, 720)
SCALE_X = VIRTUAL_RES[0] / REAL_RES[0]
SCALE_Y = VIRTUAL_RES[1] / REAL_RES[1]
# this is the board list and is the numerical version of the board and used along side the rectangles to inform where
# where all the pieces are and what they can do and so on.
board_list = [12, 13, 14, 15, 16, 14, 13, 12,
11, 11, 11, 11, 11, 11, 11, 11,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 3, 4, 5, 6, 4, 3, 2]
# I've assigned each piece and color a number. 1 is white pawn, 2 is white rook and so on.
# The black pieces are the same numbers as the white pieces except with a 1 in front.
# 11 is black pawn, 12 is black rook and so on.
# Might want to combine white and black pieces
class WhitePiece:
"""Class for all the white pieces"""
def __init__(self, image, piece_index, piece_name, piece_num):
self.image = image
self.piece_index = piece_index
self.column = self.piece_index % 8 # calculates the column that the piece is on
self.row = self.piece_index // 8 # ^ but for row
x = 560 + self.column * 100 # turns the column number into the pixel coordinate on the display for the board
y = 140 + self.row * 100 # ^ but for row
self.coordinate = x, y
self.rect = self.image.get_rect(topleft=self.coordinate) # using the pixel coordinate on the board, the rect is
# assigned it as it's top left coordinate
self.turn = 1 # this is how many turns the piece has been used
self.possible_moves = [] # all possible moves the piece can make will be in this list
self.possible_captures = [] # ^ but for possible captures
self.piece_name = piece_name # a string value with the name of the type of piece
self.piece_num = piece_num # this is a number assigned to all the pieces that identifies the type of piece
# it is, as well as it's color
def create_moves(self):
column = self.piece_index % 8
row = self.piece_index // 8
def add_pos_move_rook(start, stop, step):
"""Creates possible moves and captures for the rook piece"""
for i in range(start, stop, step): # in this loop, i represents a possible index that the rook could move
new_column = i % 8 # calculates the column and the row with the possible move index
new_row = i // 8
if board_list[i] == 0: # checks if the spot is empty
self.possible_moves.append([new_column, new_row]) # adds the move to the possible move list
elif board_list[i] > 6: # checks if the spot is taken by an enemy piece
self.possible_captures.append([new_column, new_row]) # adds the piece to the possible capture list
break # breaks the loop because the rook cannot move any further after having a possible piece
# to capture
else: # this only activates if their is a friendly piece on the spot that is checked
break # the loop is broken because the rook cannot move on or post a friendly piece
def add_pos_move_knight_king(poss_index):
"""Creates possible moves and captures for the knight and king pieces"""
new_column = poss_index % 8 # uses the possible index to calculate the column and row
new_row = poss_index // 8
if board_list[poss_index] == 0: # checks if the possible index space is empty
self.possible_moves.append([new_column, new_row]) # adds the move to the possible move list
elif board_list[poss_index] > 6: # checks if the possible index space has an enemy piece
self.possible_captures.append([new_column, new_row]) # adds a capture to the possible capture list
# if neither of the above are activated, that means there is a friendly piece in that spot
if self.piece_name == "pawn": # the piece name is used so each piece can have different movement mechanics
# without them all needing their own class because the only thing they do differently is the movement
# creating possible moves:
if self.piece_index - 8 > -1: # checks if it is in the top row of the chess board,
# if it is, the pawn can't move
if board_list[self.piece_index - 8] == 0: # - 8 is checking the spot right above the piece
# checks if the spot is empty, if it is then it is a possible move
self.possible_moves.append([column, row - 1]) # adds the move to the possible move list
if self.turn == 1 and board_list[self.piece_index - 16] == 0:
# on a pawns first turn it can move 2 spaces forward, so if it is, an additional move is added
self.possible_moves.append([column, row - 2]) # adds the move to the possible move list
# creating possible captures":
if self.piece_index - 9 > -1: # checks if it is in the top row of the chess board
# -9 is checking the top left spot above the piece
if board_list[self.piece_index - 9] > 6 and column > 0:
self.possible_captures.append([column - 1, row - 1]) # adds the move to the capture move list
if self.piece_index - 7 > -1: # checks if it is in the top row of the chess board
# -7 is checking the top right spot above the piece
if board_list[self.piece_index - 7] > 6 and column < 7:
self.possible_captures.append([column + 1, row - 1]) # adds the move to the capture move list
elif self.piece_name == "rook":
add_pos_move_rook(self.piece_index + 1, row * 8 + 8, 1) # all left moves
add_pos_move_rook(self.piece_index - 1, row * 8 - 1, -1) # all right moves
add_pos_move_rook(self.piece_index + 8, 63, 8) # all downward moves
add_pos_move_rook(self.piece_index - 8, -1, -8) # all upward moves
elif self.piece_name == "knight":
if self.piece_index + 16 + 1 < 64 and self.column < 7: # down, right
# checks if the night is too close to the bottom of the board to move down and to the right
# with self.column < 7 it also checks whether it is in the correct column to even have it has a
# possible move/capture
possible_index = self.piece_index + 16 + 1 # if the above is true, then it becomes a possible index
add_pos_move_knight_king(possible_index) # the efficacy of the index is tested with this function
if self.piece_index + 16 - 1 < 64 and self.column > 0: # down, left
possible_index = self.piece_index + 16 - 1
add_pos_move_knight_king(possible_index)
if self.piece_index - 16 + 1 > -1 and self.column < 7: # up, left
possible_index = self.piece_index - 16 + 1
add_pos_move_knight_king(possible_index)
if self.piece_index - 16 - 1 > -1 and self.column > 0: # up, right
possible_index = self.piece_index - 16 - 1
add_pos_move_knight_king(possible_index)
if self.piece_index + 2 + 8 < 64 and self.column < 6: # right, down
possible_index = self.piece_index + 2 + 8
add_pos_move_knight_king(possible_index)
if self.piece_index + 2 - 8 > -1 and self.column < 6: # right, up
possible_index = self.piece_index + 2 - 8
add_pos_move_knight_king(possible_index)
if self.piece_index - 2 + 8 < 64 and self.column > 1: # left, down
possible_index = self.piece_index - 2 + 8
add_pos_move_knight_king(possible_index)
if self.piece_index - 2 - 8 > -1 and self.column > 1: # left, up
possible_index = self.piece_index - 2 - 8
add_pos_move_knight_king(possible_index)
elif self.piece_name == "bishop":
# bot left
if self.column > 0: # the bishop cannot move left if it is in the left most side of the board
for i in range(self.piece_index + 7, 64, 7): # goes through the indices of all the bottom left spots
new_column = i % 8
new_row = i // 8
if board_list[i] == 0: # checks if the spot is empty
self.possible_moves.append([new_column, new_row]) # added to the possible moves list
if new_column > 0: # checks if the column of the spot is not the left most column
self.possible_moves.append([new_column, new_row]) # added to the possible moves list
elif new_column == 0: # checks if the column of the spot is the left most column
self.possible_moves.append([new_column, new_row]) # the left most spot is added
break # and the loop is broken because you can't go any more left
elif board_list[i] > 6: # checks if the spot is an enemy piece
self.possible_captures.append([new_column, new_row]) # the spot is added to the poss cap list
break # the loop is broken because the piece can't go through or past the enemy piece
else: # this activates when it is a friendly piece in the way
break # the loop is broken because the piece can't go through or past the friendly piece
if self.column < 7:
for i in range(self.piece_index - 7, -1, -7): # top right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index + 9, 64, 9): # bot right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column > 0:
for i in range(self.piece_index - 9, -1, -9): # top left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
elif self.piece_name == "king":
# left movements
if self.column > 0: # checks if the it is not on the left most side of the board
for i in [-9, -1, 7]: # those three numbers represent the three left movements that the king can make
# in this case the movements are -9 to the top left, -1 is left, and 7 is bottom left
if i < 0 and self.piece_index + i > -1: # checks if the movement is possible on the board
possible_index = self.piece_index + i # if the above is true, then it is a possible move
add_pos_move_knight_king(possible_index) # the possible move/capture is tested/created
elif i > 0 and self.piece_index + i < 64: # checks if the movement is possible on the board
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index) # the possible move/capture is tested/created
if self.column < 7: # right movements
for i in [9, 1, -7]:
if i < 0 and self.piece_index + i > -1:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif i > 0 and self.piece_index + i < 64:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
for i in [-8, 8]: # up and down
if i < 0 and self.piece_index + i > -1:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif i > 0 and self.piece_index + i < 64:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif self.piece_name == "queen":
# up, down, left, and right movements (copy pasted from the rook):
add_pos_move_rook(self.piece_index + 1, row * 8 + 8, 1) # all left moves
add_pos_move_rook(self.piece_index - 1, row * 8 - 1, -1) # all right moves
add_pos_move_rook(self.piece_index + 8, 63, 8) # all downward moves
add_pos_move_rook(self.piece_index - 8, -1, -8) # all upward moves
# diagonals (copy pasted from the bishop):
# bot left
if self.column > 0: # the bishop cannot move left if it is in the left most side of the board
for i in range(self.piece_index + 7, 64, 7): # goes through the indices of all the bottom left spots
new_column = i % 8
new_row = i // 8
if board_list[i] == 0: # checks if the spot is empty
self.possible_moves.append([new_column, new_row]) # added to the possible moves list
if new_column > 0: # checks if the column of the spot is not the left most column
self.possible_moves.append([new_column, new_row]) # added to the possible moves list
elif new_column == 0: # checks if the column of the spot is the left most column
self.possible_moves.append([new_column, new_row]) # the left most spot is added
break # and the loop is broken because you can't go any more left
elif board_list[i] > 6: # checks if the spot is an enemy piece
self.possible_captures.append([new_column, new_row]) # the spot is added to the poss cap list
break # the loop is broken because the piece can't go through or past the enemy piece
else: # this activates when it is a friendly piece in the way
break # the loop is broken because the piece can't go through or past the friendly piece
if self.column < 7:
for i in range(self.piece_index - 7, -1, -7): # top right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index + 9, 64, 9): # bot right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column > 0:
for i in range(self.piece_index - 9, -1, -9): # top left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif board_list[i] > 6:
self.possible_captures.append([new_column, new_row])
break
else:
break
def undo_create_moves(self):
self.possible_moves = []
self.possible_captures = []
def move(self, new_index):
self.piece_index = new_index
self.column = self.piece_index % 8
self.row = self.piece_index // 8
x = 560 + self.column * 100
y = 140 + self.row * 100
self.coordinate = x, y
self.rect.topleft = self.coordinate
self.possible_moves = []
self.possible_captures = []
self.turn += 1
# this class is basically the same as the WhitePiece class with a few number changes to account for the fact that they
# are going in the opposite direction so I did not bother commenting on it
class BlackPiece:
def __init__(self, image, piece_index, piece, piece_num):
self.image = image
self.piece_index = piece_index
self.column = self.piece_index % 8
self.row = self.piece_index // 8
x = 560 + self.column * 100
y = 140 + self.row * 100
self.coordinate = x, y
self.rect = self.image.get_rect(topleft=self.coordinate)
self.turn = 1
self.possible_moves = []
self.possible_captures = []
self.piece_name = piece
self.piece_num = piece_num
def create_moves(self):
column = self.piece_index % 8
row = self.piece_index // 8
def add_pos_move_knight_king(poss_index):
new_column = poss_index % 8
new_row = poss_index // 8
if board_list[poss_index] == 0:
self.possible_moves.append([new_column, new_row])
elif 0 < board_list[poss_index] < 7:
self.possible_captures.append([new_column, new_row])
def add_pos_move_rook(start, stop, step):
for i in range(start, stop, step): # left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.piece_name == "pawn":
# creating possible moves
if self.piece_index + 8 < 64:
if board_list[self.piece_index + 8] == 0:
self.possible_moves.append([column, row + 1])
if self.turn == 1 and board_list[self.piece_index + 16] == 0:
self.possible_moves.append([column, row + 2])
# creating possible captures
if self.piece_index + 9 < 64:
if 0 < board_list[self.piece_index + 9] < 7 and column < 7:
self.possible_captures.append([column + 1, row + 1])
if self.piece_index + 7 < 64:
if 0 < board_list[self.piece_index + 7] < 7 and column > 0:
self.possible_captures.append([column - 1, row + 1])
elif self.piece_name == "rook":
add_pos_move_rook(self.piece_index + 1, row * 8 + 8, 1) # left
add_pos_move_rook(self.piece_index - 1, row * 8 - 1, -1) # right
add_pos_move_rook(self.piece_index + 8, 63, 8) # down
add_pos_move_rook(self.piece_index - 8, -1, -8) # up
elif self.piece_name == "knight":
if self.piece_index + 16 + 1 < 64 and self.column < 7: # down right
possible_index = self.piece_index + 16 + 1
add_pos_move_knight_king(possible_index)
if self.piece_index + 16 - 1 < 64 and self.column > 0: # down left
possible_index = self.piece_index + 16 - 1
add_pos_move_knight_king(possible_index)
if self.piece_index - 16 + 1 > -1 and self.column < 7: # up left
possible_index = self.piece_index - 16 + 1
add_pos_move_knight_king(possible_index)
if self.piece_index - 16 - 1 > -1 and self.column > 0: # up right
possible_index = self.piece_index - 16 - 1
add_pos_move_knight_king(possible_index)
if self.piece_index + 2 + 8 < 64 and self.column < 6: # right down
possible_index = self.piece_index + 2 + 8
add_pos_move_knight_king(possible_index)
if self.piece_index + 2 - 8 > -1 and self.column < 6: # right up
possible_index = self.piece_index + 2 - 8
add_pos_move_knight_king(possible_index)
if self.piece_index - 2 + 8 < 64 and self.column > 1: # left down
possible_index = self.piece_index - 2 + 8
add_pos_move_knight_king(possible_index)
if self.piece_index - 2 - 8 > -1 and self.column > 1: # left up
possible_index = self.piece_index - 2 - 8
add_pos_move_knight_king(possible_index)
elif self.piece_name == "bishop":
if self.column > 0:
for i in range(self.piece_index + 7, 64, 7): # bot left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index - 7, -1, -7): # top right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index + 9, 64, 9): # bot right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column > 0:
for i in range(self.piece_index - 9, -1, -9): # top left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
elif self.piece_name == "king":
if self.column > 0: # left movements
for i in [-9, -1, 7]:
if i < 0 and self.piece_index + i > -1:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif i > 0 and self.piece_index + i < 64:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
if self.column < 7: # right movements
for i in [9, 1, -7]:
if i < 0 and self.piece_index + i > -1:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif i > 0 and self.piece_index + i < 64:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
for i in [-8, 8]: # up and down
if i < 0 and self.piece_index + i > -1:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif i > 0 and self.piece_index + i < 64:
possible_index = self.piece_index + i
add_pos_move_knight_king(possible_index)
elif self.piece_name == "queen":
# up, down, left, and right movements:
add_pos_move_rook(self.piece_index + 1, row * 8 + 8, 1) # left
add_pos_move_rook(self.piece_index - 1, row * 8 - 1, -1) # right
add_pos_move_rook(self.piece_index + 8, 63, 8) # down
add_pos_move_rook(self.piece_index - 8, -1, -8) # up
# diagonals:
if self.column > 0:
for i in range(self.piece_index + 7, 64, 7): # bot left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index - 7, -1, -7): # top right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column < 7:
for i in range(self.piece_index + 9, 64, 9): # bot right
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column < 7:
self.possible_moves.append([new_column, new_row])
elif new_column == 7:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
if self.column > 0:
for i in range(self.piece_index - 9, -1, -9): # top left
new_column = i % 8
new_row = i // 8
if board_list[i] == 0:
self.possible_moves.append([new_column, new_row])
if new_column > 0:
self.possible_moves.append([new_column, new_row])
elif new_column == 0:
self.possible_moves.append([new_column, new_row])
break
elif 0 < board_list[i] < 7:
self.possible_captures.append([new_column, new_row])
break
else:
break
def undo_create_moves(self):
self.possible_moves = []
self.possible_captures = []
def move(self, new_index):
self.piece_index = new_index
self.column = self.piece_index % 8
self.row = self.piece_index // 8
x = 560 + self.column * 100
y = 140 + self.row * 100
self.coordinate = x, y
self.rect.topleft = self.coordinate
self.possible_moves = []
self.possible_captures = []
self.turn += 1
def piece_index_finder(num):
"""Finds the every instance of an index for the chosen number on the board list"""
return [index for index, piece in enumerate(board_list) if piece == num] # the enumerate function produces a list
# same values except the values are now tuples with the index being the first value
# the format here is just a condensed for loop that I really like and will use for easy tasks like this
def piece_creator(p_indices, p_list, p_class, p_img, p_name, p_num):
"""For every index in the list, a piece will be created on that index"""
for index in p_indices:
p_list.append(p_class(p_img, index, p_name, p_num))
async def main():
# Audio config for web
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
pygame.font.init()
# WEB SETUP:
# display_window is the real browser window (scaled down).
# game_surface is the virtual 1920x1080 for the logic
display_window = pygame.display.set_mode(REAL_RES)
game_surface = pygame.Surface(VIRTUAL_RES)
clock = pygame.time.Clock()
#safer loading for web
def safe_load_img(path, scale_to_100=False):
try:
img = pygame.image.load(path).convert_alpha()
if scale_to_100:
img = pygame.transform.scale(img, (100, 100))
return img
except FileNotFoundError:
print(f"ERROR: Image not found {path}")
s = pygame.Surface((100, 100))
s.fill("magenta")
return s
def safe_load_snd(path):
try:
return pygame.mixer.Sound(path)
except FileNotFoundError:
print(f"ERROR: Sound not found {path}")
class DummySound:
def play(self): pass
return DummySound()
board = safe_load_img("assets/sprites/board2.png") # the loading of images
w_select = safe_load_img("assets/sprites/select moves/select_s2w.png")
b_select = safe_load_img("assets/sprites/select moves/select_s2b2.png")
w_reset = safe_load_img("assets/sprites/reset/white_reset.png")
b_reset = safe_load_img("assets/sprites/reset/black_reset.png")
poss_move_w = safe_load_img("assets/sprites/possible moves/poss_w2.png")
poss_move_b = safe_load_img("assets/sprites/possible moves/poss_b2.png")
poss_kill = safe_load_img("assets/sprites/possible kill/poss_kill_11.png")
w_king_img = safe_load_img("assets/sprites/white pieces/white_king.png")
w_queen_img = safe_load_img("assets/sprites/white pieces/white_queen.png")
w_bishop = safe_load_img("assets/sprites/white pieces/white_bishop.png")
w_knight = safe_load_img("assets/sprites/white pieces/white_knight.png")
w_rook = safe_load_img("assets/sprites/white pieces/white_rook.png")
w_pawn = safe_load_img("assets/sprites/white pieces/white_pawn.png")
b_king_img = safe_load_img("assets/sprites/black pieces/black_king.png")
b_queen_img = safe_load_img("assets/sprites/black pieces/black_queen.png")
b_bishop = safe_load_img("assets/sprites/black pieces/black_bishop.png")
b_knight = safe_load_img("assets/sprites/black pieces/black_knight.png")
b_rook = safe_load_img("assets/sprites/black pieces/black_rook.png")
b_pawn = safe_load_img("assets/sprites/black pieces/black_pawn.png")
start_sound = safe_load_snd("assets/sounds/start.wav")
move_sound = safe_load_snd("assets/sounds/move.wav")
capture_sound = safe_load_snd("assets/sounds/capture.wav")
castling_sound = safe_load_snd("assets/sounds/castling.wav")
check_sound = safe_load_snd("assets/sounds/check.wav")
game_over_sound = safe_load_snd("assets/sounds/game over.wav")
# Draw to game_surface instead of screen
game_surface.blit(board, [0, 0])
w_pawn_indices = piece_index_finder(1)
w_rook_indices = piece_index_finder(2)
w_knight_indices = piece_index_finder(3)
w_bishop_indices = piece_index_finder(4)
w_king_indices = piece_index_finder(5)
w_queen_indices = piece_index_finder(6)
b_pawn_indices = piece_index_finder(11)
b_rook_indices = piece_index_finder(12)
b_knight_indices = piece_index_finder(13)
b_bishop_indices = piece_index_finder(14)
b_king_indices = piece_index_finder(15)
b_queen_indices = piece_index_finder(16)
w_pawns = []
w_rooks = []
w_knights = []
w_bishops = []
w_king = []
w_queen = []
b_pawns = []
b_rooks = []
b_knights = []
b_bishops = []
b_king = []
b_queen = []
piece_creator(w_pawn_indices, w_pawns, WhitePiece, w_pawn, "pawn", 1) # create the pieces in a list
piece_creator(w_rook_indices, w_rooks, WhitePiece, w_rook, "rook", 2)
piece_creator(w_knight_indices, w_knights, WhitePiece, w_knight, "knight", 3)
piece_creator(w_bishop_indices, w_bishops, WhitePiece, w_bishop, "bishop", 4)
piece_creator(w_king_indices, w_king, WhitePiece, w_king_img, "king", 5)
piece_creator(w_queen_indices, w_queen, WhitePiece, w_queen_img, "queen", 6)
piece_creator(b_pawn_indices, b_pawns, BlackPiece, b_pawn, "pawn", 11)
piece_creator(b_rook_indices, b_rooks, BlackPiece, b_rook, "rook", 12)
piece_creator(b_knight_indices, b_knights, BlackPiece, b_knight, "knight", 13)
piece_creator(b_bishop_indices, b_bishops, BlackPiece, b_bishop, "bishop", 14)
piece_creator(b_king_indices, b_king, BlackPiece, b_king_img, "king", 15)
piece_creator(b_queen_indices, b_queen, BlackPiece, b_queen_img, "queen", 16)
piece_chosen = False # a boolean that identifies whether a piece has been chosen
chosen_list = [] # this is the list the chosen piece belongs to
chosen_piece_index = 0
chosen_piece_list = [] # a list of the pieces that are chosen in the turn
poss_move_indices = [] # the index of the possible moves that piece can have
poss_capture_indices = [] # the index of the possible captures that a piece can perform
running = True
audio_context_ready = False
even_odd = 0 # a number that will become either even or odd
player_num = 1 # this number will be either 1 or 2, depending on which player's turn it is
# Redraw background once before loop
game_surface.blit(board, [0, 0])
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Handle Audio Context on first click
if not audio_context_ready:
start_sound.play()
audio_context_ready = True
# The browser window is small (1280x720), but logic assumes 1920x1080.
real_x, real_y = event.pos
click_x = real_x * SCALE_X
click_y = real_y * SCALE_Y
event.pos = (click_x, click_y)
if 560 <= click_x <= 1360 and 140 <= click_y <= 940:
# checks if the mouse button has been pressed within the bounds of the chess board
chosen_index = (int(click_x) - 560) // 100 + (int(click_y) - 140) // 100 * 8
# if it has, the chosen index becomes the index of the pressed box within the board list
else: # if the mouse button has not been pressed
chosen_index = -1 # the chosen index will become -1
if piece_chosen:
for poss_move in poss_move_indices:
if chosen_index == poss_move: # if a possible move is clicked by the player
board_list[chosen_list[chosen_piece_index].piece_index] = 0
# this changes the board list block that the piece was on to 0 to make it "empty"
board_list[chosen_index] = chosen_list[chosen_piece_index].piece_num
# this moves the piece to the index that the player chose
chosen_list[chosen_piece_index].move(chosen_index)
# the chosen piece's rect and coordinates are moved
move_sound.play()
game_surface.blit(board, (0, 0)) # draws and essentially empties the board
chosen_index = -1 # the chosen index is reset
even_odd += 1 # even odd is updated
player_num = even_odd % 2 + 1 # the player turn is switched
piece_chosen = False
for poss_capture in poss_capture_indices:
if chosen_index == poss_capture: # if a possible move is clicked by the player
board_list[chosen_list[chosen_piece_index].piece_index] = 0
# this changes the board list block that the piece was on to 0 to make it "empty"
def capture_piece(p_list):
"""checks all the pieces in a piece list to see if it's rect has been clicked
if it is, it's deleted"""
for i in range(len(p_list)):
if p_list[i].rect.collidepoint(event.pos):
del p_list[i]
break
capture_piece(b_pawns)
capture_piece(b_rooks)
capture_piece(b_knights)
capture_piece(b_bishops)
capture_piece(b_king)
capture_piece(b_queen)
capture_piece(w_pawns)
capture_piece(w_rooks)
capture_piece(w_knights)
capture_piece(w_bishops)
capture_piece(w_king)
capture_piece(w_queen)
capture_sound.play()
board_list[chosen_index] = chosen_list[chosen_piece_index].piece_num
# this moves the piece to the index that the player chose
chosen_list[chosen_piece_index].move(chosen_index)
# the chosen piece's rect and coordinates are moved
game_surface.blit(board, (0, 0)) # draws and essentially empties the board
chosen_index = -1 # the chosen index is reset
even_odd += 1 # even odd is updated
player_num = even_odd % 2 + 1 # the player turn is switched
piece_chosen = False
def check_if_chosen(p_list):
"""Checks every piece within a piece list to see if it matches the chosen index and if it is,
it will make it known to the program which piece list it is"""
global piece_chosen, chosen_piece_index, chosen_list, chosen_piece_list
index = 0 # this is the index for each piece in the piece list
for piece in p_list:
if chosen_index == piece.piece_index:
return index
index += 1
return -1
# player 2's turn
piece_lists = [b_pawns, b_rooks, b_knights, b_bishops, b_king, b_queen]
if player_num == 1: # checks if it's player 1's turn
piece_lists = [w_pawns, w_rooks, w_knights, w_bishops, w_king, w_queen]
for piece_list in piece_lists:
val = check_if_chosen(piece_list)
if val != -1:
piece_list[val].create_moves()
chosen_piece_index = val
chosen_list = piece_list
piece_chosen = True
chosen_piece_list.append((chosen_list, chosen_piece_index))
if len(chosen_piece_list) == 2: # if there is more than 1 piece in the chosen piece list
game_surface.blit(board, (0, 0)) # this will reset the board
if chosen_piece_list[0] != chosen_piece_list[1]: # if the chosen pieces are not the same
chosen_piece_list[0][0][chosen_piece_list[0][1]].possible_moves = []
# this takes whatever piece the was chosen before the latest and resets the possible moves for it
chosen_piece_list.pop(0) # removes the piece chosen before the latest
poss_move_indices = [] # resets the possible moves indices
poss_capture_indices = [] # resets the possible captures indices
# Drawing Loop
if piece_chosen:
piece_coordinate = chosen_list[chosen_piece_index].rect.topleft
# assigns the coordinate of the top left of the rect of the chosen piece
if chosen_list[chosen_piece_index].row % 2 == 0: # checks if the row if the chosen piece is even
if chosen_list[
chosen_piece_index].piece_index % 2 == 0: # checks if the actual index of the piece is even
game_surface.blit(w_select,
piece_coordinate) # if so, the block is white, so the white select will blit
else:
game_surface.blit(b_select, piece_coordinate) # if not, it is a black block
else: # if the row of the chosen piece is odd
if chosen_list[
chosen_piece_index].piece_index % 2 == 0: # checks if the actual index of the piece is even
game_surface.blit(b_select,
piece_coordinate) # if so, the block is white, so the white select will blit
else:
game_surface.blit(w_select, piece_coordinate) # if not, it is a black block
for x, y in chosen_list[chosen_piece_index].possible_moves:
new_x = 560 + x * 100 # the pixel version of the x coordinate
new_y = 140 + y * 100
index = y * 8 + x # turns the x and y coordinates into a index on the board list
poss_move_indices.append(index) # adds the move to the possible move indices
if y % 2 == 0: # checks if the y is even
if x % 2 == 0: # checks if x is even
game_surface.blit(poss_move_w, (new_x,
new_y)) # if so, the block is white, so the white poss_move_img will blit
else:
game_surface.blit(poss_move_b, (new_x, new_y)) # if not, it is a black block
else:
if x % 2 == 0: # same as above
game_surface.blit(poss_move_b, (new_x, new_y))
else:
game_surface.blit(poss_move_w, (new_x, new_y))
# below is the same as the above except it is for the possible captures
for x, y in chosen_list[chosen_piece_index].possible_captures:
new_x = 560 + x * 100
new_y = 140 + y * 100
game_surface.blit(poss_kill, (new_x, new_y))
index = y * 8 + x
poss_capture_indices.append(index)
def draw_pieces(p_list):
"""draws all the pieces in a piece list"""
for piece in p_list:
game_surface.blit(piece.image, piece.rect)
draw_pieces(w_pawns) # all the pieces are drawn
draw_pieces(w_rooks)
draw_pieces(w_knights)
draw_pieces(w_bishops)
draw_pieces(w_king)
draw_pieces(w_queen)
draw_pieces(b_pawns)
draw_pieces(b_rooks)
draw_pieces(b_knights)
draw_pieces(b_bishops)
draw_pieces(b_king)
draw_pieces(b_queen)
# Scale down to real screen size
frame = pygame.transform.scale(game_surface, REAL_RES)
display_window.blit(frame, (0, 0))
pygame.display.flip() # actually makes everything appear
clock.tick(60)
await asyncio.sleep(0)