【问题标题】:How to scale the font size in pygame based on display resolution?如何根据显示分辨率在pygame中缩放字体大小?
【发布时间】:2019-11-13 07:07:40
【问题描述】:

largeText = pygame.font.Font('digifaw.ttf',450)

字号为450,适合分辨率为1366x768的全屏显示文字。如何更改字体大小以使其与其他显示分辨率兼容?我查找了 font 的 pydocs,但找不到与自动缩放相关的任何内容。

更新:这是代码的 sn-p

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('digifaw.ttf',450)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(1)

【问题讨论】:

    标签: python fonts pygame


    【解决方案1】:

    您必须手动缩放字体。如果字体适合高度为 768 的窗口,则您必须将字体缩放current_height/768。例如:

    h = screen.get_height();
    largeText = pygame.font.Font('digifaw.ttf', int(450*h/768))
    

    注意,您可以使用pygame.freetype 模块:

    import pygame.freetype
    
    font = pygame.freetype.Font('digifaw.ttf')
    

    和方法.render_to(),将字体直接渲染到表面:

    h = screen.get_height()
    font.render_to(screen, (x, y), 'text', color, size=int(450*h/768))
    

    如果你想缩放由字体渲染的pygame.Surface的宽度和高度,你必须使用pygame.transform.smoothscale()

    gameDisplay = pygame.display.set_mode(size, pygame.RESIZABLE)
    ref_w, ref_h = gameDisplay.get_size()
    
    def text_objects(text, font):
        textSurface = font.render(text, True, black).convert_alpha()
    
        cur_w, cur_h = gameDisplay.get_size()
        txt_w, txt_h = textSurface.get_size()
        textSurface = pygame.transform.smoothscale(
            textSurface, (txt_w * cur_w // ref_w, txt_h * cur_h // ref_h))
    
        return textSurface, textSurface.get_rect()  
    

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 2023-04-02
      • 2012-11-04
      • 2017-03-08
      • 2022-11-13
      • 2022-12-31
      • 1970-01-01
      • 2017-09-28
      • 2015-04-24
      相关资源
      最近更新 更多