1

I am beginner and learning about pygame. I am following a tutorial to a simple game and I can not figure out what the problem is. I have a crash function and use a time.sleep(). But the time sleep occurs earlier making whole code kind of useless.

I am working on Mac but I do not think that should be causing this.

I have tried to put time.sleep() into another function and use that one in the crash function but that does not work as well and I am not sure if time.sleep have some kind of preference.

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()  

def crash():
    message_display('You Crashed')

The first two function should not be the problem but I posted them just to be sure. So when the car in game crashes, it is supposed to write big "You Crashed", then wait 2 seconds and restart the game with game_loop() function. But it stops the game, waits 2 seconds, then writes "You Crashed" and immediately restart the game.

Gruny
  • 55
  • 1
  • 4

1 Answers1

1

This happens because, after you update the screen surface with pygame.display.flip() or pygame.display.update(), you'll have to process events (by e.g. calling pygame.event.get) to give the window the chance to redraw itself.

This may work on Windows, because the window management works different there, but it would still be "wrong".

You have to stick to these rules:

  • never ever call time.sleep (unless you know better)
  • never ever call pygame.display.update() or pygame.display.flip() outside of your main loop (unless you know better)
  • never ever have more than one game loop (unless you know better)
  • your game runs in a loop, so to do anything that is based on "time" (like: print this for 2 seconds, do this in 4 seconds, etc), you have to keep track of the time in your game state (probably just a variable)
  • don't call your game loop from your game loop
  • have a basic idea of how your game should work, which states in can be in, and how you move between the states, and what happens in each state.

A simple example, let's say we want to create a little racing game, so we could think of these states:

  • Title screen
  • The actual racing game
  • Game over screen

An easy implementation whould just have a variable state and a big if/else block in the main loop:

if state == 'TITLE_SCREEN':
    ...render title screen...
    ...if the space bar was pressed set state = 'GAME'
elif state == 'GAME':
    ...render the player, obstacles, etc...
    ...if the player crashed, set state = 'GAMEOVER' and keep track of the current time, e.g. with `timer = pygame.Clock().get_ticks()`
elif state == 'GAMEOVER':
    ...render a game over message...
    ...if the space bar was pressed or `pygame.Clock().get_ticks() - timer > 2000` set state = 'GAME'        

Similar problem:
Two display updates at the same time with a pygame.time.wait() function between
pygame.time.wait() makes the window freez
pygame - how to update HP bar slowly without time.sleep()

More about game states:
Pygame level/menu states

Probably also interesting:
Threading issue with Pygame

sloth
  • 99,095
  • 21
  • 171
  • 219