-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.py
323 lines (281 loc) · 11.3 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
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
#!/usr/bin/env python3
"""
Chess game using PyGame module. Main game program for chess game. Intended for 2
person multiplayer. Does not include AI for singleplayer. For details, refer to
the README.md file.
Gameplay:
- Mouseclick based GUI. Click respective pieces to move.
- Undo button: z
- Restart button: r
Last updated: 10.Jun.2020, Python 3.8.3
By Joseph Libasora
"""
import back_chess as ch
import pygame
import time
def LoadImg():
imgs = {}
pieces = [
"b_R", "b_N", "b_B", "b_Q", "b_K", "b_P",
"w_R", "w_N", "w_B", "w_Q", "w_K", "w_P"
]
for x in pieces:
imgs[x] = pygame.image.load(f"assets/{x}.png")
return imgs
def drawGame(screen, imgs, game, validMV, clickSel):
pygame.draw.line(screen, pygame.Color("black"), (0, 600), (600, 600), 4)
drawBoard(screen)
selHighlight(screen, game, validMV, clickSel)
drawPieces(screen, imgs, game.board)
def drawBoard(screen):
cols = [pygame.Color("white"), pygame.Color("grey")]
for r in range(8):
for c in range(8):
col = cols[(r + c) % 2]
pygame.draw.rect(screen, col, pygame.Rect(c * 75, r * 75, 75, 75))
def drawPieces(screen, imgs, board):
for r in range(8):
for c in range(8):
if board[r][c] != "-":
screen.blit(imgs[board[r][c]], (6 + (75 * c), 6 + (75 * r)))
def selHighlight(screen, game, validMV, clickSel):
if clickSel != ():
r, c = clickSel
if game.board[r][c][0] == ("w" if game.white else "b"):
surf = pygame.Surface((75, 75))
surf.set_alpha(100)
surf.fill((77, 255, 77))
screen.blit(surf, (c * 75, r * 75))# highlight sq sel.
for move in validMV:
if move.start[0] == r and move.start[1] == c:
if game.board[move.end[0]][move.end[1]][0] != "-" and game.board[move.end[0]][move.end[1]][0] != ("w" if game.white else "b"):
surf.fill((255, 51, 51))
screen.blit(surf, (75 * move.end[1], 75 * move.end[0]))# highlight avail. moves (n. attack)
elif move.p_captured[-1] == "P":
surf.fill((255, 51, 51))
screen.blit(surf, (75 * move.end[1], 75 * move.end[0]))# highlight avail. moves (enP. attack)
else:
surf.fill((255, 255, 153))
screen.blit(surf, (75 * move.end[1], 75 * move.end[0]))# highlight avail. moves (normal)
def showTime(screen, font, colour, timein, act, x, y):
col = [(38, 38, 38), (26, 255, 26)]
hdr = font.render(f"{colour} time:", True, col[0])
time = font.render(timein, True, col[act])
screen.blit(hdr, (x, y))
screen.blit(time, (x + 15, y + 25))
def showSel(screen, font, clickSel):
text = font.render(f"Selected {clickSel}", True, (38, 38, 38))
screen.blit(text, (245, 615))
def showStatus(screen, font, status):
dct = {
"Invalid move": 245, "": 0, "Max undo": 245, "Disabled rule check": 220,
"Current turn: White": 205, "Current turn: Black": 205,
"Checkmate: white wins": 190, "Checkmate: black wins": 190,
"Draw, stalemate": 218, "Check": 265
}
cols = [(38, 38, 38), (255, 51, 51)]
col = (cols[1] if status == "Check" else cols[0])
try:
x = dct[status]
except KeyError:
x = 250
text = font.render(status, True, col)
screen.blit(text, (x, 640))
def showPromo(screen, imgs, game):
# Show pawn promotion menu
if game.white:
screen.blit(imgs["w_Q"], (156, 681))
screen.blit(imgs["w_R"], (231, 681))
screen.blit(imgs["w_N"], (306, 681))
screen.blit(imgs["w_B"], (381, 681))
else:
screen.blit(imgs["b_Q"], (156, 681))
screen.blit(imgs["b_R"], (231, 681))
screen.blit(imgs["b_N"], (306, 681))
screen.blit(imgs["b_B"], (381, 681))
def showEndgame(screen, font, game, t_time):
text = font.render(f"Total time: {t_time}", True, (38, 38, 38))
screen.blit(text, (205, 681))# Display total game time
text = font.render("Rematch", True, (38, 38, 38))
pygame.draw.rect(screen, (102, 255, 102), pygame.Rect(0, 681, 150, 75))
screen.blit(text, (25, 701))# Display rematch button
text = font.render("Exit", True, (38, 38, 38))
pygame.draw.rect(screen, (255, 51, 51), pygame.Rect(450, 681, 150, 75))
screen.blit(text, (505, 701))# Display exit button
def main():
print(" --------- Chess game running -------- \n")
print("Controls:\n- Mouse click, move pieces\n- Undo button: z")
print("- Reset button: r")
print("\n By Joseph Libasora")
print("Last updated: 10.Jun.2020, Python 3.8.1")
print(" ------------------------------------- \n")
# Game init & game var. init
pygame.init()
game = ch.Game()
imgs = LoadImg()
validMV = game.getValid()
mvMade = promo = endgame = False
clickSel, clickLog, status = (), [], ""
# Game screen init
screen = pygame.display.set_mode((600, 750))
pygame.display.set_caption("Chess")
icon = pygame.image.load("assets/chess_icon.png")
pygame.display.set_icon(icon)
font = pygame.font.Font("freesansbold.ttf", 22)
# Game time init
w_time, b_time = ch.Time(), ch.Time()
w_act, b_act = 1, 0
prev_t = 0
# Game starter drawing funct. calls
drawGame(screen, imgs, game, validMV, clickSel)
print(" ------------ Match start ------------ ")
running = True
while running:
pygame.time.delay(100)
# Game timer
if not endgame:
if game.white:
w_act, b_act = 1, 0
ticks = pygame.time.get_ticks() // 1000
if prev_t != ticks:
w_time.add(1)
prev_t = ticks
else:
w_act, b_act = 0, 1
ticks = pygame.time.get_ticks() // 1000
if prev_t != ticks:
b_time.add(1)
prev_t = ticks
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if not endgame:
if not promo:
# Normal game mouse clicks
pos = game.getIndex(pos)
if pos == clickSel or pos is None:
clickSel, clickLog = (), []
else:
clickSel = pos
clickLog.append(pos)
else:
# Promotion menu mouse-clicks
pos = game.choiceIndex(pos)
# Separate branch to deal w/ promotion
if not promo:
if len(clickLog) == 2:
mv = ch.Move(clickLog, game.board)
if game.turnCheck(clickLog):
i, mvRun = 0, True
while i < len(validMV) and mvRun:
if mv == validMV[i]:
print(mv)
mvRun = False
if mv.promo:
clickSel, clickLog, promo, status = (), [], True, "Promotion"
else:
game.mkMove(validMV[i])
clickSel, clickLog, status, mvMade = (), [], "", True
i += 1
if not mvMade and not promo:
if game.clickCheck(clickLog):
clickLog = [clickSel]
else:
clickSel, clickLog = (), []
status = "Invalid move"
else:
clickSel, clickLog = (), []
if game.white:
status = "Current turn: White"
else:
status = "Current turn: Black"
else:
i, mvRun = 0, True
while i < len(validMV) and mvRun:
if mv == validMV[i]:
validMV[i].pChoice = game.gtChoice(pos)
game.mkMove(validMV[i])
promo, status, mvMade, mvRun = False, "", True, False
i += 1
else:
# Rematch choice mouse-cliick
choice = game.endIndex(pos)
if choice:
# Game reset
t_time = w_time + b_time
print(" ------------ Match reset ------------ ")
print(f"Total game time: {t_time}\n")
print(" ------------ Match start ------------ ")
game = ch.Game()
validMV = game.getValid()
mvMade = promo = endgame = False
clickSel, clickLog, status = (), [], ""
w_time, b_time = ch.Time(), ch.Time()
w_act, b_act = 1, 0
prev_t = 0
elif choice is False:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
# Undo key, z
status = game.undo()
print(status)
mvMade = True
if promo:
promo = False
if endgame:
endgame = False
if event.key == pygame.K_r:
# Reset button: r
t_time = w_time + b_time
print(" ------------ Match reset ------------ ")
print(f"Total game time: {t_time}\n")
print(" ------------ Match start ------------ ")
game = ch.Game()
validMV = game.getValid()
mvMade = promo = endgame = False
clickSel, clickLog, status = (), [], ""
w_time, b_time = ch.Time(), ch.Time()
w_act, b_act = 1, 0
prev_t = 0
# Move made, refresh new valid moves
if mvMade:
validMV = game.getValid()
if game.inCheck():
status = "Check"
mvMade = False
# Checkmate/Stalement
if game.cm:
endgame = True
col = ("black" if game.white else "white")
status = f"Checkmate: {col} wins"
elif game.stale:
endgame = True
status = "Draw, stalemate"
# Game time fct. calls
w_time_str, b_time_str = w_time.getTime(), b_time.getTime()
showTime(screen, font, "White", w_time_str, w_act, 55, 615)
showTime(screen, font, "Black", b_time_str, b_act, 440, 615)
# Current selected fct. call
showSel(screen, font, clickSel)
# Game status fct. call
showStatus(screen, font, status)
# Promotion msg. fct. call
if promo:
showPromo(screen, imgs, game)
# Endgame screen
if endgame:
t_time = w_time + b_time
showEndgame(screen, font, game, t_time)
# Main game drawing fct. call
drawGame(screen, imgs, game, validMV, clickSel)
pygame.display.update()
screen.fill((179, 179, 179))
pygame.quit()
t_time = w_time + b_time
print(" ------------- Match end ------------- ")
print(f"Total game time: {t_time}\n")
if __name__ == "__main__":
main()