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

标签 python python-3.x 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)

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

最佳答案

来自文档: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/

相关文章:

python - 如何使用 PySpark 在选择性列中插入数据?

带有多个引号的参数的 Python 子进程调用

node.js - 如何使用 Node.js Net 发送 SMTP 消息(不使用 Nodemailer)

python - 使用PyGame音频时发生意外错误

python - 如何在 Debian jessie 上安装适用于 Python 3 的 pygame?

python - 如果命令是列表且包含 '&&' 或 '||',则 Popen 失败

python - 值错误: x and y must be the same size

python - 我将如何分发 python 代码?

python - 在 Python 中组织可选导入

python - cx_Freeze - 从桌面快捷方式运行 .exe 时出错