【问题标题】:Text not center aligned in Pygame [duplicate]Pygame中的文本未居中对齐[重复]
【发布时间】:2021-10-10 03:32:22
【问题描述】:
'''
start_msg = welcome_screen.render("Press Y to start or autostart in 10 seconds", True, (255,255,255))
startmsg_rect = welcome.get_rect(center = (int(displayWidth/2), int(displayHeight/3)))


display.blit(start_msg, startmsg_rect)

pygame.display.flip()
'''

文本没有居中对齐。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    welcome 是什么?

    startmsg_rect 应该来自 start_msg,呈现的文本。这样,startmsg_rect 就具有正确的尺寸,允许使用 center 参数以方便的方式使文本明显居中。

    startmsg_rect = start_msg.get_rect(center = (displayWidth/2, displayHeight/3))
    

    我也相信center 的参数不必是整数,它们会被矩形本身舍入或截断。

    【讨论】:

      【解决方案2】:

      文字没有居中对齐,因为高度除以3。更进一步你需要得到文字的边界矩形(start_msg):

      startmsg_rect = welcome.get_rect(center = (int(displayWidth/2), int(displayHeight/3)))

      startmsg_rect = start_msg.get_rect(center = (displayWidth // 2, displayHeight // 2)
      

      您也可以使用pygame.display.get_surface

      获得显示Surface
      screen_center = pygame.display.get_surface().get_rect().center
      startmsg_rect = start_msg.get_rect(center = screen_center)
      display.blit(start_msg, startmsg_rect)
      

      【讨论】:

        猜你喜欢
        • 2021-10-27
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 2017-06-12
        • 1970-01-01
        • 2018-10-18
        相关资源
        最近更新 更多