-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.py
61 lines (60 loc) · 2.49 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
import pygame as p
from Piece import Piece
from Board import Board
from Move import Move
WIDTH = HEIGHT = 640
DIMENSION = 8 #8 by 8 board
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
def main():
screen = p.display.set_mode((WIDTH, HEIGHT)) #create the screen
clock = p.time.Clock() #create a clock
board = Board(SQ_SIZE, DIMENSION)
screen.fill(p.Color("Black"))
player_clicks = []
sq_selected = ()
moveMade = False
run = True
while run:
for event in p.event.get():
if event.type == p.QUIT:
run = False
elif event.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos() #(x,y) location of mouse
col = location[0]//SQ_SIZE
row = location[1]//SQ_SIZE
if sq_selected == (row, col): # check if user click the same square twice
sq_selected = () # deselct the squares
player_clicks = [] # clear playerClicks
else:
sq_selected = (row, col)
player_clicks.append(sq_selected) # append for both and second clicks
if len(player_clicks) == 2: # after second clicks
move = Move(player_clicks[0],player_clicks[1],board.board)
moves = board.get_all_moves()
valid_moves = board.get_valid_moves( moves )
for valid_move in valid_moves:
if valid_move == move:
board.make_move(move)
board.movelog.append(move)
moveMade = True
break
if moveMade:
board.get_all_moves()
moveMade = False
sq_selected = () # reset user clicks
player_clicks = []
else:
player_clicks = [sq_selected]
elif event.type == p.KEYDOWN:
if event.key == p.K_z:
board.undo_move()
clock.tick(MAX_FPS)
if sq_selected:
board.highlight(sq_selected)
board.highlight_move(sq_selected)
board.draw(screen)
board.draw_moves(screen)
p.display.flip()
if __name__ == "__main__":
main()