如何根据显示分辨率将字体尺寸缩放为pygame?

发布于 2025-01-24 21:39:27 字数 636 浏览 2 评论 0原文

largetext = pygame.font.font('digifaw.ttf',450)

字体大小为450,适用于在分辨率显示1366x768的全屏显示中显示文本。如何更改字体大小以使其与其他显示分辨率兼容?我查找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)

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

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.

Update: Here's a snippet of the code

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2025-01-31 21:39:27

您可以手动扩展字体。如果字体适用于高度为768的窗口,则您可以按current_height/768缩放字体。例如:

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

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

import pygame.freetype

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

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

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

如果要缩放 pygame.surface 由字体渲染,您使用 pygame.transform.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()  

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

Note, you can use the pygame.freetype module:

import pygame.freetype

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

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

If you want to scale the width and the height of the pygame.Surface which is rendered by the font, the you've to use 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()  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文