2010-04-18 3 views
4

현재 조직을 XML 문서로 내보내는 Python의 스프라이트 시트 도구에서 작업하고 있지만 미리보기에 애니메이션을 적용하는 데 문제가 있습니다. 나는 파이썬으로 프레임 속도를 측정하는 방법을 잘 모르겠습니다. 예를 들어 내가 적절한 프레임 데이터와 드로잉 기능을 모두 가지고 있다고 가정하면 초당 30 프레임 (또는 다른 임의의 속도)으로 표시하기 위해 타이밍을 코딩하는 방법은 무엇입니까?Python Animation Timing

답변

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

이 매우 도움이, 감사 :

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

그래서, 당신이 뭔가를 할 수 있습니다. 저는 Python을 처음 사용하지만 더 익숙해지기 위해 열심히 노력하고 있습니다. – eriknelson

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 완료를 대기 위해 사용하지만, 서명을 살펴 것 :

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