Python animation time

I am currently working on a sprite sheet tool in python that exports an organization to an XML document, but I am having trouble trying to animate the preview. I'm not quite sure how to set the frame rate using python. For example, assuming that I have all of my corresponding frame transfer functions and drawing functions, how would I encode time to display it at 30 frames per second (or any other arbitrary speed).

+4
source share
3 answers

The easiest way to do this is 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 this 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 
+8
source

There is a Timer class in the threading module. This may be more convenient than using time.sleep for some purposes.

 >>> 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
source

Can you use select? It is usually used to wait for I / O to complete, but look at the signature:

 select.select(rlist, wlist, xlist[, timeout]) 

So you can do something like:

 timeout = 30.0 while true: if select.select([], [], [], timeout): #timout reached # maybe you should recalculate your timeout ? 
0
source

Source: https://habr.com/ru/post/1307247/


All Articles