Made the TextButton a pygame.sprite.Sprite.

This commit is contained in:
The Wobbler 2024-04-20 15:55:49 +02:00
parent 67424f067f
commit 9121342bff

8
pg.py
View file

@ -74,11 +74,13 @@ class Button:
return False return False
class TextButton: class TextButton(pygame.sprite.Sprite):
""" """
Creates a button from just some string and a position. Creates a button from just some string and a position.
""" """
def __init__(self, text: str, position: tuple, function=None, args: tuple=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: tuple=(8, 8), border_radius: int=0, line_thickness: int = 0): def __init__(self, text: str, position: tuple, function=None, args: tuple=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: tuple=(8, 8), border_radius: int=0, line_thickness: int = 0):
pygame.sprite.Sprite.__init__(self)
self.text = text self.text = text
self.position = position self.position = position
self.text_color = text_color self.text_color = text_color
@ -91,7 +93,7 @@ class TextButton:
self.border_radius = border_radius self.border_radius = border_radius
self.line_thickness = line_thickness self.line_thickness = line_thickness
self.surface, self.size = self.generate_surface() self.image, self.size = self.generate_surface()
self.rect = self.make_rect() self.rect = self.make_rect()
self.button = Button(self.rect, self.function, args, self.key) self.button = Button(self.rect, self.function, args, self.key)
@ -113,7 +115,7 @@ class TextButton:
return pygame.Rect(self.position, self.size) return pygame.Rect(self.position, self.size)
def draw(self, surface: pygame.Surface): def draw(self, surface: pygame.Surface):
surface.blit(self.surface, self.position) surface.blit(self.image, self.position)
def check(self, mouse_pos, pressed): def check(self, mouse_pos, pressed):
return self.button.check(mouse_pos, pressed) return self.button.check(mouse_pos, pressed)