1
\$\begingroup\$

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)))
    pygame.display.update()

    time.sleep(3)
    game_loop()


if x > display_width - car_width or x < 0:
        crash()

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

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ I changed my code to pastie.org/10940776. It's still not working!! \$\endgroup\$
    – Freddy
    Commented Oct 10, 2016 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\$
    – user35344
    Commented Oct 10, 2016 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .