Python Pygame字体颜色不';不变
我创建了一个按钮类:Python Pygame字体颜色不';不变,python,button,colors,pygame,Python,Button,Colors,Pygame,我创建了一个按钮类: 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.Sys
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
我想在鼠标悬停时改变字体颜色,但显然,这与改变按钮的颜色不同,因为我所做的不起作用。有人知道当鼠标悬停在文本上时如何更改文本颜色吗?(我不知道我的错误是什么)在渲染文本和绘制按钮之前设置颜色:
class菜单按钮:
# [...]
def牵引(自):
mouse\u pos=pygame.mouse.get\u pos()
font=pygame.font.SysFont(self.font,self.size)
w、 h=字体大小(self.text)
button_rect=pygame.rect(0,0,w,h)
按钮\u rect.center=self.pos
c=自身颜色
如果按钮直接碰撞点(鼠标位置):
c=红色
button_text=font.render(self.text,True,c)
屏幕。blit(按钮文本、按钮矩形)