-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
204 lines (179 loc) · 7.6 KB
/
main.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
import pygame
import Engine
import warnings
pygame.init()
Width = Height = 512
Dimension = 8
sq_size = Height // Dimension
max_FPS = 15
Images = {}
def load_images():
pieces = ["wK", "bK", "wQ", "bQ", "wR", "bR", "wB", "bB", "wN", "bN", "wP", "bP"]
for piece in pieces:
Images[piece] = pygame.transform.scale(pygame.image.load("Images/" + piece + ".png"), (sq_size, sq_size))
def main():
screen = pygame.display.set_mode((Width, Height))
screen.fill(pygame.Color("black"))
clock = pygame.time.Clock()
gs = Engine.GameState()
valid_moves = gs.get_valid_moves()
move_made = False
animate = False
print('')
print('Welcome to Dama-Chess!')
print('')
print('Please do not close or minimize this terminal as you will need to use this later.')
print('')
print('Press "Z" to undo a move')
print('Press "R" to reset the game')
print('Press "Esc" to quit the game')
print('')
load_images()
sq_selected = ()
player_clicks = []
game_over = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if not game_over:
location = pygame.mouse.get_pos()
col = location[0] // sq_size
row = location[1] // sq_size
if sq_selected == (row, col):
sq_selected = ()
player_clicks = []
else:
sq_selected = (row, col)
player_clicks.append(sq_selected)
if len(player_clicks) == 1:
if sq_selected in gs.invalid_moves():
sq_selected = ()
player_clicks = []
# print(player_clicks)
if len(player_clicks) == 2:
move = Engine.Move(player_clicks[0], player_clicks[1], gs.board)
for i in range(len(valid_moves)):
if move == valid_moves[i]:
gs.make_move(move)
move_made = True
animate = True
print(move.get_chess_notation())
sq_selected = ()
player_clicks = []
if not move_made:
player_clicks = [sq_selected]
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
gs.undo_move()
move_made = True
animate = False
game_over = False
if event.key == pygame.K_r:
game_over = False
gs = Engine.GameState()
valid_moves = gs.get_valid_moves()
sq_selected = ()
player_clicks = []
move_made = False
animate = False
if event.key == pygame.K_ESCAPE:
running = False
if move_made:
if animate:
animate_move(gs.move_log[-1], screen, gs.board, clock)
valid_moves = gs.get_valid_moves()
if gs.first_white_dama or gs.first_black_dama:
highlight_enemy_pawns(screen, gs)
move_made = False
animate = False
draw_game_state(screen, gs, valid_moves, sq_selected)
if gs.check_mate:
game_over = True
if gs.white_to_move:
draw_text(screen, "Black wins by checkmate")
else:
draw_text(screen, "White wins by checkmate")
elif gs.stale_mate:
game_over = True
draw_text(screen, "Lmao stalemate")
clock.tick(max_FPS)
pygame.display.flip()
pygame.quit()
def highlight_squares(screen, gs, valid_moves, sq_selected):
if sq_selected != ():
r, c = sq_selected
if gs.board[r][c][0] == ('w' if gs.white_to_move else 'b'):
s = pygame.Surface((sq_size, sq_size))
s.set_alpha(100)
s.fill(pygame.Color('blue'))
screen.blit(s, (c * sq_size, r * sq_size))
s.fill(pygame.Color('yellow'))
for move in valid_moves:
if move.start_row == r and move.start_col == c:
screen.blit(s, (move.end_col * sq_size, move.end_row * sq_size))
# this is not fucking working idk why fml
def highlight_enemy_pawns(screen, gs):
if len(gs.enemy_pawns) != 0:
r, c = gs.enemy_pawns[0], gs.enemy_pawns[1]
if gs.board[r][c][0] == ('b' if gs.white_to_move else 'w'):
e = pygame.Surface(sq_size, sq_size)
e.set_alpha(100)
e.fill(pygame.Color('red'))
screen.blit(e, (c * sq_size), (r * sq_size))
def draw_game_state(screen, gs, valid_moves, sq_selected):
draw_board(screen)
highlight_squares(screen, gs, valid_moves, sq_selected)
draw_pieces(screen, gs.board)
# noinspection PyGlobalUndefined
def draw_board(screen):
global colors
colors = [pygame.Color('white'), pygame.Color('gray')]
for r in range(Dimension):
for c in range(Dimension):
colour = ((r + c) % 2)
if colour == 0:
color = pygame.Color("white")
else:
color = pygame.Color("gray")
pygame.draw.rect(screen, color, pygame.Rect(c * sq_size, r * sq_size, sq_size, sq_size))
def draw_pieces(screen, board):
for r in range(Dimension):
for c in range(Dimension):
piece = board[r][c]
if piece != "--":
screen.blit(Images[piece], pygame.Rect(c * sq_size, r * sq_size, sq_size, sq_size))
# noinspection PyGlobalUndefined
def animate_move(move, screen, board, clock):
global colors
d_row = move.end_row - move.start_row
d_col = move.end_col - move.start_col
frames_per_square = 2
frame_count = (abs(d_row) + abs(d_col)) * frames_per_square
for frame in range(frame_count + 1):
r, c = (move.start_row + d_row * frame / frame_count, move.start_col + d_col * frame / frame_count)
draw_board(screen)
draw_pieces(screen, board)
color = colors[(move.end_row + move.end_col) % 2]
end_square = pygame.Rect(move.end_col * sq_size, move.end_row * sq_size, sq_size, sq_size)
pygame.draw.rect(screen, color, end_square)
if move.piece_captured != '--':
screen.blit(Images[move.piece_captured], end_square)
warnings.filterwarnings("ignore", category=DeprecationWarning)
screen.blit(Images[move.piece_moved], pygame.Rect(c * sq_size, r * sq_size, sq_size, sq_size))
pygame.display.flip()
clock.tick(60)
def draw_text(screen, text):
font = pygame.font.SysFont('Helvetica', 32, True, False)
text_object = font.render(text, 5, pygame.Color('white'))
text_location = pygame.Rect(0, 0, Width, Height).move(Width / 2 - text_object.get_width() / 2,
Height / 2 - text_object.get_height() / 2)
b = pygame.Surface((Width, Height))
b.set_alpha(150)
b.fill(pygame.Color('black'))
screen.blit(b, (0, 0))
screen.blit(text_object, text_location)
if __name__ == "__main__":
main()