Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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).

like image 404
eriknelson Avatar asked Feb 27 '23 04:02

eriknelson


1 Answers

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
like image 192
Daniel G Avatar answered Mar 08 '23 00:03

Daniel G