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
- 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
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:
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()

View File

@ -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