Reputation: 71
I created a button class:
class MenuButton:
def __init__(self, font, size, text, pos, color):
self.font = font
self.size = size
self.text = text
self.pos = pos
self.color = color
def draw(self):
button_text = pygame.font.SysFont(self.font, self.size).render(self.text, True, self.color)
button_rect = button_text.get_rect()
button_rect.center = self.pos
screen.blit(button_text, button_rect)
mouse_pos = pygame.mouse.get_pos()
if button_rect.collidepoint(mouse_pos):
self.color = RED
And I wanted to make the font color change whenever the mouse hovers but apparently, this is different from changing the color of a button because what I did, didn't work. Does someone know how can I make a text color change whenever the mouse hovers over it? (I can't figure out what my mistake is)
Upvotes: 1
Views: 240
Reputation: 210890
Set the color befor rendering the text and drawing the button:
class MenuButton:
# [...]
def draw(self):
mouse_pos = pygame.mouse.get_pos()
font = pygame.font.SysFont(self.font, self.size)
w, h = font.size(self.text)
button_rect = pygame.Rect(0, 0, w, h)
button_rect.center = self.pos
c = self.color
if button_rect.collidepoint(mouse_pos):
c = RED
button_text = font.render(self.text, True, c)
screen.blit(button_text, button_rect)
Upvotes: 1