【问题标题】:Flashing Text in PyGamePyGame 中的闪烁文本
【发布时间】:2015-08-24 13:56:32
【问题描述】:

如何在 PyGame 中制作闪烁的文本?这是我尝试每秒闪烁一次的文本:

text = pygame.font.Font("c:/windows/fonts/visitor1.ttf", 50)

textsurf, textrect = text_objects("Press Any Key To Start", text)

textrect.center = ((res_w / 2), (res_h / 2))

screen.blit(textsurf, textrect)

【问题讨论】:

    标签: python text pygame


    【解决方案1】:

    为了让它闪烁,你必须画一秒钟然后删除它一秒钟,一遍又一遍地重复这个。可以使用自定义计时器事件来计时闪烁。

    #!/usr/bin/env python
    # coding: utf8
    from __future__ import absolute_import, division, print_function
    from itertools import cycle
    import pygame
    
    VISITOR_TTF_FILENAME = '/usr/share/fonts/truetype/aenigma/visitor1.ttf'
    BLINK_EVENT = pygame.USEREVENT + 0
    
    
    def main():
        pygame.init()
        try:
            screen = pygame.display.set_mode((800, 600))
            screen_rect = screen.get_rect()
    
            clock = pygame.time.Clock()
    
            font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
            on_text_surface = font.render(
                'Press Any Key To Start', True, pygame.Color('green3')
            )
            blink_rect = on_text_surface.get_rect()
            blink_rect.center = screen_rect.center
            off_text_surface = pygame.Surface(blink_rect.size)
            blink_surfaces = cycle([on_text_surface, off_text_surface])
            blink_surface = next(blink_surfaces)
            pygame.time.set_timer(BLINK_EVENT, 1000)
    
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        return
                    if event.type == BLINK_EVENT:
                        blink_surface = next(blink_surfaces)
    
                screen.blit(blink_surface, blink_rect)
                pygame.display.update()
                clock.tick(60)
        finally:
            pygame.quit()
    
    
    if __name__ == '__main__':
        main()
    

    除了在关闭阶段完全变黑之外,还可以将文本以较深的颜色进行 blit。

    如果在初始化和主循环中还有更多内容,则将闪烁文本封装在派生自 Pygame 的 Sprite 的类型中可能有助于保持代码清晰。

    【讨论】:

      【解决方案2】:

      我和@BlackJack https://stackoverflow.com/users/3815611/blackjack做了同样的事情 但我让 off_text_surface 透明:

      from __future__ import absolute_import, division, print_function
      from itertools import cycle
      import pygame
      
      VISITOR_TTF_FILENAME = 'pixelFont.ttf'
      BLINK_EVENT = pygame.USEREVENT + 0
      empty = (255,255,255,0)
      
      
      def main():
          pygame.init()
          try:
              screen = pygame.display.set_mode((800, 600))
              screen.fill((255,255,255))
              screen_rect = screen.get_rect()
      
              clock = pygame.time.Clock()
      
              font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
              on_text_surface = font.render(
                  'Press Any Key To Start', True, pygame.Color('green3')
              )
              blink_rect = on_text_surface.get_rect()
              blink_rect.center = screen_rect.center
              off_text_surface = pygame.Surface(blink_rect.size)
              off_text_surface.fill(empty)
              
              blink_surfaces = cycle([on_text_surface, off_text_surface])
              blink_surface = next(blink_surfaces)
              pygame.time.set_timer(BLINK_EVENT, 1000)
      
              while True:
                  for event in pygame.event.get():
                      if event.type == pygame.QUIT:
                          return
                      if event.type == BLINK_EVENT:
                          blink_surface = next(blink_surfaces)
      
                  screen.blit(blink_surface, blink_rect)
                  pygame.display.update()
                  clock.tick(60)
          finally:
              pygame.quit()
      
      
      if __name__ == '__main__':
          main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        相关资源
        最近更新 更多