import pygame class Button: def __init__(self, x, y, width, height, text, colour, hover_colour, text_colour, action, font, font_size, extra_data = None, rounding = 0): self.rect = pygame.Rect(x, y, width, height) self.text = text self.colour = colour self.hover_colour = hover_colour self.text_colour = text_colour self.current_colour = colour self.action = action self.font = pygame.font.Font(font, font_size) self.extra_data = extra_data self.rounding = rounding def draw(self, screen): pygame.draw.rect(screen, self.current_colour, self.rect, border_radius=self.rounding) text_surface = self.font.render(self.text, True, self.text_colour) text_rect = text_surface.get_rect(center=self.rect.center) screen.blit(text_surface, text_rect) def update_colour(self): if self.rect.collidepoint(pygame.mouse.get_pos()): self.current_colour = self.hover_colour else: 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: self.action(self) self.update_colour()