Skip to content

Commit

Permalink
- Fixed highlighting for allowed moves
Browse files Browse the repository at this point in the history
- Added check if clicked cell is in allowed moves
- Optimized finding of allowed moves (now its done only once on cell click)
  • Loading branch information
AngelVI13 committed May 1, 2019
1 parent 23e20c8 commit cd11c26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
9 changes: 5 additions & 4 deletions gui/colors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# colors.py
# Contains color definitions in the form of tuples (length 3 -> RGB representation)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 94, 88) # (250, 0, 0)
RED = (255, 94, 88)
RED_HIGHLIGHT = (255, 219, 212)
AVAILABLE_MOVE_1 = (255, 255, 223) # Yellow
AVAILABLE_MOVE_2 = (255, 255, 255) # Yellow
GREEN = (0, 200, 0)
BLUE = (59, 214, 255) # (0, 0, 250)
BLUE = (59, 214, 255)
YELLOW = (0, 250, 250)

BRIGHT_RED = (255, 0, 0)
Expand Down
40 changes: 29 additions & 11 deletions gui/gui_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import cycle, chain

from gui.gui_board import *


Expand All @@ -6,50 +8,66 @@ def __init__(self):
super(Gui, self).__init__()
self.board = UltimateBoard()
self.on_cell_clicked = self.subcell_clicked
self.allowed_cells = set()

def subcell_clicked(self, x, y, w, h, board, move):
# todo check if the cell that is clicked is also allowed to be clicked else display warning!!!!
# do not provide player just yet, only do so when we are sure we can click the cell
cell = Cell(x, y, w, h, None, board_idx=board, cell_idx=move) # todo fix this *-1
cell = Cell(x, y, w, h, player=None, board_idx=board, cell_idx=move)

if cell in self.clicked_cells: # if cell already clicked -> do nothing
return

if cell not in self.allowed_cells:
return # todo raise warning or display text

cell.player = self.board.playerJustMoved * -1
self.clicked_cells.add(cell)
self.board.make_move(move, board)
print(board, move)
print(self.board)
self.allowed_cells = self.find_allowed_cells()

def find_allowed_cells(self):
allowed_cells = set() # clear currently allowed moves

def draw_allowed_moves(self, color):
moves = self.board.get_moves()
for board, move in moves:
for cell in self.all_cells:
if cell.board_idx == board and cell.cell_idx == move:
pygame.draw.rect(self.gameDisplay, color,
(cell.pos_x, cell.pos_y, cell.width, cell.height))
allowed_cells.add(cell)

return allowed_cells

def draw_allowed_moves(self, color):
for cell in self.allowed_cells:
pygame.draw.rect(self.gameDisplay, color,
(cell.pos_x, cell.pos_y, cell.width, cell.height))

def game_loop(self):
highlight = AVAILABLE_MOVE_1
frames = 0
# set up an endless cycle of B values (rgB) for highlighting moves
highlight_range = [i for i in range(140, 201, 2)]
brightness_iter = cycle(chain(highlight_range, reversed(highlight_range)))

while True:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
self.quit_game()

if frames == 30:
highlight = AVAILABLE_MOVE_1 if highlight == AVAILABLE_MOVE_2 else AVAILABLE_MOVE_2
frames = 0
brightness = next(brightness_iter)
highlight = (255, 255, brightness) # yellow highlight used to accent available moves

self.gameDisplay.fill(WHITE)
self.draw_main_grid()

# todo check for game end here as well. Mind not have allowed cells because game is over
if not self.allowed_cells:
self.allowed_cells = self.find_allowed_cells()

self.draw_clicked_cells()
self.draw_allowed_moves(highlight)
pygame.display.update()
self.clock.tick(60)
frames += 1


if __name__ == '__main__':
Expand Down

0 comments on commit cd11c26

Please sign in to comment.