Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据显示分辨率调整pygame中的字体大小?_Python_Fonts_Pygame - Fatal编程技术网

Python 如何根据显示分辨率调整pygame中的字体大小?

Python 如何根据显示分辨率调整pygame中的字体大小?,python,fonts,pygame,Python,Fonts,Pygame,largeText=pygame.font.font('digifaw.ttf',450) 字体大小为450,适合在分辨率为1366x768的全屏显示器中显示文本。如何更改字体大小,使其与其他显示分辨率兼容?我在pydocs中查找,没有找到任何与自动缩放相关的内容 更新:下面是一段代码 def text_objects(text, font): textSurface = font.render(text, True, black) return textSurface, tex

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)


您必须手动缩放字体。如果字体适合高度为768的窗口,则必须按
当前高度/768
缩放字体。e、 g:

h=screen.get_height();
largeText=pygame.font.font('digifaw.ttf',int(450*h/768))
注意,您可以使用以下模块:

import pygame.freetype
font=pygame.freetype.font('digifaw.ttf'))
以及将字体直接渲染到曲面的方法:

h=screen.get_height()
渲染到(屏幕,(x,y),‘文本’,颜色,大小=int(450*h/768))

如果要缩放字体呈现的宽度和高度,必须使用:

gameDisplay=pygame.display.set_模式(大小,pygame.resizeable)
ref_w,ref_h=gameDisplay.get_size()
def text_对象(文本、字体):
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))
返回textSurface,textSurface.get_rect()