【问题标题】:How to align the text in textrect.center to the left?如何将 textrect.center 中的文本向左对齐?
【发布时间】:2018-03-09 14:40:15
【问题描述】:

我有一些代码可以将文本传送到屏幕上,但我只能将它与 对齐,但我希望它在左侧。 这是我现在拥有的:

smallText = pygame.font.Font('freesansbold.ttf', 25)
textSurf, textRect = text_objects(coins, smallText, black)
textRect.center = ((displayWidth-50), (38))
gameDisplay.blit(textSurf, textRect)

这将使文本的中心与给定的坐标对齐,但我希望能够选择最左边的点或左上角的坐标。 我试过textRect.left,但没用。

在有人说“谷歌它”之前,我已经有了,这就是我来这里的原因。

【问题讨论】:

    标签: text pygame python-3.2 blit


    【解决方案1】:

    尝试其他rect 属性之一,例如midlefttopleft。我不太确定您需要哪一个,因为这个问题有点不清楚。

    import pygame as pg
    
    pg.init()
    
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    FONT = pg.font.Font(None, 42)
    
    text_surface = FONT.render('text', True, pg.Color('dodgerblue1'))
    text_rect = text_surface.get_rect()
    text_rect.midleft = (screen.get_width()-50, 38)
    
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
    
        screen.fill((30, 30, 30))
        screen.blit(text_surface, text_rect)
        pg.display.flip()
        clock.tick(30)
    
    pg.quit()
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 2018-05-12
      相关资源
      最近更新 更多