我在Pygame里有个小程序。它每2秒执行一次操作。在我的代码中遗漏的这2秒钟里它到底在做什么。 现在,我可以在程序运行时按Enter键,并计算按下Enter键的时间。 我唯一的问题是,即使我使用pygame.display.update(),字体也不会更新。它总是在屏幕上的同一个地方显示数字。有人能帮我吗
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;
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))
screen.blit(text4,
(460 - text4.get_width() // 1, 40 + i * 20 - text4.get_height() // 2))
pygame.display.update()
clock.tick(30)
passed_time = time.time() - start
clock.tick(30)
if passed_time > 2 and i < 5:
start = time.time()
i = i + 1
pygame.display.update()
pygame.display.flip()
clock.tick(30)
screen.fill((255, 255, 255))
screen.blit(text4,
(320 - text4.get_width() // 2, 240 - text4.get_height() // 2))
pygame.display.flip()
clock.tick(60)
事件循环中绘制的文本在事件发生时只绘制一次,但它立即被
screen.fill((255, 255, 255))
覆盖。在主循环中绘制的所有内容在每一帧中都是连续绘制的不要在主循环中做任何额外的
pygame.display.flip()
或pygame.display.update()
。添加一个条件a,根据主循环中的条件绘制文本。e、 g.条件可以是
text4 != None
:从代码中删除所有
pygame.display.flip()
和clock.tick()
,除了主循环末尾的代码。但是使用条件和函数来选择性地绘制场景相关问题 更多 >
编程相关推荐