本文介绍了如何根据显示分辨率缩放pygame中的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Font size is 450 and is suitable for the displaying the text in a full screen display of resolution 1366x768. How do I change the font size such that it is compatible with other display resolutions ? I looked up the pydocs for font and couldn't find anything related to auto scaling.

更新:这是代码段

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)

推荐答案

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

You've to scale the font manually. If a font is suited for a window with a height of 768, the you've to scale the font by current_height/768. e.g.:

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

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

Note, you can use the pygame.freetype module:

import pygame.freetype

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

和方法 .render_to() ,将字体直接呈现在表面上:

and the method .render_to(), to render the font directly to a surface:

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

这篇关于如何根据显示分辨率缩放pygame中的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:42