gpt4 book ai didi

python - pygame:当前时间毫秒和增量时间

转载 作者:太空狗 更新时间:2023-10-30 01:58:42 25 4
gpt4 key购买 nike

正如您在下面的代码中看到的,我有一个基本的计时器系统

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)

我的问题是,如何获取当前时间毫秒数以及如何创建增量时间,以便在更新方法中使用它?

最佳答案

来自文档:pygame.time.Clock.get_time将返回前两次调用 Clock.tick 之间的毫秒数。

还有pygame.time.get_ticks这将返回自调用 pygame.init() 以来的毫秒数。

增量时间就是自上一帧以来耗时量,例如:

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

关于python - pygame:当前时间毫秒和增量时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com