I am trying to print "You Crashed" when car hits wall(or blocks) for 3 seconds and then again start game loop.

I tried using time.sleep(3) but it's not working.

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)))


if x > display_width - car_width or x < 0:

When I run this it blinks "You Crashed" for fraction of second and starts game_loop()

If anyone wants to see full code http://pastie.org/10940745


You don't want to stop executing the game loop while displaying the text. What you want to do is display the text from within your main loop, and count the passed time within the main loop. So, your code should look something like the following (pseudo-code):

# Mainloop:
while True:

    # 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()


    # 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


It's not very often that you should be using time.sleep in your game, as you most likely want to keep the main loop running at all times to keep your game responsive.

  • \$\begingroup\$ I changed my code to pastie.org/10940776. It's still not working!! \$\endgroup\$ – Freddy Oct 10 '16 at 8:34
  • 1
    \$\begingroup\$ That's because you didn't do what I said. Put this INTO your main loop. Also check your timer resolution. \$\endgroup\$ – Tyyppi_77 Oct 10 '16 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.