From 90c8364e402404188e5342f79c39ac2c6d9313a8 Mon Sep 17 00:00:00 2001 From: Vincent Rodley Date: Thu, 28 Aug 2025 10:43:48 +1200 Subject: [PATCH] test --- gui/button.py | 18 +++-- gui/{game.py => gui_game.py} | 150 +++++++++++++++++++++++++++++------ 2 files changed, 138 insertions(+), 30 deletions(-) rename gui/{game.py => gui_game.py} (62%) diff --git a/gui/button.py b/gui/button.py index 8a7c2ca..3286223 100644 --- a/gui/button.py +++ b/gui/button.py @@ -2,7 +2,7 @@ import pygame from pathlib import Path class Button: - def __init__(self, x, y, width, height, text, colour, hover_colour, text_colour, action, font, font_size, extra_data = None, rounding = 0): + def __init__(self, x, y, width, height, text, colour, hover_colour, text_colour, action, font, font_size, extra_data=None, rounding=0, outline_colour=None, outline_width=0): self.rect = pygame.Rect(x, y, width, height) self.text = text self.colour = colour @@ -12,6 +12,8 @@ class Button: self.action = action self.extra_data = extra_data self.rounding = rounding + self.outline_colour = outline_colour + self.outline_width = outline_width if isinstance(font, pygame.font.Font): self.font = font @@ -24,10 +26,15 @@ class Button: def draw(self, screen): pygame.draw.rect(screen, self.current_colour, self.rect, border_radius=self.rounding) - text_surface = self.font.render(self.text, True, self.text_colour) - text_rect = text_surface.get_rect(center=self.rect.center) - screen.blit(text_surface, text_rect) - + + if self.outline_colour and self.outline_width > 0: + pygame.draw.rect(screen, self.outline_colour, self.rect, width=self.outline_width, border_radius=self.rounding) + + if self.text: + text_surface = self.font.render(self.text, True, self.text_colour) + text_rect = text_surface.get_rect(center=self.rect.center) + screen.blit(text_surface, text_rect) + def update_colour(self): if self.rect.collidepoint(pygame.mouse.get_pos()): self.current_colour = self.hover_colour @@ -38,5 +45,4 @@ class Button: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(event.pos): if self.action: self.action(self) - self.update_colour() diff --git a/gui/game.py b/gui/gui_game.py similarity index 62% rename from gui/game.py rename to gui/gui_game.py index b6dd789..6ead0ca 100644 --- a/gui/game.py +++ b/gui/gui_game.py @@ -13,13 +13,21 @@ TILE_SIZE, TILE_SPACING = 50, 20 GRID_WIDTH = COLS * TILE_SIZE + (COLS - 1) * TILE_SPACING GRID_HEIGHT = ROWS * TILE_SIZE + (ROWS - 1) * TILE_SPACING +# grid origin cache for cursor drawing +GRID_ORIGIN_X = None +GRID_ORIGIN_Y = None + +# cursor attributes +CURSOR_HEIGHT = 18 +CURSOR_MARGIN = 14 + # inits pygame.init() font = pygame.font.Font(ROOT_PATH / "Baloo2-Bold.ttf", 50) display = pygame.display.set_mode(WINDOW_SIZE) clock = pygame.time.Clock() -# coloursss +# colours primary_colour = (70, 130, 180) hover_colour = (51, 102, 145) text_colour = (245, 245, 245) @@ -37,16 +45,27 @@ yellow_tile_hover = (220, 220, 0) tiles = [] player = "red" board_full = False -winner = None # "red", "yellow", or None for draw +winner = None + +# cursor state +cursor_col = 0 + +# repeat cursor move +key_held = None +key_held_time = 0 +initial_delay = 200 # ms before repeat starts +repeat_interval = 100 # ms between repeats # tile + board logic def create_tiles(): - global tiles + global tiles, GRID_ORIGIN_X, GRID_ORIGIN_Y tiles = [] start_x = (WINDOW_SIZE[0] - GRID_WIDTH) // 2 start_y = (WINDOW_SIZE[1] - GRID_HEIGHT) // 2 + GRID_ORIGIN_X, GRID_ORIGIN_Y = start_x, start_y + for c in range(COLS): col = [] for r in range(ROWS): @@ -56,7 +75,7 @@ def create_tiles(): x, y, TILE_SIZE, TILE_SIZE, "", tile_colour, tile_hover, tile_text, tile_press, None, 30, (c, r), - rounding=30 + rounding=30, outline_colour=(0,0,0), outline_width=5 ) col.append(tile) tiles.append(col) @@ -72,8 +91,8 @@ def drop_tile(col_index): else: target_tile.colour = yellow_tile target_tile.hover_colour = yellow_tile_hover - return r # row where tile landed - return None # column full + return r + return None def check_win(): global tiles, player @@ -106,28 +125,30 @@ def is_board_full(): return False return True -def tile_press(tile: Button): +# drop a tile in a column +def play_move(col_index: int): global board_full, winner, player - if board_full: + if board_full or not tiles: return - col_index, row_index = tile.extra_data row_dropped = drop_tile(col_index) if row_dropped is None: - return # column full + return win = check_win() - - # check for win if win: winner = player board_full = True elif is_board_full(): - winner = None # draw + winner = None board_full = True else: player = "yellow" if player == "red" else "red" +def tile_press(tile: Button): + col_index, _row_index = tile.extra_data + play_move(col_index) + # buttons width, height = 280, 75 x = WINDOW_SIZE[0] / 2 - width / 2 @@ -158,10 +179,11 @@ game_over_text = Button(x, 50, width, height / 1.5, "text", # menu callbacks def start_game(): - global board_full, player, winner + global board_full, player, winner, cursor_col board_full = False winner = None player = "red" + cursor_col = 0 create_tiles() menu_manager.change_menu("game") @@ -170,11 +192,60 @@ def draw_settings(display): text_rect = text_surface.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2 - 100)) display.blit(text_surface, text_rect) +# checks if a column is full +def column_is_full(c: int) -> bool: + return all(t.colour != tile_colour for t in tiles[c]) + +# helper for ghost, find the lowest empty tile +def lowest_empty_row(c: int): + for r in reversed(range(ROWS)): + if tiles[c][r].colour == tile_colour: + return r + return None + +# ghost preview tile +def draw_ghost_piece(display): + if not tiles or GRID_ORIGIN_X is None: + return + row = lowest_empty_row(cursor_col) + if row is None: # column full + return + + # semi-transparent ghost color + base_color = red_tile if player == "red" else yellow_tile + ghost_color = (*base_color, 120) # RGBA + + # compute position + x = GRID_ORIGIN_X + cursor_col * (TILE_SIZE + TILE_SPACING) + y = GRID_ORIGIN_Y + row * (TILE_SIZE + TILE_SPACING) + + # create surface with alpha + surf = pygame.Surface((TILE_SIZE, TILE_SIZE), pygame.SRCALPHA) + pygame.draw.circle(surf, ghost_color, (TILE_SIZE//2, TILE_SIZE//2), TILE_SIZE//2 - 4) + # --- NEW: white outline --- + pygame.draw.circle(surf, (255, 255, 255), (TILE_SIZE//2, TILE_SIZE//2), TILE_SIZE//2 - 4, 3) + + display.blit(surf, (x, y)) + +# draws the cursor +def draw_cursor(display): + if not tiles or GRID_ORIGIN_X is None: + return + base_color = red_tile if player == "red" else yellow_tile + color = base_color if not column_is_full(cursor_col) else (120, 120, 120) + col_center_x = GRID_ORIGIN_X + cursor_col * (TILE_SIZE + TILE_SPACING) + TILE_SIZE // 2 + y_top = GRID_ORIGIN_Y + GRID_HEIGHT + CURSOR_MARGIN + points = [ + (col_center_x, y_top), + (col_center_x - TILE_SIZE//3, y_top + CURSOR_HEIGHT), + (col_center_x + TILE_SIZE//3, y_top + CURSOR_HEIGHT), + ] + pygame.draw.polygon(display, color, points) + def draw_game(display): if board_full: if winner: game_over_text.text = f"{winner.title()} wins!" - # Set the text colour based on the winner if winner == "red": game_over_text.text_colour = red_tile elif winner == "yellow": @@ -184,16 +255,14 @@ def draw_game(display): game_over_button.draw(display) else: game_over_text.text = "Red's turn!" if player == "red" else "Yellow's turn!" - if player == "red": - game_over_text.text_colour = red_tile - elif player == "yellow": - game_over_text.text_colour = yellow_tile - - + game_over_text.text_colour = red_tile if player == "red" else yellow_tile game_over_text.draw(display) - -# menu manager init + setup + if not board_full: + draw_cursor(display) + draw_ghost_piece(display) + +# menu manager init menu_manager = MenuManager(display, bg_colour) menu_manager.register_menu("start", @@ -212,10 +281,20 @@ menu_manager.register_menu("game", menu_manager.change_menu("start") +# cursor movement + wrapping +def move_cursor(direction: str): + global cursor_col + if direction == "left": + cursor_col = (cursor_col - 1) % COLS + elif direction == "right": + cursor_col = (cursor_col + 1) % COLS + # main loop if __name__ == "__main__": running = True while running: + dt = clock.tick(TARGET_FPS) # time since last frame + for event in pygame.event.get(): if event.type == pygame.QUIT: running = False @@ -224,9 +303,32 @@ if __name__ == "__main__": running = False elif event.key == pygame.K_d: board_full = True + elif event.key == pygame.K_LEFT: + move_cursor("left") + key_held = "left" + key_held_time = 0 + elif event.key == pygame.K_RIGHT: + move_cursor("right") + key_held = "right" + key_held_time = 0 + elif event.key in (pygame.K_RETURN, pygame.K_KP_ENTER): + if tiles and not board_full and not column_is_full(cursor_col): + play_move(cursor_col) + elif event.type == pygame.KEYUP: + if event.key in (pygame.K_LEFT, pygame.K_RIGHT): + key_held = None + key_held_time = 0 + menu_manager.handle_event(event) + # repeat held key + if key_held: + key_held_time += dt + if key_held_time > initial_delay: + if (key_held_time - initial_delay) // repeat_interval > \ + (key_held_time - initial_delay - dt) // repeat_interval: + move_cursor(key_held) + display.fill(bg_colour) menu_manager.draw() pygame.display.flip() - clock.tick(TARGET_FPS)