8

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 ?

Xerath
  • 1,069
  • 5
  • 17
  • 26

2 Answers2

15
ms = clock.tick(30)

The function returns milliseconds since the previous call.

otus
  • 5,572
  • 1
  • 34
  • 48
12

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
kevintodisco
  • 5,061
  • 1
  • 22
  • 28
  • Thank you, but I'm getting the exception. I tried to make my sprite shoot every 0.5s in this code: if pygame.time.get_time - self.last_fire_projectile < 500: return self.last_fire_projectile = pygame.time.get_time But I get this error AttributeError: 'builtin_function_or_method' object has no attribute 'get_time' – Xerath Jun 04 '14 at 14:46
  • @Xerath You need to call `get_time` on your `Clock` object, rather than from the `time` module. – kevintodisco Jun 04 '14 at 15:11