pygame:当前毫秒时间和增量时间
在下面的代码中,你可以看到我有一个基本的计时器系统。
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)
我想问的是,怎么才能获取当前的时间(以毫秒为单位),还有怎么创建一个“增量时间”,这样我就可以在更新方法中使用它?
2 个回答
15
ms = clock.tick(30)
这个函数会返回自上次调用以来经过的毫秒数。
13
根据文档:pygame.time.Clock.get_time
这个函数会返回上一次和前一次调用 Clock.tick
之间经过的毫秒数。
还有一个函数 pygame.time.get_ticks
,它会返回自从调用 pygame.init()
以来经过的毫秒数。
所谓的“增量时间”(Delta-time),就是自上一个帧(画面)以来经过的时间,简单来说就是:
t = pygame.time.get_ticks()
# deltaTime in seconds.
deltaTime = (t - getTicksLastFrame) / 1000.0
getTicksLastFrame = t