-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonChess.py
60 lines (51 loc) · 2.03 KB
/
PythonChess.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
import pygame as pg
import sys
from BoardRenderer import BoardRenderer
from ChessBoard import ChessBoard, Move
from GameManager import GameManager
def handle_events():
global start_square
for event in pg.event.get():
# 1. Regular mouse clicks
if event.type == pg.MOUSEBUTTONDOWN or event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
mouse_field = renderer.coordinates_to_field(*mouse_pos)
mouse_square = chessboard.get_square(*mouse_field)
# 1a. no start_square set -> highlight fields
if not start_square and event.type == pg.MOUSEBUTTONDOWN:
if highlighted_fields := chessboard.legal_moves_from(mouse_square):
start_square = mouse_square
renderer.draw_board(highlighted_fields)
# 1b. start_square set and mouse square is valid chess move -> move
elif event.type == pg.MOUSEBUTTONUP and start_square:
if mouse_square in [move.end_square for move in chessboard.legal_moves_from(start_square)]:
game_manager.exec_move(start_square, mouse_square)
start_square = None
renderer.draw_board()
# 1c. clicked on start_square again -> reset
elif event.type == pg.MOUSEBUTTONDOWN and mouse_square == start_square:
start_square = None
renderer.draw_board()
# 2. Ctrl+Z -> Takeback
elif event.type == pg.KEYDOWN and event.key == pg.K_z:
game_manager.take_back()
renderer.draw_board()
# 3. Escape -> Quit
elif event.type == pg.QUIT:
pg.quit()
sys.exit()
FPS = 40
# Initialize Pygame
pg.init()
pg.display.set_caption('Philipps Schachprogramm')
# Create game objects
chessboard = ChessBoard()
renderer = BoardRenderer(chessboard)
game_manager = GameManager(chessboard)
start_square = ()
renderer.draw_board()
# Game loop
clock = pg.time.Clock()
while True:
clock.tick(FPS)
handle_events()