【问题标题】:How to Center Text in Pygame如何在 Pygame 中将文本居中
【发布时间】:2014-07-21 21:13:54
【问题描述】:

我有一些代码:

# draw text
font = pygame.font.Font(None, 25)
text = font.render("You win!", True, BLACK)
screen.blit(text, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2])

我怎样才能得到文本的宽度和高度,所以我可以像这样居中文本:

screen.blit(text, [SCREEN_WIDTH / 2 - text_w / 2, SCREEN_HEIGHT / 2 - text_h / 2])

如果这是不可能的,还有什么办法? 我找到了this的例子,但我不是很明白。

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    当你抓住它时,你总是可以让文本矩形居中:

    # draw text
    font = pygame.font.Font(None, 25)
    text = font.render("You win!", True, BLACK)
    text_rect = text.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    screen.blit(text, text_rect)
    

    只是另一种选择

    【讨论】:

    • 这是该问题的最佳答案。
    • 好,你可以用SURF.get_width // 2和SURF.height // 2得到中心宽和高
    【解决方案2】:

    您可以使用text.get_rect() 获取渲染文本图像的尺寸,它返回一个具有width 和height 属性的Rect 对象,以及其他属性(请参阅链接文档以获取完整列表)。 IE。你可以简单地做text.get_rect().width。

    【讨论】:

      【解决方案3】:

      为了简化文本的使用,我使用了这个函数(将其居中 x 没有用)

      import pygame
      
      pygame.init()
      WIDTH = HEIGHT = 500
      screen = pygame.display.set_mode((WIDTH, HEIGHT))
      font = pygame.font.SysFont("Arial", 14)
      
      
      def write(text, x, y, color="Coral",):
          text = font.render(text, 1, pygame.Color(color))
          text_rect = text.get_rect(center=(WIDTH//2, y))
          return text, text_rect
      
      text, text_rect = write("Hello", 10, 10) # this will be centered anyhow, but at 10 height
      loop = 1
      while loop:
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  loop = 0
          screen.blit(text, text_rect)
          pygame.display.update()
      
      pygame.quit()
      

      【讨论】:

        【解决方案4】:

        pygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数center 指定矩形的中心。这些关键字参数在返回之前应用于pygame.Rect 的属性(有关关键字参数的完整列表,请参见pygame.Rect)。

        获取文本矩形并将文本矩形的中心放在窗口矩形的中心:

        text_rect = text.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        

        你甚至可以从显示Surface获得窗口的中心:

        text_rect = text.get_rect(center = screen.get_rect().center)
        

        或者使用pygame.display.get_surface():

        text_rect = text.get_rect(center = pygame.display.get_surface().get_rect().center)
        

        可以使用blit 方法在另一个Surface 上绘制一个Surface。第二个参数是表示左上角的元组 (x, y) 或矩形。对于矩形,只考虑矩形的左上角。因此,您可以将文本矩形直接传递给blit:

        screen.blit(text, text_rect)
        

        小例子:

        import pygame
        import pygame.font
        
        pygame.init()
        font = pygame.font.SysFont(None, 50)
        text = font.render('Hello World', True, (255, 0, 0))
        
        window = pygame.display.set_mode((300, 100))
        clock = pygame.time.Clock()
        
        run = True
        while run:
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
        
            window.fill(0)
            window.blit(text, text.get_rect(center = window.get_rect().center))
            pygame.display.flip()
        
        pygame.quit()
        exit()
        

        【讨论】:

          【解决方案5】:

          渲染的文本在 Pygame 中的透明表面上进行 blitted。所以你可以使用这里描述的表面类的方法: http://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_width

          所以对你来说,以下是可行的:

          text.get_width()

          text.get_height()

          【讨论】:

            猜你喜欢
            • 2021-09-22
            • 2021-03-31
            • 1970-01-01
            • 2014-09-02
            • 2014-08-08
            • 2011-06-12
            • 2012-07-23
            • 1970-01-01
            • 2021-10-10
            相关资源
            最近更新 更多