Pygameではイベントキューに送られた全てのイベントメッセージを処理することができます。このモジュールの命令を使用することでイベントキューの操作を行えます。イベントキューへイベントメッセージを送る場合には、displayモジュールの影響を大きく受けます。displayモジュールが初期化されずにビデオモードが設定されていないと、イベントキューは一切動作しません。
pygameで使用されるキューはイベントオブジェクトを設定するごく普通のキューなので、順番待ちに設定されているイベントを操作することもできます。単にイベントが順番待ちに並んでいるかどうか調べたりする命令から、イベントを順番待ちから取り除く命令まで、様々な方法で操作することができます。
全てのイベントは、固有の識別定数を持っています。イベントの識別定数に使用されている値はNOEVENT定数(0)からNUMEVENTS定数(32)の範囲となります。 USEREVENT定数(24)かそれよりも大きい値を、ユーザー独自のイベント識別定数として使用することもできます。独自のイベント識別定数を使用する場合は、上記の規約に従っているかをよく確認してください。
入力装置の操作状況を調べるには、対応モジュールを使用して入力装置に直接アクセスすればイベントキューを経由するよりも早く情報を取得できます;入力装置とはマウスやキーボード、ジョイスティックのことです。この方法を使う場合は、pygameとシステムウィンドウマネージャーや他のプラットフォーム部分との間で同期が取れていなければいけないので覚えて置いてください。pygameとシステムとの同期を維持するには、pygame.event.pump命令を実行して現在の状態を保つ必要があります。大抵はゲームループ1回ごとに1度の割合でこの命令を実行するとよいでしょう。
イベントキューにはシンプルではありますが振り分け機能が付いています。イベントキューに送られるイベントの中から特定のイベントを除外することで若干パフォーマンスを向上させることができます。pygame.event.set_allowed命令やpygame.event.set_blocked命令を使用してイベントの振り分けを行います。既定では全てのイベントの受け取りが許可されています。
ジョイスティック機器の初期化が行われるまで、Joystickモジュールはイベントを発生させることはありません。
Eventオブジェクトにはイベントの種類や様々な情報などが含まれており、それらは参照はできますが変更はできません。Eventオブジェクトには命令は搭載されておらず、データのみが入っています。Eventオブジェクトは通常イベントキューから取得されますが、 pygame.event.Event命令を使用して独自のEventを作成することもできます。
プログラムを作る際にはイベントキューにイベントが溜まり過ぎてがオーバーフローしないよう注意しなければいけません。イベントをイベントキューから定期的に削除したり取り出したりしないとオーバーフローしてしまいます。イベントキューでオーバーフローが発生すると例外が発生します。
全てのEventオブジェクトでは、Event.typeを使用してイベントの種類を特定する識別番号を取得できます。また Event.dictを使用してEvent内のデータを直接参照することもできます。各データを参照するには、Event.dictに参照したいデータの辞書キーワードを添え字として指定します。
プログラムのデバッグや動作試験を行う際に、Eventオブジェクトが持つイベントの種類や内部データ画面にすばやく表示することができます。pygameの標準イベントでは、イベントの種類によって取得できるデータが変わってきます。下記のリストがイベントの種類とそのイベントで取得できるデータとなります。
QUIT none
ACTIVEEVENT gain, state
KEYDOWN unicode, key, mod
KEYUP key, mod
MOUSEMOTION pos, rel, buttons
MOUSEBUTTONUP pos, button
MOUSEBUTTONDOWN pos, button
JOYAXISMOTION joy, axis, value
JOYBALLMOTION joy, ball, rel
JOYHATMOTION joy, hat, value
JOYBUTTONUP joy, button
JOYBUTTONDOWN joy, button
VIDEORESIZE size, w, h
VIDEOEXPOSE none
USEREVENT code
Eventオブジェクトでは等価比較を使用できます。二つのEventオブジェクト比較してイベントの種類と各内部データが同じ値だった場合、==で比較した結果はTrueとなります。 Inequality checks also work.
Pygame handles all it's event messaging through an event queue. The routines in this module help you manage that event queue. The input queue is heavily dependent on the pygame display module. If the display has not been initialized and a video mode not set, the event queue will not really work.
The queue is a regular queue of Event objects, there are a variety of ways to access the events it contains. From simply checking for the existance of events, to grabbing them directly off the stack.
All events have a type identifier. This event type is in between the values of NOEVENT and NUMEVENTS. All user defined events can have the value of USEREVENT or higher. It is recommended make sure your event id's follow this system.
To get the state of various input devices, you can forego the event queue and access the input devices directly with their appropriate modules; mouse, key, and joystick. If you use this method, remember that pygame requires some form of communication with the system window manager and other parts of the platform. To keep pygame in synch with the system, you will need to call pygame.event.pump - internally process pygame event handlers to keep everything current. You'll want to call this function usually once per game loop.
The event queue offers some simple filtering. This can help performance slightly by blocking certain event types from the queue, use the pygame.event.set_allowed - control which events are allowed on the queue and pygame.event.set_blocked - control which events are allowed on the queue to work with this filtering. All events default to allowed.
Joysticks will not send any events until the device has been initialized.
An Event object contains an event type and a readonly set of member data. The Event object contains no method functions, just member data. Event objects are retrieved from the pygame event queue. You can create your own new events with the pygame.event.Event - create a new event object function.
Your program must take steps to keep the event queue from overflowing. If the program is not clearing or getting all events off the queue at regular intervals, it can overflow. When the queue overflows an exception is thrown.
All Event objects contain an event type identifier in the Event.type member. You may also get full access to the Event's member data through the Event.dict method. All other member lookups will be passed through to the Event's dictionary values.
While debugging and experimenting, you can print the Event objects for a quick display of its type and members. Events that come from the system will have a guaranteed set of member items based on the type. Here is a list of the Event members that are defined with each type.
QUIT none
ACTIVEEVENT gain, state
KEYDOWN unicode, key, mod
KEYUP key, mod
MOUSEMOTION pos, rel, buttons
MOUSEBUTTONUP pos, button
MOUSEBUTTONDOWN pos, button
JOYAXISMOTION joy, axis, value
JOYBALLMOTION joy, ball, rel
JOYHATMOTION joy, hat, value
JOYBUTTONUP joy, button
JOYBUTTONDOWN joy, button
VIDEORESIZE size, w, h
VIDEOEXPOSE none
USEREVENT code
Events support equality comparison. Two events are equal if they are the same type and have identical attribute values. Inequality checks also work.
ゲームをプログラムする際には、1フレーム毎に何らかの方法でイベントキューを呼び出す必要があります。そうすることによって、プログラムを通してrest of the operating systemを操作することができます。特にイベントを制御する命令を実行していない場合は、このpygame.event.pump命令を実行してpygameがイベントを処理できるようにしなければいけません。
pygame.eventの他の命令を使って常にイベントキューに溜まったイベントの処理を行っているのであれば、この命令を使う必要はありません。
イベントキューに関して、必ず対処しておかなければならないことがあります。 ゲームではユーザーの操作によって表示内容が常に移り変わっていくので、システムから送られてくるクリックやキー入力といったイベントをイベントキューから常時受け取って処理をし続けなければ正常に動きません。そのため長時間イベントキューが処理されないような状況になると、システム側の判断によってpygameプログラムが停止させられてしまうことがあります。
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump - internally process pygame event handlers to allow pygame to handle internal actions.
This function is not necessary if your program is consistently processing events on the queue through the other pygame.event functions.
There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.
この命令を実行するとイベントキューから全てのイベント情報を取得し、取得されたイベントはイベントキューから削除されます。引数にイベントの種類が設定されていた場合は、指定した種類のイベント情報のみがイベントキューから取得されます。
特定のイベントのみをイベントキューから取得していると、取得されなかったイベントが溜まって最後にはイベントキューの許容量を超えてしまうので気をつけてください。
This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue.
If you are only taking specific events from the queue, be aware that the queue could eventually fill up with the events you are not interested.
イベントキューからイベントを一つ取得します。イベントキューが空の場合はpygame.NOEVENTイベントが戻り値として帰ります。取得されたイベントはイベントキューから削除されます。
Returns a single event from the queue. If the event queue is empty an event of type pygame.NOEVENT will be returned immediately. The returned event is removed from the queue.
イベントキューからイベントを一つ取得します。イベントキューが空の場合はイベントが発生するまで待機します。取得されたイベントはイベントキューから削除されます。イベント発生を待っている間、プログラムは休止状態となります。休止状態になることでパソコンの動作リソースを占有せず、他のアプリケーションも正常に使用することができるのです。
Returns a single event from the queue. If the queue is empty this function will wait until one is created. The event is removed from the queue once it has been returned. While the program is waiting it will sleep in an idle state. This is important for programs that want to share the system with other applications.
指定した種類のイベントがイベントキュー内にあった場合、trueが戻り値として返ります。複数の種類のイベントをリスト型にまとめて引数に設定すると、どれか一つでもイベントキュー内にあればtrueが戻り値として返されます。
Returns true if there are any events of the given type waiting on the queue. If a sequence of event types is passed, this will return True if any of those events are on the queue.
イベントキュー内にある全てのイベント、もしくは特定の種類のイベントを削除します。これはpygame.event.getと同じような動作ですが、削除したイベントは戻り値として返されません。全てのイベントを削除するだけの用途であれば、こちらの方がやや処理が速いです。
Remove all events or events of a specific type from the queue. This has the same effect as pygame.event.get - get events from the queue except nothing is returned. This can be slightly more effecient when clearing a full event queue.
Pygameでは、あらかじめ定義されているイベント識別定数を使ってイベントの種類を特定します。これが何のイベントなのかをユーザーに伝える場合には、数字ではなく文字にしなければ上手く説明ができません。この命令を使用すると、イベントの種類が定義された定数名を戻り値として返します。The string is in the WordCap style.
Pygame uses integer ids to represent the event types. If you want to report these types to the user they should be converted to strings. This will return a the simple name for an event type. The string is in the WordCap style.
この命令で設定された種類のイベントはイベントキューに送られなくなります。既定では、全てのイベントはイベントキューに送られるように設定されています。同じ種類のイベントに複数回禁止設定を行っても特に問題はありません。
Noneを引数として設定した場合は振り分けの解除設定がされ、全ての種類のイベントがイベントキューに送られるようになります。
The given event types are not allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to disable an event type multiple times.
If None is passed as the argument, this has the opposite effect and ALL of the event types are allowed to be placed on the queue.
この命令で設定された種類のイベントは、イベントキューに送られるようになります。既定では、全てのイベントはイベントキューに送られるように設定されています。同じ種類のイベントに複数回許可設定を行っても特に問題はありません。
Noneを引数として設定した場合、 NONEの値を持つイベントがイベントキューに送れるようになります。
The given event types are allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to enable an event type multiple times.
If None is passed as the argument, NONE of the event types are allowed to be placed on the queue.
指定した種類のイベントがイベントキューの振り分け対象になっていた場合、trueが戻り値として返されます。
Returns true if the given event type is blocked from the queue.
プログラムが動作してウィンドウが開いている時、他のアプリケーションにフォーカスが移るとマウスやキーボードからの入力権もそちらに移ります。イベントのgrab設定をTrueにした場合、あなたのプログラムが入力権を独占してしまいます。
ユーザーが他の作業を行えなくなってしまうので、入力の独占設定はなるべく行わないようにしてください。
When your program runs in a windowed environment, it will share the mouse and keyboard devices with other applications that have focus. If your program sets the event grab to True, it will lock all input into your program.
It is best to not always grab the input, since it prevents the user from doing other things on their system.
プログラムが入力操作を独占している場合、trueが戻り値として返されます。pygame.event.set_grab命令を使用することで入力操作の独占設定を行えます。
Returns true when the input events are grabbed for this application. Use pygame.event.set_grab - control the sharing of input devices with other applications to control this state.
イベントキューの最後尾に作成したイベントを送ります。送られたイベントは後で他のイベントキュー操作関数から取得することができます。
この命令は主に、ユーザーが独自に定義したpygame.USEREVENTイベントをイベントキューに送るために使用されます。それ以外の種類のイベントもこの命令で送ることができますが、キー入力やマウス操作といった通常はシステム側が発生させるイベントを送る場合、作成したイベントの内部データに適切な値をあらかじめ設定しておいてください。
This places a new event at the end of the event queue. These Events will later be retrieved from the other queue functions.
This is usually used for placing pygame.USEREVENT events on the queue. Although any type of event can be placed, if using the sytem event types your program should be sure to create the standard attributes with appropriate values.