メニュー

pygame.time

時間を監視するためのpygameモジュールです。

pygameで使用される時間はミリ秒(1/1000 秒)単位で表されます。大抵の使用環境では、最小の時間単位は約10ミリ秒程度に制限されます。ミリ秒単位の細かい時間処理を実現するためには、 TIMER_RESOLUTION定数を使用します。

pygame module for monitoring time

Times in pygame are represented in milliseconds (1/1000 seconds). Most platforms have a limited time resolution of around 10 milliseconds. This resolution, in milliseconds, is given in the TIMER_RESOLUTION constant.


pygame.time.get_ticks

ミリ秒単位で時間を取得します。
pygame.time.get_ticks(): return milliseconds

pygame.init命令でpygameを初期化してからこれまでに経過した時間をミリ秒単位の戻り値として返します。pygameが初期化される前の状態では、この命令は常に0を戻り値として返します。

get the time in milliseconds.
pygame.time.get_ticks(): return milliseconds

Return the number of millisconds since pygame.init - initialize all imported pygame modules was called. Before pygame is initialized this will always be 0.


pygame.time.wait

一定時間プログラムを停止します。
pygame.time.wait(milliseconds): return time

指定した時間(ミリ秒単位)の間プログラムを停止します。この命令によってプログラムが停止されると、他のプログラムでパソコンの処理機能を使用できるようになります。この命令で設定する停止時間が小さすぎる場合は、わずかな時間しかパソコンの処理機能が開放されません。細かい時間を設定する場合はpygame.time.delay命令を使用したほうが、少しではありますが正確に時間を停止できます。

実際に停止した時間が戻り値として取得されます。

pause the program for an amount of time
pygame.time.wait(milliseconds): return time

Will pause for a given number of milliseconds. This function sleeps the process to share the processor with other programs. A program that waits for even a few milliseconds will consume very little processor time. It is slightly less accurate than the pygame.time.delay - pause the program for an amount of time function.

This returns the actual number of milliseconds used.


pygame.time.delay

一定時間プログラムを停止します。
pygame.time.delay(milliseconds): return time

指定した時間(ミリ秒単位)の間プログラムを停止します。この命令ではpygame.time.wait命令よりも正確に遅延を発生させるため、パソコンの処理機能を占有したままの状態になります。(開放はしません)

実際に停止した時間が戻り値として取得されます。

pause the program for an amount of time
pygame.time.delay(milliseconds): return time

Will pause for a given number of milliseconds. This function will use the processor (rather than sleeping) in order to make the delay more accurate than pygame.time.wait - pause the program for an amount of time.

This returns the actual number of milliseconds used.


pygame.time.set_timer

一定の間隔で、イベントキューにイベントを繰り返し発生させます。
pygame.time.set_timer(eventid, milliseconds): return None

イベントキューに発生させるイベントタイプと、そのイベントが何ミリ秒ごとに発生させるかを設定します。指定した時間が経過するまで最初のイベントは発生しません。

全てのイベントタイプにそれぞれ個別の発生間隔を設定することができます。既にpygame内部のイベント定数として使用されている値もあるので注意してください。pygame.USEREVENT定数(24)とpygame.NUMEVENTS定数(32)の間が空いているので、25以上31以下の値をeventid引数に設定するようにしましょう。

イベントを発生させないようにするためには、milliseconds引数に0を設定します。

repeatedly create an event on the event queue
pygame.time.set_timer(eventid, milliseconds): return None

Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.

Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.

To disable the timer for an event, set the milliseconds argument to 0.


pygame.time.Clock

プログラム内の時間管理に役立つオブジェクトを作成します。
pygame.time.Clock(): return Clock

プログラム内の時間管理に役立つClockオブジェクトを新規に作成します。このClockオブジェクトを使用すれば、ゲームのフレームレート制御など様々な処理を行うことができます。

create an object to help track time
pygame.time.Clock(): return Clock

Creates a new Clock object that can be used to track an amount of time. The clock also provides several functions to help control a game's framerate.


Clock.tick

clockオブジェクトを更新します。
Clock.tick(framerate=0): return milliseconds

この命令はフレームごとに実行する必要があります。前回この命令が実行されて何ミリ秒が経過したかを計算して戻り値として返します。

オプションとして設定できるframerate引数を設定していた場合、ゲームの動作を設定したフレーム秒以下に保つために遅延を発生させます。これはゲームの実行速度に制限をかけたい場合に役に立ちます。フレームごとに一回Clock.tick(40)を実行すると、プログラムは一秒毎に40フレームを超える速度で実行されることはありません。

この命令内部で使用されているSDL_Delay命令は全てのパソコン環境で正確な時間動作をする訳ではありませんが、それほどCPUを消費しません。tick_busy_loop命令の方が正確に時間を測れるので、CPUを大量に消費してしまうのが気にならないのであればそちらを使用したほうがよいでしょう。

update the clock
Clock.tick(framerate=0): return millisecondscontrol timer events

This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.

If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second.

Note that this function uses SDL_Delay function which is not accurate on every platform, but does not use much cpu. Use tick_busy_loop if you want an accurate timer, and don't mind chewing cpu.


Clock.tick_busy_loop

clockオブジェクトを更新します。
Clock.tick_busy_loop(framerate=0): return milliseconds

この命令はフレームごとに実行する必要があります。前回この命令が実行されて何ミリ秒が経過したかを計算して戻り値として返します。

オプションとして設定できるframerate引数を設定していた場合、ゲームの動作を設定したフレーム/秒以下に保つために遅延を発生させます。これはゲームの実行速度に制限をかけたい場合に役に立ちます。フレームごとに一回Clock.tick(40)を実行すると、プログラムは一秒毎に40フレームを超える速度で実行されることはありません。

この命令内部で使用されているpygame.time.delay命令はCPUを大量に消費し、より正確に時間を計測します。

この命令はpygameのバージョン 1.8.0で新しく実装されました。

update the clock
Clock.tick_busy_loop(framerate=0): return millisecondscontrol timer events

This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.

If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second.

Note that this function uses pygame.time.delay, which uses lots of cpu in a busy loop to make sure that timing is more acurate.

New in pygame 1.8.0.


Clock.get_time

前回tick命令実行時の間隔時間を取得します。
Clock.get_time(): return milliseconds

最後にClock.tick命令が実行されてから経過した時間を戻り値として返します。この戻り値は、直前に実行されたtick()と更にその前に実行されたtick()との間の経過時間をミリ秒単位で表した値となります。

※原文では「最後に実行したClock.tickに引数として渡した値を戻り値として取得する(Returns the parameter passed to the last call to Clock.tick)」と記載されています。Clock.tickで設定するのはフレーム/秒、この命令で取得されるのはミリ秒単位の経過時間であり、同じ経過時間でも単位が異なっています。そのため該当部分は「ミリ秒単位の経過時間」に変えて訳し直しています。

time used in the previous tick
Clock.get_time(): return milliseconds

Returns the parameter passed to the last call to Clock.tick - update the clock. It is the number of milliseconds passed between the previous two calls to Pygame.tick().


Clock.get_rawtime

前回tick命令が実行時の実際の間隔時間を取得します。
Clock.get_rawtime(): return milliseconds

Clock.get_time命令と同じような機能ですが、こちらはClock.tick命令でフレームレートの制限をかけていた場合の遅延時間は換算されません。

actual time used in the previous tick
Clock.get_rawtime(): return milliseconds

Similar to Clock.get_time - time used in the previous tick, but this does not include any time used while Clock.tick - update the clock was delaying to limit the framerate.


Clock.get_fps

フレームレートの間隔を計算します。
Clock.get_fps(): return float

ゲームで使用するフレームレート(一秒間に何回画面が更新されるか)を計算します。直近に実行されたClock.tick命令のいくつかの平均を取ってフレームレートを計算します。

compute the clock framerate
Clock.get_fps(): return float

Compute your game's framerate (in frames per second). It is computed by averaging the last few calls to Clock.tick - update the clock.