如何根据显示分辨率将字体尺寸缩放为pygame?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以手动扩展字体。如果字体适用于高度为768的窗口,则您可以按
current_height/768
缩放字体。例如:注意,您可以使用
pygame.freetype.freetype a>模块:
和方法 () ,要直接将字体渲染到表面:
如果要缩放
pygame.surface
由字体渲染,您使用pygame.transform.transform.smoothscale()
::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.:Note, you can use the
pygame.freetype
module:and the method
.render_to()
, to render the font directly to a surface:If you want to scale the width and the height of the
pygame.Surface
which is rendered by the font, the you've to usepygame.transform.smoothscale()
: