Search code examples
pythontextpygamescalerectangles

How do I scale the text without increasing the resolution of the font?


import pygame

pygame.init()

run = True
scale = 3
width = 256
height = 224
screen = pygame.display.set_mode((width*scale, height*scale), pygame.FULLSCREEN)
highScore = 0

textColour = (0,0,0)
font = pygame.font.Font("FreeSerif.ttf", 10)
textSurface = font.render(f"HiScore: {highScore}", True, (textColour))
textRect = textSurface.get_rect()
textRect.center = (width*scale//2, height*scale//2)


while run:

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
    highScore += 1
    screen.fill((255, 0, 0))
    textSurface = font.render(f"HiScore: {highScore}", True, (textColour))
    screen.blit(textSurface, textRect)
    pygame.display.update()

I'm making a game with a lower resolution for stylistic purposes, and I have a system for multiplying everything with the scale variable. How do I scale the text without increasing the resolution of the font?

I've tried setting the textRect.w and textRect.h manually, I've tried pygame.transform.scale on multiple things and I've tried the inflate method. All expecting the text to scale, but no luck.


Solution

  • If you're trying to decide on the approach you'd prefer, you can iterate through a few options and see what they look like.

    Here's an example that loops through some font sizes and scaling factors:

    import pygame
    
    pygame.init()
    
    run = True
    scale = 3
    width = 256
    height = 224
    # screen = pygame.display.set_mode((width*scale, height*scale), pygame.FULLSCREEN)
    screen = pygame.display.set_mode((width * scale, height * scale))
    high_score= 0
    
    text_colour = (0, 0, 0)
    fonts = []
    font_sizes = 10, 12, 14, 16, 20, 24, 30, 36
    for size in font_sizes:
        # font = pygame.font.Font("FreeSerif.ttf", size)
        font = pygame.font.Font(None, size) 
        fonts.append(font)
    
    font_scales = [1, 2, 3, 4, 5, 6]
    grid_width = width * scale // len(font_scales)
    grid_height = height * scale // len(font_sizes)
    
    clock = pygame.time.Clock()
    while run:
    
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    run = False
            elif event.type == pygame.QUIT:
                run = False
        high_score+= 1
        screen.fill("red")
        for y, font in enumerate(fonts):
            for x, f_scale in enumerate(font_scales):
                # msg = f"{font_sizes[y]} x {f_scale:2} : {high_score}"
                msg = f"{font_sizes[y]} x {f_scale:2}"
                text_surface = font.render(msg, True, (text_colour))
                text_surface = pygame.transform.scale_by(text_surface, f_scale)
                text_rect = text_surface.get_rect()
                text_rect.left = grid_width * x + 10
                text_rect.top = grid_height * y + 10
                screen.blit(text_surface, text_rect)
    
        pygame.display.update()
        clock.tick(30)
    pygame.quit()
    

    The larger text sizes and scales overlap and the spacing could be improved, but it should be enough for you to compare and build upon.

    Unintended Zalgo

    I also changed some variable names to match the Python Style Guide (PEP 8).