From 257f4f9ed11cdce6f6fe7f933d26ea34f2ad6ba5 Mon Sep 17 00:00:00 2001 From: Angel ILIEV Date: Thu, 2 May 2019 10:38:27 +0300 Subject: [PATCH] - Added menu button parameters to constants.py - 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 --- gui/colors.py | 1 + gui/constants.py | 15 +++++++++++++++ gui/gui_app.py | 47 +++++++++++++++++++++++++++++++++++++++-------- gui/gui_board.py | 12 +++++++----- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/gui/colors.py b/gui/colors.py index 7f509b5..5bc7f37 100644 --- a/gui/colors.py +++ b/gui/colors.py @@ -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) diff --git a/gui/constants.py b/gui/constants.py index 2740f08..60a4978 100644 --- a/gui/constants.py +++ b/gui/constants.py @@ -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, diff --git a/gui/gui_app.py b/gui/gui_app.py index 66926b5..60f1c90 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -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() @@ -63,7 +63,6 @@ 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)) @@ -71,20 +70,36 @@ 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: @@ -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() @@ -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() diff --git a/gui/gui_board.py b/gui/gui_board.py index cb2976c..e3cd5b1 100644 --- a/gui/gui_board.py +++ b/gui/gui_board.py @@ -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)) @@ -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)