-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessMain.py
106 lines (87 loc) · 3.38 KB
/
ChessMain.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
import pygame
import ChessEngine
from ChessEngine import Move
IMAGES = {}
WIDTH = HEIGHT = 512
DIMENSION = 8 # for the chess board
SQUARE_SIZE = WIDTH // DIMENSION
def load_images():
global IMAGES
pieces = ["bR", "bN", "bB", "bQ", "bK", "bp", "wR", "wN", "wB", "wQ", "wK", "wp"]
for piece in pieces:
IMAGES[piece] = pygame.image.load(f"images/{piece}.png")
def draw_state(screen, board, sqSelected):
draw_board(screen)
highlight_squares(screen, sqSelected)
draw_pieces(screen, board)
def draw_board(screen):
colors = [pygame.Color("white"), pygame.Color("gray")]
for r in range(DIMENSION):
for c in range(DIMENSION):
color = colors[(r + c) % 2]
pygame.draw.rect(screen, color, pygame.Rect(c * SQUARE_SIZE, r * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
def draw_pieces(screen, board):
# board = game_state.board
for r in range(DIMENSION):
for c in range(DIMENSION):
piece = board[r][c]
if piece != "--":
screen.blit(IMAGES[piece], pygame.Rect(c * SQUARE_SIZE, r * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
def highlight_squares(screen, selected_sq):
if selected_sq:
# print(selected_sq)
r, c = selected_sq
s = pygame.Surface((SQUARE_SIZE, SQUARE_SIZE))
s.set_alpha(100)
s.fill(pygame.Color('red'))
screen.blit(s, (c * SQUARE_SIZE, r * SQUARE_SIZE))
def main():
pygame.init()
load_images()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
screen.fill(pygame.Color("white"))
game_state = ChessEngine.GameState()
valid_moves = game_state.get_valid_moves()
move_made = False
running = True
selected_sq = () # keeping track of last click
player_clicks = [] # keep track of last 2 clicks
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
elif e.type == pygame.MOUSEBUTTONDOWN:
print(player_clicks)
location = pygame.mouse.get_pos()
col = location[0] // SQUARE_SIZE
row = location[1] // SQUARE_SIZE
if selected_sq == (row, col):
selected_sq = ()
player_clicks = []
else:
selected_sq = (row, col)
player_clicks.append(selected_sq)
toPlay = "w" if game_state.white_to_move else "b"
if len(player_clicks) == 2:
move = Move(game_state.board, player_clicks[0], player_clicks[1])
# print(move.get_notation())
if move in valid_moves:
game_state.make_move(move)
move_made = True
player_clicks = []
selected_sq = []
else:
player_clicks = [selected_sq]
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_z:
game_state.undo_move()
move_made = True
if move_made:
valid_moves = game_state.get_valid_moves()
move_made = False
draw_state(screen, game_state.board, selected_sq)
clock.tick(60)
pygame.display.flip()
if __name__ == "__main__":
main()