-
Notifications
You must be signed in to change notification settings - Fork 2
/
Chess.py
139 lines (113 loc) · 4.57 KB
/
Chess.py
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
# coding=utf-8
from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP
from ChessPieces import *
# noinspection PyUnusedLocal
def game():
# The Game loop
SHOW_END_GAME = 1
Mousedown = False
Mousereleased = False
TargetPiece = None
checkmate = False
check_message = False
check = False
teams = ['White', 'Black']
colors = [dark_brown, light_brown]
drawboard(colors)
while True:
turn = teams[0]
checkquitgame()
pieceholder = None
# -------- CHECK FOR CHECKMATE AND CHECK CONDITIONS --------
for piece in Pieces:
if type(piece) == King and piece.team == turn:
check = piece.undercheck()
if not check:
check_message = False
checkmate = piece.checkforcheckmate()
if checkmate:
colors = [gray, violet]
drawboard(colors)
if SHOW_END_GAME:
show_checkmate(teams)
SHOW_END_GAME = 0
elif check and not check_message:
show_check(teams)
check_message = True
continue
# ----- END CHECKING ----
drawboard(colors)
# get cursor
Cursor = pygame.mouse.get_pos()
# ---- BLOCKING CALL ----
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
Mousedown = True
if event.type == MOUSEBUTTONUP:
Mousedown = False
Mousereleased = True
# ---- Events on MouseDown -----
# pickup the nearest piece if a piece is not selected
if Mousedown and not TargetPiece:
TargetPiece = nearest_piece(Cursor, Pieces)
if TargetPiece:
OriginalPlace = TargetPiece.square
if Mousedown and TargetPiece:
TargetPiece.drag(Cursor)
# ---- MouseReleased Events -----
if Mousereleased:
Mousereleased = False
# ----- validate turn -----
if TargetPiece and TargetPiece.team != turn: # check your turn
TargetPiece.update(OriginalPlace)
TargetPiece = None
# ---- set closest piece that taken be taken ----
elif TargetPiece:
pos1 = TargetPiece.rect.center
for Square in squareCenters:
if distance_formula(pos1, Square.center) < BoardWidth / 16: # half width of square
newspot = Square
otherpiece = nearest_piece(Square.center, Pieces)
break
if otherpiece and otherpiece != TargetPiece and otherpiece.team == TargetPiece.team:
# check if space is occupied by team
TargetPiece.update(OriginalPlace)
elif newspot not in TargetPiece.movelist():
# check if you can move there
TargetPiece.update(OriginalPlace)
elif otherpiece and otherpiece != TargetPiece and type(otherpiece) != King:
# take enemy piece
for piece in Pieces:
if piece == otherpiece:
pieceholder = piece
Pieces.remove(piece)
TargetPiece.update(newspot)
teams = teams[::-1] # switch teams
else:
# move
TargetPiece.update(newspot)
if type(TargetPiece) == Pawn or type(TargetPiece) == BlackPawn or type(TargetPiece) == King:
TargetPiece.bool += 1
teams = teams[::-1] # switch teams
if True: # always check every turn at end
check = False
for piece in Pieces:
if type(piece) == King and piece.team == turn:
check = piece.undercheck()
if check:
# if still under check revert back
TargetPiece.update(OriginalPlace)
if pieceholder and pieceholder.team != TargetPiece.team:
Pieces.append(pieceholder)
# noinspection PyUnusedLocal
pieceholder = None
teams = teams[::-1] # switch back
TargetPiece = None
for piece in Pieces:
piece.draw(screen)
pygame.display.flip()
pygame.display.set_caption('Shatranj')
FPS = pygame.time.Clock()
FPS.tick(10)
if __name__ == '__main__':
game()