Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame: current time millis and delta time

As you can see in the below code I have a basic timer system

done = False
clock = pygame.time.Clock()

# Create an instance of the Game class
game = space_world.SpaceWorld()

# Main game loop
while not done:
    # Process events (keystrokes, mouse clicks, etc)
    done = game.process_events()

    # Update object positions, check for collisions...
    game.update()

    # Render the current frame
    game.render(screen)

    # Pause for the next frame
    clock.tick(30)

My question is, how can I get the current time milli seconds and how do I create delta time, so I can use it in the update method ?

like image 618
Xerath Avatar asked Jun 04 '14 14:06

Xerath


People also ask

What does Pygame time clock () do?

pygame.time.Clock It will compute how many milliseconds have passed since the previous call. If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second.

Does time sleep work in Pygame?

sleep() . The reason for not using time. sleep is because it will block pygame's event loop and so pygame will not be able to process other events.

How is Delta time calculated?

It is done by calling a timer every frame per second that holds the time between now and last call in milliseconds. Thereafter the resulting number (delta time) is used to calculate how far, for instance, a video game character would have travelled during that time.


1 Answers

ms = clock.tick(30)

The function returns milliseconds since the previous call.

like image 115
otus Avatar answered Sep 28 '22 17:09

otus