我目前正在使用python中的sprite sheet工具将组织导出到xml文档中,但是我在尝试设置预览动画时遇到了一些问题。我不太确定如何用python计算帧速率。例如,假设我拥有所有适当的帧数据和绘图功能,我将如何编写时序以每秒30帧(或任何其他任意速率)显示它。
答案 0 :(得分:8)
最简单的方法是使用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
答案 1 :(得分:0)
Timer
模块中有一个threading
类。为某些目的使用time.sleep
可能更方便。
>>> from threading import Timer
>>> def hello(who):
... print 'hello %s' % who
...
>>> t = Timer(5.0, hello, args=('world',))
>>> t.start() # and five seconds later...
hello world
答案 2 :(得分:0)
你可以使用select吗?它通常用于等待I / O完成,但请看一下签名:
select.select(rlist, wlist, xlist[, timeout])
所以,你可以这样做:
timeout = 30.0
while true:
if select.select([], [], [], timeout):
#timout reached
# maybe you should recalculate your timeout ?