From 41cecfc5ee76f0b7d170ea70173dad30664c7d80 Mon Sep 17 00:00:00 2001 From: Vincent Rodley Date: Thu, 28 Aug 2025 14:28:57 +1200 Subject: [PATCH] cursor follows the mouse --- README.md | 2 +- gui/button.py | 10 ++++++---- gui/gui_game.py | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7439658..3910205 100644 --- a/README.md +++ b/README.md @@ -38,4 +38,4 @@ Both game modes include: - Add the CPU - animations -- make the cursor follow the mouse \ No newline at end of file +- highlight the last dropped tile for the ppl who arent paying attention \ No newline at end of file diff --git a/gui/button.py b/gui/button.py index 3286223..89f06dc 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, outline_colour=None, outline_width=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, hover_action = None): self.rect = pygame.Rect(x, y, width, height) self.text = text self.colour = colour @@ -10,6 +10,7 @@ class Button: self.text_colour = text_colour self.current_colour = colour self.action = action + self.hover_action = hover_action self.extra_data = extra_data self.rounding = rounding self.outline_colour = outline_colour @@ -42,7 +43,8 @@ class Button: self.current_colour = self.colour def handle_event(self, event): - if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(event.pos): - if self.action: - self.action(self) + if event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.rect.collidepoint(event.pos) and callable(self.action): + self.action(self) + if event.type == pygame.MOUSEMOTION and self.rect.collidepoint(event.pos) and callable(self.hover_action): + self.hover_action(self) self.update_colour() diff --git a/gui/gui_game.py b/gui/gui_game.py index aa16ba3..f066c97 100644 --- a/gui/gui_game.py +++ b/gui/gui_game.py @@ -77,7 +77,8 @@ def create_tiles(): x, y, TILE_SIZE, TILE_SIZE, "", tile_colour, tile_hover, tile_text, tile_press, None, 30, (c, r), - rounding=30, outline_colour=(0,0,0), outline_width=5 + rounding=30, outline_colour=(0,0,0), outline_width=5, + hover_action=tile_hover_event ) col.append(tile) tiles.append(col) @@ -136,9 +137,12 @@ def play_move(col_index: int): if row_dropped is None: return - win = check_win() - if win: + wins = check_win() + if wins: winner = player + for winX, winY in wins: + tiles[winX][winY].outline_width = 5 + tiles[winX][winY].outline_colour = (0, 255, 0) board_full = True elif is_board_full(): winner = None @@ -150,6 +154,11 @@ def tile_press(tile: Button): col_index, _row_index = tile.extra_data play_move(col_index) +def tile_hover_event(tile: Button): + col_index, _row_index = tile.extra_data + global cursor_col + cursor_col = col_index + # buttons width, height = 280, 75 x = WINDOW_WIDTH / 2 - width / 2