-
Notifications
You must be signed in to change notification settings - Fork 29
/
gui.py
245 lines (192 loc) · 5.85 KB
/
gui.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
import sys
import pygame
from random import random
from pygame.locals import QUIT
from common.board import Board
from common.constants.pygame import (
SIZE,
COLOR_GRAY,
COLOR_WHITE,
COLOR_BLUE,
IMAGE_DIR
)
from common.constants import BLACK, WHITE, OPPOSITE
from common.helpers import get_chess_square, get_chess_square_reverse, get_chess_square_border
pygame.init()
screen = pygame.display.set_mode((SIZE, SIZE))
def draw_chessboard(board):
"""
Draws the board with the current `board`'s piece positions
"""
screen.fill(COLOR_WHITE)
start_x, start_y = 0, 0
for idx in range(8):
if board.user_color == WHITE:
start_x = 0 if idx % 2 != 0 else SIZE // 8
else:
start_x = SIZE // 8 if idx % 2 != 0 else 0
for __ in range(8):
pygame.draw.rect(screen, COLOR_GRAY, ((start_x, start_y), (SIZE // 8, SIZE // 8)))
start_x += 2 * SIZE // 8
start_y += SIZE // 8
for piece in board.pieces:
y, x = piece.position
img = pygame.image.load(f'{IMAGE_DIR}{piece.color}_{piece.type}.png')
screen.blit(img, (x * SIZE // 8 + SIZE // 80, y * SIZE // 8 + SIZE // 80))
pygame.display.update()
def draw_move_borders(board, moves):
"""
Draws border on square blocks for every move in `moves`
"""
draw_chessboard(board)
for move in moves:
pygame.draw.rect(
screen,
COLOR_BLUE,
(get_chess_square_reverse(SIZE, move[1], move[0]), (SIZE // 8, SIZE // 8))
)
if board.user_color != WHITE:
color = COLOR_WHITE if (move[1] + move[0]) % 2 != 0 else COLOR_GRAY
else:
color = COLOR_GRAY if (move[1] + move[0]) % 2 != 0 else COLOR_WHITE
pygame.draw.rect(
screen,
color,
(get_chess_square_border(SIZE, move[1], move[0]), (SIZE // 8 - 4, SIZE // 8 - 4))
)
for piece in board.pieces:
y, x = piece.position
if piece.position == move:
img = pygame.image.load(f'{IMAGE_DIR}{piece.color}_{piece.type}.png')
screen.blit(img, (x * SIZE // 8 + SIZE // 80, y * SIZE // 8 + SIZE // 80))
pygame.display.update()
def draw_borders_if_clicked_on_piece(board, x, y):
"""
Draws borders for moves if clicked on a piece, returning piece clicked on, its moves and its old
position
"""
_x, _y = get_chess_square(SIZE, x, y)
piece_clicked = None
moves = []
for piece in board.pieces:
if piece.position == [_y, _x]:
moves = [move['new_position'] for move in piece.get_moves(board)]
piece_clicked = piece.type
piece_moves = moves
if moves:
draw_move_borders(board, moves)
return (_x, _y), piece_clicked, moves
def looping_cpu_vs_human():
"""
Player v/s CPU mode
"""
old_x = 0
old_y = 0
new_x = 0
new_y = 0
color = WHITE if round(random() * 100) <= 50 else BLACK
board = Board(color)
if color != WHITE:
board.flip()
draw_chessboard(board)
if color != WHITE:
board.play_move()
draw_chessboard(board)
piece_clicked = None
piece_moves = []
game_over = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if not game_over:
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
old_pos, piece_clicked, piece_moves = draw_borders_if_clicked_on_piece(board, x, y)
old_x, old_y = old_pos
if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
new_x, new_y = get_chess_square(SIZE, x, y)
if (new_x != old_x or new_y != old_y) and [new_y, new_x] in piece_moves:
move = {
"old_position": [old_y, old_x],
"new_position": [new_y, new_x],
"piece": piece_clicked,
"color": board.side_to_move
}
is_over = board.play_move(move=move)
draw_chessboard(board)
if is_over:
# TODO: display message
print(is_over)
game_over = True
break
else:
is_over = board.play_move()
draw_chessboard(board)
if is_over:
# TODO: display message
print(is_over)
game_over = True
break
def looping_cpu_vs_cpu():
"""
CPU v/s CPU mode
"""
board = Board()
draw_chessboard(board)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
is_over = board.play_move()
draw_chessboard(board)
if is_over:
# TODO: display message
break
def looping_human_vs_human():
"""
Human v/s Human mode
"""
old_x = 0
old_y = 0
new_x = 0
new_y = 0
board = Board()
draw_chessboard(board)
piece_clicked = None
piece_moves = []
game_over = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if not game_over:
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
piece_moves = draw_borders_if_clicked_on_piece(board, x, y)
if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
new_x, new_y = get_chess_square(SIZE, x, y)
if (new_x != old_x or new_y != old_y) and [new_y, new_x] in piece_moves:
move = {
"old_position": [old_y, old_x],
"new_position": [new_y, new_x],
"piece": piece_clicked,
"color": board.side_to_move
}
is_over = board.play_move(move=move)
if is_over:
# TODO: display message
print(is_over)
game_over = True
break
board.user_color = OPPOSITE[board.user_color]
board.flip()
draw_chessboard(board)