如何根据显示分辨率缩放pygame中的字体大小?

2024-09-30 03:26:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

更新:下面是一段代码

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)


Tags: textobjectsdefdisplay分辨率ttfpygame字体大小
1条回答
网友
1楼 · 发布于 2024-09-30 03:26:10

你必须手动调整字体。如果字体适合高度为768的窗口,则必须按current_height/768缩放字体。e、 g.:

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

注意,您可以使用^{}模块:

^{pr2}$

以及方法^{},将字体直接呈现到一个表面:

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

如果要缩放字体呈现的^{}的宽度和高度,则必须使用^{}

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()  

相关问题 更多 >

    热门问题