cursor follows the mouse

This commit is contained in:
Vincent Rodley 2025-08-28 14:28:57 +12:00
parent a4998531ac
commit 41cecfc5ee
3 changed files with 19 additions and 8 deletions

View File

@ -38,4 +38,4 @@ Both game modes include:
- Add the CPU - Add the CPU
- animations - animations
- make the cursor follow the mouse - highlight the last dropped tile for the ppl who arent paying attention

View File

@ -2,7 +2,7 @@ import pygame
from pathlib import Path from pathlib import Path
class Button: 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.rect = pygame.Rect(x, y, width, height)
self.text = text self.text = text
self.colour = colour self.colour = colour
@ -10,6 +10,7 @@ class Button:
self.text_colour = text_colour self.text_colour = text_colour
self.current_colour = colour self.current_colour = colour
self.action = action self.action = action
self.hover_action = hover_action
self.extra_data = extra_data self.extra_data = extra_data
self.rounding = rounding self.rounding = rounding
self.outline_colour = outline_colour self.outline_colour = outline_colour
@ -42,7 +43,8 @@ class Button:
self.current_colour = self.colour self.current_colour = self.colour
def handle_event(self, event): def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(event.pos): if event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.rect.collidepoint(event.pos) and callable(self.action):
if self.action: self.action(self)
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() self.update_colour()

View File

@ -77,7 +77,8 @@ def create_tiles():
x, y, TILE_SIZE, TILE_SIZE, "", x, y, TILE_SIZE, TILE_SIZE, "",
tile_colour, tile_hover, tile_text, tile_colour, tile_hover, tile_text,
tile_press, None, 30, (c, r), 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) col.append(tile)
tiles.append(col) tiles.append(col)
@ -136,9 +137,12 @@ def play_move(col_index: int):
if row_dropped is None: if row_dropped is None:
return return
win = check_win() wins = check_win()
if win: if wins:
winner = player winner = player
for winX, winY in wins:
tiles[winX][winY].outline_width = 5
tiles[winX][winY].outline_colour = (0, 255, 0)
board_full = True board_full = True
elif is_board_full(): elif is_board_full():
winner = None winner = None
@ -150,6 +154,11 @@ def tile_press(tile: Button):
col_index, _row_index = tile.extra_data col_index, _row_index = tile.extra_data
play_move(col_index) 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 # buttons
width, height = 280, 75 width, height = 280, 75
x = WINDOW_WIDTH / 2 - width / 2 x = WINDOW_WIDTH / 2 - width / 2