如何以不同的方式更新屏幕的字体?
How to update the font of the screen in different ways?
在下方您可以看到 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))
# [...]
在下方您可以看到 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))
# [...]