【问题标题】:Pygame Font Color doesn't ChangePygame 字体颜色不变
【发布时间】:2021-06-30 14:25:30
【问题描述】:

我创建了一个按钮类:

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

而且我想在鼠标悬停时更改字体颜色,但显然,这与更改按钮颜色不同,因为我所做的没有奏效。有人知道当鼠标悬停在文本上时如何更改文本颜色? (我不知道我的错误是什么)

【问题讨论】:

    标签: python button colors pygame


    【解决方案1】:

    在渲染文本和绘制按钮之前设置颜色:

    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)
    

    【讨论】:

      猜你喜欢
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 2018-04-04
      相关资源
      最近更新 更多