largeText = pygame.font.Font('digifaw.ttf',450)
字体大小为 450,适合在分辨率 1366x768
的全屏显示中显示文本。如何更改字体大小以使其与其他显示分辨率兼容?我查找了 font 的 pydocs并且找不到与自动缩放相关的任何内容。
更新:这是一段代码
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
缩放字体。例如:
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()
关于python - 如何根据显示分辨率缩放pygame中的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855775/