Food buttons

This commit is contained in:
Joe Penny 2025-03-22 17:57:04 +00:00
commit 8c82091236
4 changed files with 32 additions and 37 deletions

View file

@ -18,8 +18,6 @@ def main():
for _ in pygame.event.get(eventtype=pygame.QUIT):
running = False
pygame.mouse.set_cursor(*pygame.cursors.arrow)
for event in pygame.event.get(eventtype=util.TRANSITION_EVENT_TYPE):
next_scene = event.dict["to"]
match next_scene:

View file

@ -4,37 +4,15 @@ import pygame
import resources
class Button:
class SurfaceButton:
surface: pygame.SurfaceType
text: str
size: tuple[int, int]
font: pygame.font.Font
already_pressed: bool
already_collided: bool
def __init__(self, text, size, font=resources.FONT_SM):
self.surface = pygame.Surface(size)
self.surface.fill((255, 0, 255))
self.surface.set_colorkey((255, 0, 255))
def __init__(self, surface):
self.surface = surface
self.already_pressed = False
self.already_collided = False
self.font = font
self.text = text
self.size = size
(text_width, text_height) = self.font.size(text)
self.text_pos = (
(size[0] - text_width) / 2,
(size[1] - text_height) / 2,
)
def _draw(self):
pygame.draw.rect(self.surface, 0xdf3062, pygame.Rect((0, 0), self.size), border_radius=5)
self.surface.blit(
self.font.render(self.text, True, "black"),
self.text_pos,
)
def blit_onto(self, output_surface: pygame.SurfaceType, pos: tuple[int, int]) -> bool:
"""
@ -44,14 +22,12 @@ class Button:
:param pos: position to place the button
:return: if the button is clicked or not - is debounced
"""
self._draw()
output_surface.blit(
self.surface,
pos
)
does_mouse_collide = pygame.Rect(*pos, *self.size).collidepoint(*pygame.mouse.get_pos())
does_mouse_collide = pygame.Rect(*pos, *self.surface.get_size()).collidepoint(*pygame.mouse.get_pos())
if does_mouse_collide != self.already_collided:
self.already_collided = does_mouse_collide
@ -71,3 +47,23 @@ class Button:
return False
return False
def text_button(text, size, font=resources.FONT_SM) -> SurfaceButton:
surface = pygame.Surface(size)
surface.fill((255, 0, 255))
surface.set_colorkey((255, 0, 255))
(text_width, text_height) = font.size(text)
text_pos = (
(size[0] - text_width) / 2,
(size[1] - text_height) / 2,
)
pygame.draw.rect(surface, 0xdf3062, pygame.Rect((0, 0), size), border_radius=5)
surface.blit(
font.render(text, True, "black"),
text_pos,
)
return SurfaceButton(surface)

View file

@ -5,13 +5,13 @@ import util
class Menu:
surface: pygame.SurfaceType
play_button: components.Button
quit_button: components.Button
play_button: components.SurfaceButton
quit_button: components.SurfaceButton
def __init__(self, surface):
self.surface = surface
self.play_button = components.Button("Play", (250, 50), font=resources.FONT)
self.quit_button = components.Button("Quit", (250, 50), font=resources.FONT)
self.play_button = components.text_button("Play", (250, 50), font=resources.FONT)
self.quit_button = components.text_button("Quit", (250, 50), font=resources.FONT)
def do(self):
self.surface.blit(resources.SPLASH_SCREEN_IMAGE, (0, 0))
@ -24,10 +24,10 @@ class Menu:
)
gap = 15
central_button_block = util.center_within(self.surface.get_size(), (250, (self.play_button.size[1] * 2) + gap))
central_button_block = util.center_within(self.surface.get_size(), (250, (50 * 2) + gap))
if self.play_button.blit_onto(self.surface, central_button_block):
pygame.event.post(util.make_transition_event("gameplay"))
if self.quit_button.blit_onto(self.surface, util.add_coord(central_button_block, (0, self.play_button.size[1] + gap))):
if self.quit_button.blit_onto(self.surface, util.add_coord(central_button_block, (0, 50 + gap))):
pygame.event.post(pygame.event.Event(pygame.QUIT))

View file

@ -11,4 +11,5 @@ FONT = pygame.font.Font(RESOURCES_DIR / "Jersey10-Regular.ttf", size=40)
FONT_LG = pygame.font.Font(RESOURCES_DIR / "Jersey10-Regular.ttf", size=60)
FONT_XL = pygame.font.Font(RESOURCES_DIR / "Jersey10-Regular.ttf", size=100)
SPLASH_SCREEN_IMAGE = pygame.image.load(RESOURCES_DIR / "splash.png")
SPLASH_SCREEN_IMAGE = pygame.image.load(RESOURCES_DIR / "splash.png")