Python动画时序 [英] Python Animation Timing

查看:143
本文介绍了Python动画时序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用python中的Sprite Sheet工具,该工具可将组织导出到xml文档中,但在尝试制作预览动画时遇到了一些问题。我不太确定如何使用python设置帧频。例如,假设我拥有所有适当的帧数据和绘图功能,我该如何编码以每秒30帧(或任何其他任意速率)显示的时间。

I'm currently working on sprite sheet tool in python that exports the organization into an xml document but I've run into some problems trying to animate a preview. I'm not quite sure how to time the frame rate with python. For example, assuming I have all of my appropriate frame data and drawing functions, how would I go about coding the timing to display it at 30 frames per second (or any other arbitrary rate).

推荐答案

最简单的方法是使用 Pygame

The easiest way to do it is with 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)

第二种最简单的方法是手动进行:

The second easiest way to do it is manually:

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

这篇关于Python动画时序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆