Reputation: 6742
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)
Upvotes: 7
Views: 6748
Reputation: 210878
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()
Upvotes: 5