pygame: current time millis and delta time

14,777

Solution 1

From the documentation: pygame.time.Clock.get_time will return the number of milliseconds between the previous two calls to Clock.tick.

There is also pygame.time.get_ticks which will return the number of milliseconds since pygame.init() was called.

Delta-time is simply the amount of time that passed since the last frame, something like:

t = pygame.time.get_ticks()
# deltaTime in seconds.
deltaTime = (t - getTicksLastFrame) / 1000.0
getTicksLastFrame = t

Solution 2

ms = clock.tick(30)

The function returns milliseconds since the previous call.

Share:
14,777
Xerath
Author by

Xerath

Updated on June 08, 2022

Comments

  • Xerath
    Xerath almost 2 years

    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 ?