简体   繁体   English

如何在不增加字体分辨率的情况下缩放文本?

[英]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.我试过手动设置 textRect.w 和 textRect.h,我试过 pygame.transform.scale 很多东西,我试过 inflate 方法。 All expecting the text to scale, but no luck.所有人都希望文本能够缩放,但没有运气。

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.较大的文本大小和比例重叠并且间距可以改进,但它应该足以让您进行比较和构建。

意外的扎尔戈

I also changed some variable names to match the Python Style Guide ( PEP 8 ).我还更改了一些变量名称以匹配 Python 风格指南 ( PEP 8 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM