私は現在、組織を xml ドキュメントにエクスポートする Python のスプライト シート ツールに取り組んでいますが、プレビューをアニメーション化しようとしていくつかの問題に遭遇しました。Pythonでフレームレートの時間を計る方法がよくわかりません。たとえば、適切なフレーム データと描画関数がすべて揃っていると仮定すると、1 秒あたり 30 フレーム (またはその他の任意のレート) で表示するタイミングをコーディングするにはどうすればよいでしょうか。
5105 次
3 に答える
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)
2 番目に簡単な方法は手動です。
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
于 2010-04-18T03:11:12.760 に答える
0
モジュールにはTimer
クラスがありthreading
ます。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
于 2010-04-18T03:48:24.977 に答える
0
select を使用できますか?I/O の完了を待機するために一般的に使用されますが、署名を見てください。
select.select(rlist, wlist, xlist[, timeout])
したがって、次のようなことができます。
timeout = 30.0
while true:
if select.select([], [], [], timeout):
#timout reached
# maybe you should recalculate your timeout ?
于 2010-04-18T11:26:29.143 に答える