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 ?
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.
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.
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.
ms = clock.tick(30)
The function returns milliseconds since the previous call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With