-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
39 lines (34 loc) · 1.35 KB
/
controller.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
from board import Board
from gameState import GameState
import pygame
class Controller:
def __init__(self):
self.board = Board()
self.game_state = GameState()
self.selected_square = [0, 0]
self.possible_moves = []
self.white_turn = True
def select_square(self, square):
self.selected_square = square
self.game_state.select_piece(self.selected_square)
self.possible_moves = self.game_state.get_moves(self.white_turn)
def handle_mouseclick(self):
mouse_state = pygame.mouse.get_pressed()
if mouse_state[0] == True:
mouse_pos = pygame.mouse.get_pos()
pressed_square = self.board.get_square(mouse_pos)
if self.is_possible_move(pressed_square):
self.game_state.move_selected_piece(pressed_square)
self.white_turn = not self.white_turn
self.select_square(pressed_square)
def is_possible_move(self, square):
if self.possible_moves == None:
return False
else:
for move in self.possible_moves:
if square[0] == move[0] and square[1] == move[1]:
return True
return False
def draw(self, surface):
self.board.draw(surface, self.selected_square, self.possible_moves)
self.game_state.draw_pieces(surface)