我目前正在使用python中的sprite sheet工具将组织导出到xml文档中,但是我在尝试为预览设置动画时遇到了一些问题.我不太确定如何用python计算帧速率.例如,假设我拥有所有适当的帧数据和绘图功能,我将如何编写时序以每秒30帧(或任何其他任意速率)显示它.
解决方法:
最简单的方法是使用Pygame:
import pygame
pygame.init()
clock = pygame.time.Clock()
# or whatever loop you're using for the animation
while True:
# draw animation
# pause so that the animation runs at 30 fps
clock.tick(30)
第二种最简单的方法是手动:
import time
FPS = 30
last_time = time.time()
# whatever the loop is...
while True:
# draw animation
# pause so that the animation runs at 30 fps
new_time = time.time()
# see how many milliseconds we have to sleep for
# then divide by 1000.0 since time.sleep() uses seconds
sleep_time = ((1000.0 / FPS) - (new_time - last_time)) / 1000.0
if sleep_time > 0:
time.sleep(sleep_time)
last_time = new_time