当汽车撞墙(或阻挡)3秒钟,然后再次启动游戏循环时,我正在尝试打印“您坠毁”。
我试过使用time.sleep(3)
但它不工作。
def crash(): crash_font = pygame.font.Font('freesansbold.ttf',115) crash_text = crash_font.render("You Crashed!",True,black) game_display.blit(crash_text,((display_width/4 - 100), (display_height/4 + 100))) pygame.display.update() time.sleep(3) game_loop() if x > display_width - car_width or x < 0: crash()
当我运行这个时,它闪烁“你坠毁”秒的一小部分,并开始game_loop()
如果有人想看完整的代码http://pastie.org/10940745
你不想在显示文本时停止执行游戏循环。 你想要做的是显示主循环内的文本,并计算在主循环内传递的时间。 所以,你的代码应该如下所示(伪代码):
# Mainloop: while True: check_events() # Put your own condition here if not display_crash_text and shouldCrash: # Set the flag and get the current time. display_crash_text = True start_time = get_current_time() screen.fill(Black) # If the text should be displayed. if display_crash_text: screen.blit("You Crashed!", ...) # Check if three seconds have passed. This assumes that "get_current_time()" operates on seconds. if get_current_time() - start_time > 3: display_crash_text = False pygame.display.flip()
你不应该在游戏中使用time.sleep
,因为你很可能总是希望保持主循环的运行,以保持游戏的响应。