Skip to content

Commit

Permalink
- Added menu button parameters to constants.py
Browse files Browse the repository at this point in the history
- On game end, now we wait for user to click anywhere before going back to main menu
- Added menu loop
- Changed message_display() to only update canvas if a flag is set
  • Loading branch information
AngelVI13 committed May 2, 2019
1 parent 05da26b commit 257f4f9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
1 change: 1 addition & 0 deletions gui/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
RED = (255, 94, 88)
RED_HIGHLIGHT = (255, 219, 212)
GREEN = (0, 200, 0)
GREEN_HIGHLIGHT = (0, 255, 0)
BLUE = (59, 214, 255)
BLUE_HIGHLIGHT = (164, 217, 221)
GREY = (170, 191, 193)
Expand Down
15 changes: 15 additions & 0 deletions gui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@
# offset for main grid from main window
OFFSET_X, OFFSET_Y = (DISPLAY_WIDTH - BOARD_WIDTH) / 2, (DISPLAY_HEIGHT - BOARD_HEIGHT) / 2

# Menu buttons
BUTTON_WIDTH, BUTTON_HEIGHT = 300, 50
BUTTON_Y_SPACING = 1.5

MENU_BUTTON_POSITIONS = {
0: {'x': (DISPLAY_WIDTH-BUTTON_WIDTH) / 2, 'y': (DISPLAY_HEIGHT / 3) + 1*BUTTON_HEIGHT*BUTTON_Y_SPACING,
'w': BUTTON_WIDTH, 'h': BUTTON_HEIGHT, 'ic': GREEN, 'ac': GREEN_HIGHLIGHT},
1: {'x': (DISPLAY_WIDTH-BUTTON_WIDTH) / 2, 'y': (DISPLAY_HEIGHT / 3) + 2*BUTTON_HEIGHT*BUTTON_Y_SPACING,
'w': BUTTON_WIDTH, 'h': BUTTON_HEIGHT, 'ic': GREEN, 'ac': GREEN_HIGHLIGHT},
2: {'x': (DISPLAY_WIDTH-BUTTON_WIDTH) / 2, 'y': (DISPLAY_HEIGHT / 3) + 3*BUTTON_HEIGHT*BUTTON_Y_SPACING,
'w': BUTTON_WIDTH, 'h': BUTTON_HEIGHT, 'ic': GREEN, 'ac': GREEN_HIGHLIGHT},
3: {'x': (DISPLAY_WIDTH-BUTTON_WIDTH) / 2, 'y': (DISPLAY_HEIGHT / 3) + 4*BUTTON_HEIGHT*BUTTON_Y_SPACING,
'w': BUTTON_WIDTH, 'h': BUTTON_HEIGHT, 'ic': RED, 'ac': RED_HIGHLIGHT},
}

GRID_RESULT_COLORS = {
PLAYER_X: RED_HIGHLIGHT,
PLAYER_O: BLUE_HIGHLIGHT,
Expand Down
47 changes: 39 additions & 8 deletions gui/gui_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
self.allowed_cells = set()
self.grids_with_result = set() # all grids that have a result on them

def restart_game(self):
def reset_game(self):
self.board = UltimateBoard()
self.allowed_cells = set()
self.grids_with_result = set()
Expand Down Expand Up @@ -63,28 +63,43 @@ def click_cell_under_mouse(self, pos):

def draw_results(self): # todo this is the same as draw_all moves? parameterize
for grid in self.grids_with_result:
# print(grid.player, GRID_RESULT_COLORS[grid.player])
pygame.draw.rect(self.gameDisplay, GRID_RESULT_COLORS[grid.player],
(grid.pos_x, grid.pos_y, grid.width, grid.height))

def check_for_game_over(self):
result = self.board.get_result(player_jm=PLAYER_X)
if result is not None:
self.message_display(text=RESULT_TEXT[result])
time.sleep(PAUSE_BEFORE_GAME_RESTART)
self.restart_game()
self.message_display(text='Click anywhere to continue...',
pos=(DISPLAY_WIDTH / 2, (BOARD_HEIGHT + OFFSET_Y + 30)), font='comicsansms', size=20)

# Wait until a mouse click before going back to main menu
clicked = False
while not clicked:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
self.quit_game()
elif event.type == pygame.MOUSEBUTTONUP:
clicked = True
return True # game is over
return False # game not over

def click_random_cell(self):
random_cell = random.choice(list(self.allowed_cells))
self.subcell_clicked(random_cell)
time.sleep(0.2) # sleep 1s so output can be checked

def do_nothing(self):
pass # todo remove this later

def game_loop(self):
self.reset_game()
# set up an endless cycle of B values (rgB) for highlighting moves
highlight_range = [i for i in range(HIGHLIGHT_LOW, HIGHLIGHT_HIGH+1, HIGHLIGHT_STEP)]
brightness_iter = cycle(chain(highlight_range, reversed(highlight_range)))

while True:
while not self.check_for_game_over():
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
Expand All @@ -102,7 +117,7 @@ def game_loop(self):
if not self.allowed_cells:
self.allowed_cells = self.find_allowed_cells()

self.click_random_cell()
self.click_random_cell() # todo add support for single player and two player game

self.draw_clicked_cells()
self.draw_results()
Expand All @@ -111,9 +126,25 @@ def game_loop(self):
pygame.display.update()
self.clock.tick(FRAMES_PER_SECOND)

self.check_for_game_over()
def menu_loop(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit_game()

self.gameDisplay.fill(WHITE)
self.message_display("Ultimate Tic Tac Toe", pos=(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 3),
font='comicsansms', size=40, update=False)

self.button("Single Player", **MENU_BUTTON_POSITIONS[0], action=self.game_loop)
self.button("Two Player", **MENU_BUTTON_POSITIONS[1], action=self.do_nothing)
self.button("Settings", **MENU_BUTTON_POSITIONS[2], action=self.do_nothing)
self.button("Quit", **MENU_BUTTON_POSITIONS[3], action=self.quit_game)

pygame.display.update()
self.clock.tick(15)


if __name__ == '__main__':
g = Gui()
g.game_loop()
g.menu_loop()
12 changes: 7 additions & 5 deletions gui/gui_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ def get_text_objects(text, font):
text_surface = font.render(text, True, BLACK)
return text_surface, text_surface.get_rect()

def message_display(self, text, pos=None, size=60):
def message_display(self, text, pos=None, font='freesansbold.ttf', size=60, update=True):
if pos is None:
pos_x, pos_y = DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2
else:
pos_x, pos_y = pos

large_text = pygame.font.Font('freesansbold.ttf', size)
large_text = pygame.font.SysFont(font, size)
text_surf, text_rect = self.get_text_objects(text, large_text)
text_rect.center = (pos_x, pos_y)
self.gameDisplay.blit(text_surf, text_rect)
pygame.display.update()

if update:
pygame.display.update()

def button(self, msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
# print(click)
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(self.gameDisplay, ac, (x, y, w, h))

Expand Down Expand Up @@ -163,4 +165,4 @@ def draw_side_to_move(self, player_to_move):
# todo remove magic numbers
pygame.draw.rect(self.gameDisplay, BLACK, (OFFSET_X+8, OFFSET_Y-22, 24, 14))
pygame.draw.rect(self.gameDisplay, self.colors[player_to_move], (OFFSET_X+10, OFFSET_Y-20, 20, 10))
self.message_display(text=' to move', pos=(OFFSET_X+60, OFFSET_Y-15), size=14)
self.message_display(text=' to move', pos=(OFFSET_X+60, OFFSET_Y-15), font='comicsansms', size=14)

0 comments on commit 257f4f9

Please sign in to comment.