-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessMain.py
More file actions
91 lines (80 loc) · 3.08 KB
/
chessMain.py
File metadata and controls
91 lines (80 loc) · 3.08 KB
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
#Shows the board and game state
import pygame as p
import chessEngine
WIDTH = HEIGHT = 512
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}
#Initialize images in dictionary
def load_images():
pieces = ['wp', 'wR', 'wN', 'wB', 'wK', 'wQ', 'bp', 'bR', 'bN', 'bB', 'bK', 'bQ']
for piece in pieces:
IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = chessEngine.GameState()
valid_moves = gs.get_valid_moves()
move_made = False
load_images()
running = True
sqSelected = () # follows last square, blank at first
playerClicks = [] #follows what the player clicks, 1st and 2nd click
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
elif e.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos() # x,y
col = location[0]//SQ_SIZE
row = location[1]//SQ_SIZE
if sqSelected == (row, col):
sqSelected = () #deselect
playerClicks = []
else:
sqSelected = (row, col)
playerClicks.append(sqSelected)
if len(playerClicks) == 2:
move = chessEngine.Move(playerClicks[0], playerClicks[1], gs.board)
print(move.get_chess_notation())
for i in range(len(valid_moves)):
if valid_moves[i] == move:
gs.make_move(valid_moves[i])
move_made = True
sqSelected = () #reset user click
playerClicks = []
if not move_made:
playerClicks = [sqSelected]
elif e.type == p.KEYDOWN:
if e.key == p.K_z:
gs.undo_move()
valid_moves = gs.get_valid_moves()
move_made = True
if move_made:
valid_moves = gs.get_valid_moves()
move_made = False
draw_game_state(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()
# used for graphics in game state
def draw_game_state(screen, gs):
draw_board(screen) #draw squares
draw_pieces(screen, gs.board) #draw pieces on top of board
def draw_board(screen):
colors =[p.Color("white"), p.Color("gray")]
for r in range(DIMENSION):
for c in range(DIMENSION):
color = colors[(r + c)%2]
p.draw.rect(screen, color, p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
# uses current game state to draw pieces on the board
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], p.Rect(c * SQ_SIZE, r * SQ_SIZE, SQ_SIZE, SQ_SIZE))
if __name__ == "__main__":
main()