Skip to content Skip to sidebar Skip to footer

Python Animation Timing

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 s

Solution 1:

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 animationwhileTrue:
    # 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...whileTrue:
    # 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.0if sleep_time > 0:
        time.sleep(sleep_time)
    last_time = new_time

Solution 2:

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

>>>from threading import Timer>>>defhello(who):...print'hello %s' % who...>>>t = Timer(5.0, hello, args=('world',))>>>t.start()      # and five seconds later...
hello world

Solution 3:

You could use select ? It's commonly used for waiting on I/O completion, but take a look at the signature:

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

so, you could do something like:

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

Post a Comment for "Python Animation Timing"