-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMoveableUnitsandSprites.py
More file actions
559 lines (461 loc) · 17.5 KB
/
MoveableUnitsandSprites.py
File metadata and controls
559 lines (461 loc) · 17.5 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
import pygame
import os
# Loading up all of the sprite images to use for display
chessboard = pygame.transform.scale(pygame.image.load((os.path.join("Sprites","Chess_board.jpg"))), (626,626))
chesspylogo = pygame.transform.scale(pygame.image.load(os.path.join("Sprites","ChesspyLogo1.png")), (160,120))
madeby = pygame.transform.scale(pygame.image.load(os.path.join("Sprites","madeby.png")), (260, 38))
woodboard = pygame.transform.scale(pygame.image.load((os.path.join("Sprites","woodboard.jpg"))), (626,626))
black_bishop = pygame.image.load(os.path.join("Sprites", "blackBishop.png"))
black_king = pygame.image.load(os.path.join("Sprites", "blackKing.png"))
black_knight = pygame.image.load(os.path.join("Sprites", "blackKnight.png"))
black_pawn = pygame.image.load(os.path.join("Sprites", "blackPawn.png"))
black_rook = pygame.image.load(os.path.join("Sprites", "blackRook.png"))
black_queen = pygame.image.load(os.path.join("Sprites", "blackQueen.png"))
white_bishop = pygame.image.load(os.path.join("Sprites", "whiteBishop.png"))
white_king = pygame.image.load(os.path.join("Sprites", "whiteKing.png"))
white_knight = pygame.image.load(os.path.join("Sprites", "whiteKnight.png"))
white_pawn = pygame.image.load(os.path.join("Sprites", "whitePawn.png"))
white_rook = pygame.image.load(os.path.join("Sprites", "whiteRook.png"))
white_queen = pygame.image.load(os.path.join("Sprites", "whiteQueen.png"))
black = [black_bishop, black_king, black_knight, black_pawn, black_queen, black_rook]
white = [white_bishop, white_king, white_knight, white_pawn, white_queen, white_rook]
# for scaling the pieces and the board
BlackPieces = []
for piece in black:
BlackPieces.append(pygame.transform.scale(piece, (64,64)))
WhitePieces = []
for piece in white:
WhitePieces.append(pygame.transform.scale(piece, (64,64)))
class ChessPieces:
# index used to reference each piece later on
index = -1
game_field = (60, 60, 505, 505)
start_x_pos = game_field[0]
start_y_pos = game_field[1]
def __init__(self, row, column, color):
self.row = row
self.column = column
self.color = color
self.all_possible_movements_list = []
# self.pawn is true when a pawn is chosen subsequently the same for self.king if a king is chosen
self.pawn = False
# self.selected_piece is true when a piece is selected other than that all others that are not should be false
self.selected_piece = False
self.king = False
def get_color(self):
return self.color
def get_selected(self):
return self.selected_piece
def possible_moves(self, board):
self.all_possible_movements_list = self.move_valid(board)
def move_pos(self, pos):
self.row = pos[0]
self.column = pos[1]
def border(self, win, board):
if self.color == "White":
img = WhitePieces[self.index]
else:
img = BlackPieces[self.index]
if self.selected_piece:
moves = self.move_valid(board)
# Positions of red dots to display moves
for movement in moves:
x_pos = 33 + self.start_x_pos + (movement[0] * self.game_field[2] / 8)
y_pos = 33 + self.start_x_pos + (movement[1] * self.game_field[3] / 8)
pygame.draw.circle(win, (255,0,0),(x_pos, y_pos), 10)
# to find coordinates of 1st column and row in chessboard
x_pos = self.start_x_pos + (self.column * self.game_field[2] / 8)
y_pos = self.start_x_pos + (self.row * self.game_field[3] / 8)
# Blue border to outline which piece is selected
if self.selected_piece:
pygame.draw.rect(win, (0,0,255), (x_pos, y_pos,65,65), 2)
win.blit(img, (x_pos, y_pos))
class Bishop(ChessPieces):
index = 0
# move_valid and all other move valids later on are all logical operations to tell the movement set of a piece, which is then put into a list
def move_valid(self, board):
rows = self.row
columns = self.column
move_list = []
# Top Right Move
dial = columns + 1
diar = columns - 1
for x in range(rows - 1, -1, -1):
if dial < 8:
j = board[x][dial]
if j == 0:
move_list.append((dial, x))
elif j.color != self.color:
move_list.append((dial, x))
break
else:
dial = 9
dial += 1
for x in range(rows - 1, -1, -1):
if diar > -1:
j = board[x][diar]
if j == 0:
move_list.append((diar, x))
elif j.color != self.color:
move_list.append((diar, x))
break
else:
diar = -1
diar -= 1
# Top Left Movement
dial = columns + 1
diar = columns - 1
for x in range(rows + 1, 8):
if dial < 8:
j = board[x][dial]
if j == 0:
move_list.append((dial, x))
elif j.color != self.color:
move_list.append((dial, x))
break
else:
dial = 9
dial += 1
for x in range(rows + 1, 8):
if diar > -1:
j = board[x][diar]
if j == 0:
move_list.append((diar, x))
elif j.color != self.color:
move_list.append((diar, x))
break
else:
diar = -1
diar -= 1
return move_list
# instill methods into pawn class as pawns have extra features such as being able to turn to a queen and first moves
class Pawn(ChessPieces):
index = 3
def __init__(self, rows, columns, color):
super().__init__(rows, columns, color)
self.first_movement = True
self.pawn = True
self.turn_to_queen = False
def move_valid(self, board):
currrow = self.row
currcol = self.column
move_list = []
try:
if self.color == "Black":
if self.first_movement:
if currrow < 6:
j = board[currrow + 2][currcol]
if j == 0:
move_list.append((currcol, currrow + 2))
if currrow < 7:
j = board[currrow + 1][currcol]
if j == 0:
move_list.append((currcol, currrow + 1))
# Diagonal attacks
# Right attack
if currcol < 7:
j = board[currrow + 1][currcol + 1]
if j != 0:
if j.color != self.color:
move_list.append((currcol + 1, currrow + 1))
# Left attack
if currcol > 0:
j = board[currrow + 1][currcol - 1]
if j != 0:
if j.color != self.color:
move_list.append((currcol - 1, currrow + 1))
else:
if self.first_movement:
if currrow > 0:
j = board[currrow - 2][currcol]
if j == 0:
move_list.append((currcol, currrow - 2))
if currrow > 0:
j = board[currrow - 1][currcol]
if j == 0:
move_list.append((currcol, currrow - 1))
# Diagonal attacks
# Right attack
if currcol < 7:
j = board[currrow - 1][currcol + 1]
if j != 0:
if j.color != self.color:
move_list.append((currcol + 1, currrow - 1))
# Left attack
if currcol > 0:
j = board[currrow - 1][currcol - 1]
if j != 0:
if j.color != self.color:
move_list.append((currcol - 1, currrow - 1))
except IndexError:
pass
return move_list
class King(ChessPieces):
index = 1
def __init__(self, rows, columns, color):
super().__init__(rows, columns, color)
self.king = True
def move_valid(self, board):
currrow = self.row
currcol = self.column
move_list = []
if currrow > 0:
# Top Left Movement
if currcol > 0:
j = board[currrow - 1][currcol - 1]
if j == 0:
move_list.append((currcol - 1, currrow - 1))
elif j.color != self.color:
move_list.append((currcol - 1, currrow - 1))
# Top Middle Movement
j = board[currrow - 1][currcol]
if j == 0:
move_list.append((currcol, currrow - 1))
elif j.color != self.color:
move_list.append((currcol, currrow - 1))
# Top Right
if currcol < 7:
j = board[currrow - 1][currcol + 1]
if j == 0:
move_list.append((currcol + 1, currrow - 1))
elif j.color != self.color:
move_list.append((currcol + 1, currrow - 1))
if currrow < 7:
# Bottom Left
if currcol > 0:
j = board[currrow + 1][currcol - 1]
if j == 0:
move_list.append((currcol - 1, currrow + 1))
elif j.color != self.color:
move_list.append((currcol - 1, currrow + 1))
# Bottom Middle
j = board[currrow + 1][currcol]
if j == 0:
move_list.append((currcol, currrow + 1))
elif j.color != self.color:
move_list.append((currcol, currrow + 1))
# Bottom Right
if currcol < 7:
j = board[currrow + 1][currcol + 1]
if j == 0:
move_list.append((currcol + 1, currrow + 1))
elif j.color != self.color:
move_list.append((currcol + 1, currrow + 1))
# Middle Left
if currcol > 0:
j = board[currrow][currcol - 1]
if j == 0:
move_list.append((currcol - 1, currrow))
elif j.color != self.color:
move_list.append((currcol - 1, currrow))
# Middle Right
if currcol < 7:
j = board[currrow][currcol + 1]
if j == 0:
move_list.append((currcol + 1, currrow))
elif j.color != self.color:
move_list.append((currcol + 1, currrow))
return move_list
class Queen(ChessPieces):
index = 4
def move_valid(self, board):
currrow = self.row
currcol = self.column
move = []
# Top Right Move
dial = currcol + 1
diar = currcol - 1
for i in range(currrow - 1, -1, -1):
if dial < 8:
j = board[i][dial]
if j == 0:
move.append((dial, i))
elif j.color != self.color:
move.append((dial, i))
break
else:
dial = 9
dial += 1
for i in range(currrow - 1, -1, -1):
if diar > -1:
j = board[i][diar]
if j == 0:
move.append((diar, i))
elif j.color != self.color:
move.append((diar, i))
break
else:
diar = -1
diar -= 1
# Top Left Move
dial = currcol + 1
diar = currcol - 1
for i in range(currrow + 1, 8):
if dial < 8:
j = board[i][dial]
if j == 0:
move.append((dial, i))
elif j.color != self.color:
move.append((dial, i))
break
else:
dial = 9
dial += 1
for i in range(currrow + 1, 8):
if diar > -1:
j = board[i][diar]
if j == 0:
move.append((diar, i))
elif j.color != self.color:
move.append((diar, i))
break
else:
diar = -1
diar -= 1
# Up Movement
for x in range(currrow - 1, -1, -1):
j = board[x][currcol]
if j == 0:
move.append((currcol, x))
elif j.color != self.color:
move.append((currcol, x))
break
else:
break
# Down Movement
for x in range(currrow + 1, 8, 1):
j = board[x][currcol]
if j == 0:
move.append((currcol, x))
elif j.color != self.color:
move.append((currcol, x))
break
else:
break
# Left Movement
for x in range(currcol - 1, -1, -1):
j = board[currrow][x]
if j == 0:
move.append((x, currrow))
elif j.color != self.color:
move.append((x, currrow))
break
else:
break
# Right Movement
for x in range(currcol + 1, 8, 1):
j = board[currrow][x]
if j == 0:
move.append((x, currrow))
elif j.color != self.color:
move.append((x, currrow))
break
else:
break
return move
class Rook(ChessPieces):
index = 5
def move_valid(self, board):
currrow = self.row
currcol = self.column
move_list = []
# Up
for x in range(currrow - 1, -1, -1):
j = board[x][currcol]
if j == 0:
move_list.append((currcol,x))
elif j.color != self.color:
move_list.append((currcol,x))
break
else:
break
# Down
for x in range(currrow + 1, 8, 1):
j = board[x][currcol]
if j == 0:
move_list.append((currcol,x))
elif j.color != self.color:
move_list.append((currcol,x))
break
else:
break
# Left
for x in range(currcol - 1, -1, -1):
j = board[currrow][x]
if j == 0:
move_list.append((x,currrow))
elif j.color != self.color:
move_list.append((x,currrow))
break
else:
break
# Right
for x in range(currcol + 1, 8, 1):
j = board[currrow][x]
if j == 0:
move_list.append((x,currrow))
elif j.color != self.color:
move_list.append((x,currrow))
break
else:
break
return move_list
class Knight(ChessPieces):
index = 2
def move_valid(self, board):
currrow = self.row
currcol = self.column
move_list = []
# Down Left
if currrow < 6 and currcol > 0:
j = board[currrow + 2][currcol - 1]
if j == 0:
move_list.append((currcol - 1, currrow + 2))
elif j.color != self.color:
move_list.append((currcol - 1, currrow + 2))
# Up left
if currrow > 1 and currcol > 0:
j = board[currrow - 2][currcol - 1]
if j == 0:
move_list.append((currcol - 1, currrow - 2))
elif j.color != self.color:
move_list.append((currcol - 1, currrow - 2))
# Down right
if currrow < 6 and currcol < 7:
j = board[currrow + 2][currcol + 1]
if j == 0:
move_list.append((currcol + 1, currrow + 2))
elif j.color != self.color:
move_list.append((currcol + 1, currrow + 2))
# Up right
if currrow > 1 and currcol < 7:
j = board[currrow - 2][currcol + 1]
if j == 0:
move_list.append((currcol + 1, currrow - 2))
elif j.color != self.color:
move_list.append((currcol + 1, currrow - 2))
if currrow > 0 and currcol > 1:
j = board[currrow - 1][currcol - 2]
if j == 0:
move_list.append((currcol - 2, currrow - 1))
elif j.color != self.color:
move_list.append((currcol - 2, currrow - 1))
if currrow > 0 and currcol < 6:
j = board[currrow - 1][currcol + 2]
if j == 0:
move_list.append((currcol + 2, currrow - 1))
elif j.color != self.color:
move_list.append((currcol + 2, currrow - 1))
if currrow < 7 and currcol > 1:
j = board[currrow + 1][currcol - 2]
if j == 0:
move_list.append((currcol - 2, currrow + 1))
elif j.color != self.color:
move_list.append((currcol - 2, currrow + 1))
if currrow < 7 and currcol < 6:
j = board[currrow + 1][currcol + 2]
if j == 0:
move_list.append((currcol + 2, currrow + 1))
elif j.color != self.color:
move_list.append((currcol + 2, currrow + 1))
return move_list