ADD rudementary main menu, needs refinement

This commit is contained in:
2025-04-19 19:26:58 +02:00
parent 7c025cd8fd
commit 45e81b7182
8 changed files with 195 additions and 33 deletions

View File

@@ -1,8 +1,7 @@
import pygame
class Button:
def __init__(self, screen, x, y, width, height, action):
self.screen = screen
def __init__(self, x, y, width, height, action):
self.x = x
self.y = y
self.width = width
@@ -15,7 +14,9 @@ class Button:
## Default values
self.font_size = 20
self.colour = (255, 255, 255)
self.pressed_colour = (200, 200, 200)
self.pressed = False
self.border_radius = 3
## Getters and setters
def get_font_size(self) -> int:
@@ -24,12 +25,18 @@ class Button:
def get_text(self) -> str:
return self.text
def get_border_radius(self) -> int:
return self.border_radius
def set_font_size(self, font_size: int) -> None:
self.font_size = font_size
def set_text(self, text: str) -> None:
self.text = text
def set_border_radius(self, border_radius: int) -> None:
self.border_radius = border_radius
def set_image(self, image_path: str) -> None:
self._load_image(image_path)
@@ -41,11 +48,11 @@ class Button:
self.image = pygame.image.load(image_path)
self.image = pygame.transform.scale(self.image, (self.width, self.height))
def _draw_text(self, text: str, font_size: int) -> None:
def _draw_text(self, screen, text: str, font_size: int) -> None:
font = pygame.font.Font(None, font_size)
text_surface = font.render(text, True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(self.x + self.width // 2, self.y + self.height // 2))
self.screen.blit(text_surface, text_rect)
screen.blit(text_surface, text_rect)
def _button_press_down(self) -> None:
self.pressed = True
@@ -54,7 +61,6 @@ class Button:
def _button_press_up(self) -> None:
self.pressed = False
pass
## Public methods
def handle_event(self, event: pygame.event.Event) -> None:
@@ -65,17 +71,17 @@ class Button:
if self.pressed and event.type == pygame.MOUSEBUTTONUP:
self._button_press_up()
def draw(self) -> None:
def draw(self, screen) -> None:
if self.pressed:
pygame.draw.rect(self.screen, (200, 200, 200), (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, self.pressed_colour, (self.x, self.y, self.width, self.height), border_radius=self.border_radius)
else:
pygame.draw.rect(self.screen, self.colour, (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height), border_radius=self.border_radius)
if self.image:
self.screen.blit(self.image, (self.x, self.y))
screen.blit(self.image, (self.x, self.y))
if self.text:
self._draw_text(self.text, self.font_size)
self._draw_text(screen, self.text, self.font_size)
if __name__ == "__main__":
pygame.init()