下面您在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 :(得分:1)
只需在循环中为[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))
# [...]