我目前正在研究python中的精灵工作表工具,它将组织导出到一个xml文档中,但我遇到了一些尝试动画预览的问题。我不太确定如何使用python来计算帧频。例如,假设我拥有所有适当的帧数据和绘图功能,我将如何编码时间以每秒30帧(或任何其他任意速率)显示。Python动画计时
4
A
回答
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
0
还有就是threading
模块中的Timer
类。这可能比使用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
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 ?
相关问题
- 1. 计时动画gif显示
- 2. 倒计时应用动画
- 3. 正确计时CSS动画
- 4. 计算动画时间Qt
- 5. Python中的实时动画
- 6. 流动动画与计时(OpenGL)
- 7. 核心动画 - 组中的动画单独计时
- 8. Python动画图
- 9. IPad Safari CSS3动画计时错误
- 10. MatPlotLib中的计时/艺术家动画
- 11. 针对非CSS的jQuery动画计时
- 12. 使用jQuery进行计时动画
- 13. 为倒计时脚本设置动画
- 14. 重置倒计时动画c#WPF
- 15. 刷新UIScrollView /动画,计时(Objective-C)
- 16. 使用时间线设计动画
- 17. 角路由器动画计时错误
- 18. 用python去动画动画gif
- 19. IE11萨斯计算C动画计时不工作
- 20. 用动画计算数字
- 21. 累计动画与CSS
- 22. 动画actionBar材质设计
- 23. Android动画 - 选择动画时
- 24. 在动画视图时动画UIButton
- 25. Spyder Python动画不工作
- 26. 在python中动画Colormesh图
- 27. Python tkinter画布动态Create.Line
- 28. Python Kivy小部件动画
- 29. Python中的2D动画
- 30. Python动画阴影matplotlib
谢谢你,非常有帮助。我是Python的新手,但努力工作以更熟悉它。 – eriknelson 2010-04-18 03:21:57