使用sleep(5)或time.wait(5000)您无法在此时关闭窗口,因为它不检查事件。在
您可以使用pygame.time.get_ticks()来获取当前时间,并比较是否延迟退出循环。它不会停止循环。在def deathScreen():
# you can render only once
deadText = titleFont.render("You Died!", True, white)
# you can display only once
screen.fill(black)
screen.blit(deadText, (155,200))
pg.display.flip()
# calculate exit time: 5000ms = 5s
current_time = pygame.time.get_ticks()
exit_time = current_time + 5000
# - mainloop -
deathScreen = True
while deathScreen:
for event in pg.event.get():
if event.type == pg.QUIT:
quitGame()
current_time = pygame.time.get_ticks()
if current_time >= exit_time:
deathScreen = False
clock.tick(5)
# - after loop -
screen.fill(black)
pg.display.flip()
同样的方式你可以做其他事情在游戏-即每2秒发射子弹。在
编辑:工作示例
^{pr2}$