dropping tiles in GUI

This commit is contained in:
Vincent Rodley 2025-08-22 20:59:47 +12:00
parent 18bde75910
commit be51abf777
2 changed files with 58 additions and 26 deletions

View File

@ -19,13 +19,14 @@ class Button:
text_rect = text_surface.get_rect(center=self.rect.center) text_rect = text_surface.get_rect(center=self.rect.center)
screen.blit(text_surface, text_rect) screen.blit(text_surface, text_rect)
def handle_event(self, event): def update_color(self):
if event.type == pygame.MOUSEMOTION: if self.rect.collidepoint(pygame.mouse.get_pos()):
if self.rect.collidepoint(event.pos):
self.current_color = self.hover_color self.current_color = self.hover_color
else: else:
self.current_color = self.color self.current_color = self.color
elif event.type == pygame.MOUSEBUTTONDOWN: def handle_event(self, event):
if self.rect.collidepoint(event.pos): if event.type == pygame.MOUSEBUTTONDOWN and self.rect.collidepoint(event.pos):
self.action(self) self.action(self)
self.update_color()

View File

@ -21,6 +21,7 @@ clock = pygame.time.Clock()
# variables # variables
tiles = [] tiles = []
menu_manager = MenuManager(display, (30, 30, 40)) # background color menu_manager = MenuManager(display, (30, 30, 40)) # background color
player = "red"
# colorss # colorss
primary_color = (70, 130, 180) # button background primary_color = (70, 130, 180) # button background
@ -31,6 +32,11 @@ tile_hover = (170, 170, 170) # tile hover
tile_text = (50, 50, 50) # tile text tile_text = (50, 50, 50) # tile text
bg_color = (30, 30, 40) # main background bg_color = (30, 30, 40) # main background
red_tile = (255, 0, 0) # the red that a red tile is
red_tile_hover = (220, 0, 0) # the red that a hovered red tile is
yellow_tile = (255, 255, 0) # the yellow that a yellow tile is
yellow_tile_hover = (220, 220, 0) # the yellow that a hovered yellow tile is
# menu functions # menu functions
def start_game_func(*_): def start_game_func(*_):
create_tiles() create_tiles()
@ -51,21 +57,44 @@ def create_tiles():
start_y = (WINDOW_SIZE[1] - GRID_HEIGHT) // 2 start_y = (WINDOW_SIZE[1] - GRID_HEIGHT) // 2
for c in range(COLS): for c in range(COLS):
col = []
for r in range(ROWS): for r in range(ROWS):
x = start_x + c * (TILE_SIZE + TILE_SPACING) x = start_x + c * (TILE_SIZE + TILE_SPACING)
y = start_y + r * (TILE_SIZE + TILE_SPACING) y = start_y + r * (TILE_SIZE + TILE_SPACING)
tile = Button( tile = Button(
x, y, TILE_SIZE, TILE_SIZE, str(len(tiles)), x, y, TILE_SIZE, TILE_SIZE, str(c * ROWS + r),
tile_color, tile_hover, tile_text, tile_press, None, 30, (len(tiles), c, r), tile_color, tile_hover, tile_text, tile_press, None, 30, (str(c * ROWS + r), c, r),
rounding=5 rounding=5
) )
tiles.append(tile) col.append(tile)
tiles.append(col)
def tile_press(tile): def tile_press(tile: Button):
tile_id, x, y = tile.extra_data global player
print(f"TILE {tile_id} at {x},{y} PRESSED")
_, col_index, _ = tile.extra_data # we only care about column
column = tiles[col_index]
# find the lowest unoccupied tile in this column
for r in reversed(range(ROWS)):
target_tile = column[r]
# check if already taken (colored by a player)
if target_tile.color not in (red_tile, yellow_tile):
# claim this tile for the current player
target_tile.color = red_tile if player == "red" else yellow_tile
target_tile.hover_color = red_tile_hover if player == "red" else yellow_tile_hover
print(f"Player {player} placed at col {col_index}, row {r}")
# switch turn
player = "yellow" if player == "red" else "red"
print(f"Next turn: {player}")
break
else:
# column is full
print(f"Column {col_index} is full!")
# button stuff # button stuff
width, height = 280, 75 width, height = 280, 75
@ -107,11 +136,13 @@ def settings_menu_draw():
# game # game
def game_menu_events(event): def game_menu_events(event):
for tile in tiles: for col in tiles:
for tile in col:
tile.handle_event(event) tile.handle_event(event)
def game_menu_draw(): def game_menu_draw():
for tile in tiles: for col in tiles:
for tile in col:
tile.draw(display) tile.draw(display)
# register the menus # register the menus