python - 如何以不同的方式更新屏幕字体?

标签 python fonts pygame screen

下面您可以看到 Stack Overflow 上另一位用户修改过的代码。 它的左侧和右侧包含相同的文本。它还每 2 秒更新一次屏幕上的 2 种字体。现在,是否可以将屏幕分成两半?这意味着,左侧的字体 text5 会动态更新。每 2 秒字体就会被替换一次。

在屏幕右侧,我希望更新字体 text4,但不被替换。这意味着,字体重叠。

如何解决这个问题?

import pygame
import time

    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    done = False

    font = pygame.font.SysFont("comicsansms", 72)


    start = time.time()
    i=0
    F = 0;








        text4 = None
        text5 = None
        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    done = True

                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        F = F + 1
                        text4 = font.render(str(F), True, (128, 128, 0))
                        text5 = font.render(str(F), True, (0, 128, 0))


            passed_time = time.time() - start
            if passed_time > 2 and i < 5:  
                start = time.time()  
                i += 1

            screen.fill((255, 255, 255))

            if text4 != None:
                screen.blit(text4,(460 - text4.get_width() // 1, 40 + i * 20 - text4.get_height() // 2))
                screen.blit(text5,(260 - text5.get_width() // 1, 40 + i * 20 - text5.get_height() // 2))

            # [...]

            pygame.display.flip()
            clock.tick(60)

最佳答案

只需在 [0, i] 范围内的位置循环绘制文本:

for j in range(i+1):
    screen.blit(text4,(460 - text4.get_width() // 1, 40 + j * 20 - text4.get_height() // 2))

例如:

text4, text5 = None, None
while not done:
    for event in pygame.event.get():

        # [...]

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                F = F + 1
                text4 = font.render(str(F), True, (128, 128, 0))
                text5 = font.render(str(F), True, (0, 128, 0))

    # [...]

    screen.fill((255, 255, 255))

    if text4 != None:
        for j in range(i+1):
            screen.blit(text4,(460 - text4.get_width() // 1, 40 + j * 20 - text4.get_height() // 2))
    if text5 != None:
        screen.blit(text5,(260 - text5.get_width() // 1, 40 + i * 20 - text5.get_height() // 2))

    # [...]

关于python - 如何以不同的方式更新屏幕字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511380/

相关文章:

python - 将具有多个值的键映射到python中的json

objective-c - UIButton 自定义字体未显示

text - 如何在 Squeak 中指定文本大小

cocoa - NSTextView 的疯狂字体问题

python - 为什么 pygame.sndarray.make_sound 似乎使声音持续时间增加了四倍?

python - 使用python爬行google搜索url列表

python - 通过pexpect发送命令时如何避免冗余?

Python - 如何在不互相干扰的情况下多次播放相同的声音?

python - 访问类(class)外的 Sprite /类(class)位置

python - 如何托管我自己的私有(private) conda 存储库?