メニュー

pygame.sprite

基本的なゲームオブジェクトクラスのpygameモジュール

このモジュールにはゲームで使えるシンプルなクラスがいくつか含まれています。メインとなるSpriteクラスと、Spriteクラスを含んだ様々なGroupクラスがあります。Pygameのゲーム開発でこれらのクラスを使うかどうかは、あなたの自由です。これらのクラスは非常にコンパクトな作りとなっており、基本的な機能しか持っていませんが、大抵のゲームに流用できるようなプログラムコードとなっています。

ゲーム開発では、Spriteクラスは他のクラスの基底クラスとして使われるのを目的として作られています。単にspriteを格納するためのGroupという基底クラスも用意されてます。 ゲーム開発では、Spriteクラスを内部に持って特別にカスタマイズした、様々な種類のGroupクラスを作ることができます。

この基底Spriteクラスは、内部に格納した情報をSurfaceへ描写する機能を持っています。Group.draw命令を実行するには、格納している各SpriteオブジェクトにSurface.image属性と Surface.rect属性が設定されている必要があります。Group.clear 命令にも上記属性が設定されている必要があり、この命令を使うと背景を上書きして全てのSpriteの画像をクリアすることができます。他にも更なる拡張を加えたpygame.sprite.RenderUpdatesクラス と pygame.sprite.OrderedUpdatesクラスもあります。

最後に、このモジュールには様々は当たり判定機能があります。この機能を使うと、複数のgroupに格納されている各spriteが衝突していないか、rect範囲を基にして調べることができます。当たり判定を行うためには、SpriteオブジェクトにSurface.rect属性を設定しておく必要があります。

Groupクラスは、内部に保持するSpriteオブジェクトを削除したり追加したりして、より高度な処理を行えるように設計されています。Groupクラスを使うことで、対象のSpriteオブジェクトが既に内部に存在しているかどうかを簡単に調べることができます。Spriteオブジェクトは、複数のGroupクラスに追加することができます。 ゲーム開発では、これらのGroupオブジェクトを使って対象物の描写処理を行ったり、プレイヤーの操作とそれによって生じる内部処理をgroupごとに分けたりできます。Spriteクラスを継承する場合は新しい属性を定義せずに、同じgroupに所属する他のSpriteと同じ構造にするようにしてください。そうすることで、ゲーム中でのsprite走査処理が簡単になります。

add()命令やremove()命令を使って、GroupクラスにSpriteクラスを追加したり削除したりできます。これらの命令では一つもしくは複数のクラスをまとめて処理することができます。処理できる数はクラスの構造によります。また、これらのクラスで既定のコンストラクタを実行する際にも一つもしくは複数のクラスを引数として設定することもできます。設定できる数はコンストラクタの構造によります。Groupクラスに対して、同じSpriteクラスを繰り返し追加・削除しても特に問題はありません。

SpriteクラスやGroupクラスを継承せず新規にクラスを作成することも可能ですが、作ったクラスをSpriteやGroupに追加するのであれば、既存のクラスを継承することを強く勧めます。

Spriteクラスにはスレッドセーフ機能はありません。そのため、マルチスレッド環境で使う場合はSpriteクラスのロックを必ず行ってください。

pygame module with basic game object classes

This module contains several simple classes to be used within games. There is the main Sprite class and several Group classes that contain Sprites. The use of these classes is entirely optional when using Pygame. The classes are fairly lightweight and only provide a starting place for the code that is common to most games.

The Sprite class is intended to be used as a base class for the different types of objects in the game. There is also a base Group class that simply stores sprites. A game could create new types of Group classes that operate on specially customized Sprite instances they contain.

The basic Sprite class can draw the Sprites it contains to a Surface. TheGroup.draw - blit the Sprite images method requires that each Sprite have a Surface.image attribute and a Surface.rect. The Group.clear - draw a background over the Sprites method requires these same attributes, and can be used to erase all the Sprites with background. There are also more advanced Groups: pygame.sprite.RenderUpdates - Group class that tracks dirty updates and pygame.sprite.OrderedUpdates - RenderUpdates class that draws Sprites in order of addition.

Lastly, this module contains several collision functions. These help find sprites inside multiple groups that have intersecting bounding rectangles. To find the collisions, the Sprites are required to have a Surface.rect attribute assigned.

The groups are designed for high efficiency in removing and adding Sprites to them. They also allow cheap testing to see if a Sprite already exists in a Group. A given Sprite can exist in any number of groups. A game could use some groups to control object rendering, and a completely separate set of groups to control interaction or player movement. Instead of adding type attributes or bools to a derived Sprite class, consider keeping the Sprites inside organized Groups. This will allow for easier lookup later in the game.

Sprites and Groups manage their relationships with the add() and remove() methods. These methods can accept a single or multiple targets for membership. The default initializers for these classes also takes a single or list of targets for initial membership. It is safe to repeatedly add and remove the same Sprite from a Group.

While it is possible to design sprite and group classes that don't derive from the Sprite and AbstractGroup classes below, it is strongly recommended that you extend those when you add a Sprite or Group class.

Sprites are not thread safe. So lock them yourself if using threads.


pygame.sprite.Sprite

特定の画像をゲーム画面に表示するためのシンプルな基底クラス
pygame.sprite.Sprite(*groups): return Sprite
  • Sprite.update - spriteクラスで行う処理を制御する命令です。
  • Sprite.add - GroupにSpriteを追加します。
  • Sprite.remove - GroupからSpriteを削除します。
  • Sprite.kill - 全てのGroupからSpriteを削除します。
  • Sprite.alive - spriteが1つ以上のgroupに属しているか調べます。
  • Sprite.groups - このSpriteが属しているGroupの一覧

特定の画像をゲーム画面に表示するためのシンプルな基底クラスです。このクラスを継承する場合は、 Sprite.update命令をオーバーライドして、 Sprite.image属性とSprite.rect属性を設定してください。コンストラクタ実行時には望む数のGroupインスタンスを引数として設定することができ、所属Groupとして追加されます。

このSpriteクラスを継承して使う場合は、Groupへ追加する前に必ず基底Spriteクラスの初期化処理を実行するようにしてください。

simple base class for visible game objects
pygame.sprite.Sprite(*groups): return Sprite

The base class for visible game objects. Derived classes will want to override the Sprite.update - method to control sprite behavior and assign a Sprite.image and Sprite.rect attributes. The initializer can accept any number of Group instances to be added to.

When subclassing the Sprite, be sure to call the base initializer before adding the Sprite to Groups.


Sprite.update

spriteクラスで行う処理を制御する命令です。
Sprite.update(*args):

この命令は本来は何の処理も行いません。;自分で好きなように処理を定義できる、便利な"フック"なのです。引数に何を設定したかに関わらず、この命令はGroup.update命令実行時に自動的に呼び出されます。

所属するGroupでupdate()命令を使わないのであれば、この命令を使う必要はありません。

method to control sprite behavior
Sprite.update(*args):

The default implementation of this method does nothing; it's just a convenient "hook" that you can override. This method is called by Group.update - call the update method on contained Sprites with whatever arguments you give it.

There is no need to use this method if not using the convenience method by the same name in the Group class.


Sprite.add

GroupにSpriteを追加します。
Sprite.add(*groups): return None

任意の数のGroupオブジェクトを引数として設定することができます。SpriteオブジェクトがそのGroupにまだ属していない場合に、SpriteオブジェクトはそのGroupに追加されます。

add the sprite to groups
Sprite.add(*groups): return None

Any number of Group instances can be passed as arguments. The Sprite will be added to the Groups it is not already a member of.


Sprite.remove

GroupからSpriteを削除します。
Sprite.remove(*groups): return None

任意の数のGroupオブジェクトを引数として設定することができます。SpriteオブジェクトがそのGroupに属している場合に、SpriteオブジェクトはそのGroupから削除されます。

remove the sprite from groups
Sprite.remove(*groups): return None

Any number of Group instances can be passed as arguments. The Sprite will be removed from the Groups it is currently a member of.


Sprite.kill

全てのGroupからSpriteを削除します。
Sprite.kill(): return None

所属する全てのGroupからSpriteを削除します。所属情報以外のSpriteの情報については、一切変更されません。この命令を実行した後もSpriteオブジェクトは使い続けることはできますし、再度Groupに所属させることもできます。

remove the Sprite from all Groups
Sprite.kill(): return None

The Sprite is removed from all the Groups that contain it. This won't change anything about the state of the Sprite. It is possible to continue to use the Sprite after this method has been called, including adding it to Groups.


Sprite.alive

spriteが1つ以上のgroupに属しているか調べます。
Sprite.alive(): return bool

Spriteが1つ以上のgroupに属している場合はTrueが戻り値として返ります。

does the sprite belong to any groups
Sprite.alive(): return bool

Returns True when the Sprite belongs to one or more Groups.


Sprite.groups

このSpriteが属しているGroupの一覧
Sprite.groups(): return group_list

このSpriteが所属している全てのGroup一覧を戻り値として返します。

list of Groups that contain this Sprite
Sprite.groups(): return group_list

Return a list of all the Groups that contain this Sprite.


pygame.sprite.DirtySprite

属性を増やして機能を追加した、Spriteのサブクラス。
pygame.sprite.DirtySprite(*groups): return DirtySprite

DirtySpriteオブジェクトに追加された属性には、既定値が設定されています。:

dirty = 1

    1が設定されている場合は、画面描写後に再び0が設定されます。
    2が設定されている場合は、描写画像の更新が必要です。( 画面描写後もこのフラグは0にリセットされません)
    0が設定されている場合は、描写画像画像の更新が必要ないという意味なので、画面描写は行われません。

blendmode = 0

    これはコピー描写時のブレンドモードの設定に使われるspecial_flags 引数です

source_rect = None

    source_rectは、self.imageを画面に表示する描写位置と関わってくるので
    覚えておいてください。

visible = 1

    通常は1が設定されており、0を設定した場合は画面に描写されません。
    (you must set it dirty too to be erased from screen)

layer = 0

    (参照のみに使う値です。このSpriteクラスがLayeredRenderGroupに追加された場合に使われます。
    詳しくはLayeredRenderGroupの項目を参照してください)
a more featureful subclass of Sprite with more attributes
pygame.sprite.DirtySprite(*groups): return DirtySprite

Extra DirtySprite attributes with their default values:

dirty = 1

    if set to 1, it is repainted and then set to 0 again
    if set to 2 then it is always dirty ( repainted each frame,
    flag is not reset)
    0 means that it is not dirty and therefor not repainted again

blendmode = 0

    its the special_flags argument of blit, blendmodes

source_rect = None

    source rect to use, remember that it is relative to
    topleft (0,0) of self.image

visible = 1

    normally 1, if set to 0 it will not be repainted
    (you must set it dirty too to be erased from screen)

layer = 0

    (READONLY value, it is read when adding it to the
    LayeredRenderGroup, for details see doc of LayeredRenderGroup)

pygame.sprite.Group

複数のSpriteを保持するクラス
pygame.sprite.Group(*sprites): return Group
  • Group.sprites - このGroupが保持するSpriteの一覧
  • Group.copy - Groupを複製する
  • Group.add - このGroupにSpriteを追加する
  • Group.remove - GroupからSpriteを削除します。
  • Group.has - 指定したSpriteがGroupに格納されているか調べます。
  • Group.update - 格納している各Spriteオブジェクトのupdate命令を実行します。
  • Group.draw - Spriteの持つ画像をコピー描写します
  • Group.clear - Spriteの画像に背景を上書きします。
  • Group.empty - 全てのSpriteを削除します

Spriteオブジェクトを格納するためのシンプルなクラスです。このクラスを継承して、格納するSpriteオブジェクトに対して具体的な処理を行うクラスを作成することができます。コンストラクタ実行時には、望む数のSpriteを引数に設定してこの Groupに追加できます。 Groupクラスでは、下記のPython標準操作をサポートしています。:

    in      指定したSpriteオブジェクトを格納しているか調べます。
    len     格納しているSpriteオブジェクトの数
    bool    Spriteオブジェクトを1つ以上格納しているか調べます。
    iter    格納されている全てのSpriteオブジェクトを逐次参照します。

Groupクラスに格納されたSpriteオブジェクトは順番どおりに並んでいません。そのため、Spriteオブジェクトを画面描写したり逐次参照したりする順番は特に決まっていません。

container class for many Sprites
pygame.sprite.Group(*sprites): return Group

A simple container for Sprite objects. This class can be inherited to create containers with more specific behaviors. The constructor takes any number of Sprite arguments to add to the Group. The group supports the following standard Python operations:

    in      test if a Sprite is contained
    len     the number of Sprites contained
    bool test if any Sprites are contained
    iter    iterate through all the Sprites

The Sprites in the Group are not ordered, so drawing and iterating the Sprites is in no particular order.


Group.sprites

このGroupが保持するSpriteの一覧
Group.sprites(): return sprite_list

このgroupが格納している全てのSpriteオブジェクトの一覧を戻り値として返します。iter命令を使った逐次参照を行うこともできますが、groupの編集を行っている間は逐次参照はできません。

list of the Sprites this Group contains
Group.sprites(): return sprite_list

Return a list of all the Sprites this group contains. You can also get an iterator from the group, but you cannot iterator over a Group while modifying it.


Group.copy

Groupを複製する
Group.copy(): return Group

このGroupが持つSpriteオブジェクトと、全て同じものを持つGroupオブジェクトを新規に作成します。 If you have subclassed Group, the new object will have the same (sub-)class as the original. This only works if the derived class's constructor takes the same arguments as the Group class's.

duplicate the Group
Group.copy(): return Group

Creates a new Group with all the same Sprites as the original. If you have subclassed Group, the new object will have the same (sub-)class as the original. This only works if the derived class's constructor takes the same arguments as the Group class's.


Group.add

このGroupにSpriteを追加する
Group.add(*sprites): return None

このGroupに任意の数のSpriteオブジェクトを追加します。まだGroupに格納されていないSpriteのみを追加できます。

引数には、Spriteオブジェクトのインテレーターを設定することもできます。

add Sprites to this Group
Group.add(*sprites): return None

Add any number of Sprites to this Group. This will only add Sprites that are not already members of the Group.

Each sprite argument can also be a iterator containing Sprites.


Group.remove

GroupからSpriteを削除します。
Group.remove(*sprites): return None

このGroupから任意の数のSpriteを削除します。既にGroupに格納されているSpriteのみを削除できます。

引数には、Spriteオブジェクトのインテレーターを設定することもできます。

remove Sprites from the Group
Group.remove(*sprites): return None

Remove any number of Sprites from the Group. This will only remove Sprites that are already members of the Group.

Each sprite argument can also be a iterator containing Sprites.


Group.has

指定したSpriteがGroupに格納されているか調べます。
Group.has(*sprites): return None

引数に設定したspriteをGroupが全て格納していた場合、Trueが戻り値として返ります。これを使うと、"in"演算子を使ってGroupに格納されたSpriteを一つ一つ調べる時と同じ処理を行えます。("if sprite in group: ...")

引数には、Spriteオブジェクトのインテレーターを設定することもできます。

test if a Group contains Sprites
Group.has(*sprites): return None

Return True if the Group contains all of the given sprites. This is similar to using the "in" operator on the Group ("if sprite in group: ..."), which tests if a single Sprite belongs to a Group.

Each sprite argument can also be a iterator containing Sprites.


Group.update

格納している各Spriteオブジェクトのupdate命令を実行します。
Group.update(*args): return None

この Groupが格納している全てのSpriteのupdate()命令を実行します。基底Spriteクラスのupdate()命令には引数を設定できますが、特に何か処理をすることはありません。Group.updateに設定した引数は、格納するSpriteのupdate命令へそのまま渡されます。

Sprite.update命令から戻り値を取得する、といった使い方はできません。

call the update method on contained Sprites
Group.update(*args): return None

Calls the update() method on all Sprites in the Group. The base Sprite class has an update method that takes any number of arguments and does nothing. The arguments passed to Group.update - call the update method on contained Sprites will be passed to each Sprite.

There is no way to get the return value from the Sprite.update - method to control sprite behavior methods.


Group.draw

Spriteの持つ画像をコピー描写します
Group.draw(Surface): return None

格納する Sprite の画像を、引数として設定した Surface へ描写します。Sprite.image属性が描写する画像として使われ、Sprite.rect属性が画像の描写位置として使われます。

この Group はspriteを順番通りに管理しているわけではないので、描写はランダムな順番で行われます。

blit the Sprite images
Group.draw(Surface): return None

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.

The Group does not keep sprites in any order, so the draw order is arbitrary.


Group.clear

Spriteの画像に背景を上書きします。
Group.clear(Surface_dest, background): return None

直近のGroup.draw命令で描写されたSpriteの画像を消去します。Spriteの画像の表示された位置を背景画像で上書きして塗り潰すことで、 描写先の Surface をクリアします。

ここで言う背景画像とは、普通は描写先Surfaceの該当位置に表示されている画像のことを指します。ですが、引数を二つ持つコールバック関数を独自に作成することもできます。;二つの引数とは、描写先Surfaceとその上書き範囲のことです。コールバック関数は画像クリアの度に複数回実行されます。

下記がSpriteの画像を赤一色で塗りつぶすコールバック関数の例です。:

    def clear_callback(surf, rect):
        color = 255, 0, 0
        surf.fill(color, rect)
draw a background over the Sprites
Group.clear(Surface_dest, background): return None

Erases the Sprites used in the last Group.draw - blit the Sprite images call. The destination Surface is cleared by filling the drawn Sprite positions with the background.

The background is usually a Surface image the same dimensions as the destination Surface. However, it can also be a callback function that takes two arguments; the destination Surface and an area to clear. The background callback function will be called several times each clear.

Here is an example callback that will clear the Sprites with solid red:

    def clear_callback(surf, rect):
        color = 255, 0, 0
        surf.fill(color, rect)

Group.empty

全てのSpriteを削除します
Group.empty(): return None
このGroupから全てのSpriteを削除します。
remove all Sprites
Group.empty(): return None

Removes all Sprites from this Group.


pygame.sprite.RenderUpdates

描写先範囲の情報を取得するGroupクラス
pygame.sprite.RenderUpdates(*sprites): return RenderUpdates
  • RenderUpdates.draw - Spriteの画像のコピー描写を行い、描写先となった範囲の情報を取得します。

これはpygame.sprite.Groupの派生クラスです。draw()命令が拡張されており、画面上の描写先となった範囲の情報を取得できます。

Group class that tracks dirty updates
pygame.sprite.RenderUpdates(*sprites): return RenderUpdates

This class is derived from pygame.sprite.Group - container class for many Sprites. It has an extended draw() method that tracks the changed areas of the screen.


RenderUpdates.draw

Spriteの画像のコピー描写を行い、描写先となった範囲の情報を取得します。
RenderUpdates.draw(surface): return Rect_list

格納している全てのSpriteの画像を指定surface上に描写します。Group.drawと同じ動きですが、この命令では画面上で描写が行われた範囲の一覧を戻り値として返します。Group.clear命令でクリアした範囲も描写先範囲に含まれるので、戻り値として取得されます。

戻り値として得られた Rect 値はpygame.display.update命令実行時に引数として設定するとよいでしょう。そうすることで、ソフトウェア駆動モードでのパフォーマンスが向上します。こうした画面更新の速度を向上させる手法は、描写先の背景が静止画の場合にのみ有効です。

blit the Sprite images and track changed areas
RenderUpdates.draw(surface): return Rect_list

Draws all the Sprites to the surface, the same as Group.draw - blit the Sprite images. This method also returns a list of Rectangular areas on the screen that have been changed. The returned changes include areas of the screen that have been affected by previous Group.clear - draw a background over the Sprites calls.

The returned Rect list should be passed to pygame.display.update - update portions of the screen for software displays. This will help performance on software driven display modes. This type of updating is usually only helpful on destinations with non-animating backgrounds.


pygame.sprite.OrderedUpdates

追加された順番に従ってSpriteの画面描写を行うRenderUpdatesクラス
pygame.sprite.OrderedUpdates(*spites): return OrderedUpdates

これはpygame.sprite.RenderUpdatesの派生クラスです。追加された順番に従ってSpriteの画面描写を行います。通常のGroupに比べて、Spriteの追加や削除処理がやや遅くなります。

RenderUpdates class that draws Sprites in order of addition
pygame.sprite.OrderedUpdates(*spites): return OrderedUpdates

This class derives from pygame.sprite.RenderUpdates - Group class that tracks dirty updates. It maintains the order in which the Sprites were added to the Group for rendering. This makes adding and removing Sprites from the Group a little slower than regular Groups.


pygame.sprite.LayeredUpdates

レイヤーを制御し、OrderedUpdatesのように画像描写処理を行うLayeredUpdates Groupクラス
pygame.sprite.LayeredUpdates(*spites, **kwargs): return LayeredUpdates

この group にもpygame.sprite.Spriteオブジェクトの追加・削除をすることができます。

kwargs 引数にキーワード 'default_layer'とレイヤー値を設定することで、既定となるレイヤー値を設定することができます。既定となるレイヤー値は0が設定されています。

格納したsprite にレイヤー属性が設定されていた場合は、そのレイヤー属性が使用されます。**kwarg引数の中に'layer'の文字が含まれていた場合は、引数に設定したspriteにはそのレイヤーが設定されます(sprite._layer 属性に上書きされます)。Spriteにlayer属性が設定されておらず、**kwarg引数も設定されなかった場合は、既定のレイヤーが設定されます。

このクラスはpygameバージョン1.8.0で新たに実装されました。

sprite.layerは誤りのためsprite._layerに変更して訳しています。

LayeredUpdates Group handles layers, that draws like OrderedUpdates.
pygame.sprite.LayeredUpdates(*spites, **kwargs): return LayeredUpdates

This group is fully compatible with pygame.sprite.Sprite.

You can set the default layer through kwargs using 'default_layer' and an integer for the layer. The default layer is 0.

If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains 'layer' then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.

New in pygame 1.8.0


LayeredUpdates.add

一つ、もしくは複数のspriteをgroupに追加します。
LayeredUpdates.add(*sprites, **kwargs): return None

sprite にレイヤー属性が設定されていた場合は、そのレイヤー属性が使用されます。**kwarg引数の中に'layer'の文字が含まれていた場合は、引数に設定したspriteにはその引数が設定されます(sprite._layer 属性に上書きされます)。**kwarg引数が設定されなかった場合は、既定のレイヤーが設定されます。

sprite.layerは誤りのためsprite._layerに変更して訳しています。

add a sprite or sequence of sprites to a group
LayeredUpdates.add(*sprites, **kwargs): return None

If the sprite(s) have an attribute layer then that is used for the layer. If **kwargs contains 'layer' then the sprite(s) will be added to that argument (overriding the sprite layer attribute). If neither is passed then the sprite(s) will be added to the default layer.


LayeredUpdates.sprites

spriteの一覧を戻り値として返します。(一覧の並び順は、最後列にあるspriteから始まり、最前列にあるspriteが最後になります)。
LayeredUpdates.sprites(): return sprites
 
returns a ordered list of sprites (first back, last top).
LayeredUpdates.sprites(): return sprites
 

LayeredUpdates.draw

引数に指定したsurfaceへ、格納する全てのspriteを順番に描写します。
LayeredUpdates.draw(surface): return Rect_list
 
draw all sprites in the right order onto the passed surface.
LayeredUpdates.draw(surface): return Rect_list
 

LayeredUpdates.get_sprites_at

指定した位置にある全てのspriteの一覧を戻り値として返します。
LayeredUpdates.get_sprites_at(pos): return colliding_sprites

最後列にあるspriteは一覧の最初に取得され、最前列にあるspriteは一覧の最後に取得されます。

returns a list with all sprites at that position.
LayeredUpdates.get_sprites_at(pos): return colliding_sprites

Bottom sprites first, top last.


LayeredUpdates.get_sprite

格納されたspriteの中から、idx引数に指定した配列位置にあるspriteを戻り値として返します。
LayeredUpdates.get_sprite(idx): return sprite

idx引数に指定した値が格納しているsprite数の配列範囲外だった場合は、IndexOutOfBoundsエラーが発生します。

returns the sprite at the index idx from the groups sprites
LayeredUpdates.get_sprite(idx): return sprite

Raises IndexOutOfBounds if the idx is not within range.


LayeredUpdates.remove_sprites_of_layer

レイヤーから全てのspriteを削除し、その削除した一覧を戻り値として返します。
LayeredUpdates.remove_sprites_of_layer(layer_nr): return sprites
 
removes all sprites from a layer and returns them as a list.
LayeredUpdates.remove_sprites_of_layer(layer_nr): return sprites
 

LayeredUpdates.layers

設定されているレイヤーの一覧を戻り値として取得します(unique)。並び順は、最後列から最前列の順番になります。
LayeredUpdates.layers(): return layers
 
returns a list of layers defined (unique), sorted from botton up.
LayeredUpdates.layers(): return layers
 

LayeredUpdates.change_layer

spriteのレイヤーを変更します。
LayeredUpdates.change_layer(sprite, new_layer): return None

この命令を実行するためには、引数に指定するspriteがこのクラスに格納されていなければなりません。プログラム内部では、格納されているかのチェックは行われません。

changes the layer of the sprite
LayeredUpdates.change_layer(sprite, new_layer): return None

sprite must have been added to the renderer. It is not checked.


LayeredUpdates.get_layer_of_sprite

spriteが載っているレイヤーを戻り値として返します。
LayeredUpdates.get_layer_of_sprite(sprite): return layer

指定したspriteが見つからなかった場合は、既定のレイヤーが戻り値として返ります。

returns the layer that sprite is currently in.
LayeredUpdates.get_layer_of_sprite(sprite): return layer

If the sprite is not found then it will return the default layer.


LayeredUpdates.get_top_layer

最前列のレイヤーを戻り値として返します。
LayeredUpdates.get_top_layer(): return layer
 
returns the top layer
LayeredUpdates.get_top_layer(): return layer
 

LayeredUpdates.get_bottom_layer

最後列のレイヤーを戻り値として返します。
LayeredUpdates.get_bottom_layer(): return layer
 
returns the bottom layer
LayeredUpdates.get_bottom_layer(): return layer
 

LayeredUpdates.move_to_front

指定したspriteを最前列のレイヤーに移動させます。
LayeredUpdates.move_to_front(sprite): return None

spriteのレイヤーを最前列のレイヤーに変更し、前に移動させます(added at the end of that layer)。

brings the sprite to front layer
LayeredUpdates.move_to_front(sprite): return None

Brings the sprite to front, changing sprite layer to topmost layer (added at the end of that layer).


LayeredUpdates.move_to_back

指定したspriteを最後列のレイヤーに移動させます。
LayeredUpdates.move_to_back(sprite): return None

Moves the sprite to the bottom layer, moving it behind all other layers and adding one additional layer.

moves the sprite to the bottom layer
LayeredUpdates.move_to_back(sprite): return None

Moves the sprite to the bottom layer, moving it behind all other layers and adding one additional layer.


LayeredUpdates.get_top_sprite

最前列のspriteを戻り値として返します。
LayeredUpdates.get_top_sprite(): return Sprite
 
returns the topmost sprite
LayeredUpdates.get_top_sprite(): return Sprite
 

LayeredUpdates.get_sprites_from_layer

レイヤーから全てのspriteを戻り値として返します。追加された順番がspriteの並び順となります。
LayeredUpdates.get_sprites_from_layer(layer): return sprites

レイヤーから全てのspriteを戻り値として返します。追加された順番がspriteの並び順となります。この命令では線形探索が使われ、取得したspriteがレイヤーから削除されることもありません。

returns all sprites from a layer, ordered by how they where added
LayeredUpdates.get_sprites_from_layer(layer): return sprites

Returns all sprites from a layer, ordered by how they where added. It uses linear search and the sprites are not removed from layer.


LayeredUpdates.switch_layer

layer1 から layer2 へspritesを移動させます。
LayeredUpdates.switch_layer(layer1_nr, layer2_nr): return None

この関数では、引数に指定したlayerの整合性チェックまでは行わないので、必ず定義済みのlayerを指定してください。

switches the sprites from layer1 to layer2
LayeredUpdates.switch_layer(layer1_nr, layer2_nr): return None

The layers number must exist, it is not checked.


pygame.sprite.LayeredDirty

DirtySprites用のGroupです。これは LayeredUpdatesのサブクラスです。
pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty

この group ではpygame.sprite.DirtySpriteクラスか、もしくは下記の属性を持つspriteクラス を格納することができます。:

    image, rect, dirty, visible, blendmode (詳しくはDirtySpriteの項目を参照してください)。

この関数はdirtyフラグを使っているので、spriteクラスを常時大量に使う場合にはpygame.sprite.RenderUpdates関数よりも処理が速くなります。また、dirtyフラグを使った部分更新と通常の画面全体更新を必要に応じて切り替えるので、処理速度を気にする必要はありません。

このクラスはpygame.sprite.Groupクラスとほぼ同じ機能を持ちます。kwargs引数に任意の属性を設定することができます:

    _use_update: TrueかFalseを設定します。既定では Falseが設定されています。
    _default_layer: default layer where sprites without a layer are added.
    _time_threshold:部分更新モードと全体更新モードの切り替えにかかる限界時間です。既定では 1000./80 が設定されており、1000./(フレームレート)となります。

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

LayeredDirty Group is for DirtySprites. Subclasses LayeredUpdates.
pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty

This group requires pygame.sprite.DirtySprite or any sprite that has the following attributes:

    image, rect, dirty, visible, blendmode (see doc of DirtySprite).

It uses the dirty flag technique and is therefore faster than the pygame.sprite.RenderUpdates if you have many static sprites. It also switches automatically between dirty rect update and full screen drawing, so you do no have to worry what would be faster.

Same as for the pygame.sprite.Group. You can specify some additional attributes through kwargs:

    _use_update: True/False   default is False
    _default_layer: default layer where sprites without a layer are added.
    _time_threshold: treshold time for switching between dirty rect mode
        and fullscreen mode, defaults to 1000./80  == 1000./fps

New in pygame 1.8.0


LayeredDirty.draw

引数に指定したsurfaceへ、格納する全てのspriteを順番に描写します。
LayeredDirty.draw(surface, bgd=None): return Rect_list

この命令ではbackgroundの情報も設定することができます。 background情報が既に設定されている場合は、bgd引数で設定した値は無視されます。

draw all sprites in the right order onto the passed surface.
LayeredDirty.draw(surface, bgd=None): return Rect_list

You can pass the background too. If a background is already set, then the bgd argument has no effect.


LayeredDirty.clear

画面の塗りつぶしに使うbackground情報を設定します。
LayeredDirty.clear(surface, bgd): return None
 
used to set background
LayeredDirty.clear(surface, bgd): return None
 

LayeredDirty.repaint_rect

指定した範囲の再描写を行います。
LayeredDirty.repaint_rect(screen_rect): return None
screen_rect引数で、再描写する画面の座標を設定します。
repaints the given area
LayeredDirty.repaint_rect(screen_rect): return None

screen_rect is in screencoordinates.


LayeredDirty.set_clip

描写可能領域を設定します。 None値を引数に設定すると、描写可能領域の設定が解除されます。(既定ではNone値が設定されています)
LayeredDirty.set_clip(screen_rect=None): return None
 
clip the area where to draw. Just pass None (default) to reset the clip
LayeredDirty.set_clip(screen_rect=None): return None
 

LayeredDirty.get_clip

描写可能領域を取得します。
LayeredDirty.get_clip(): return Rect
 
clip the area where to draw. Just pass None (default) to reset the clip
LayeredDirty.get_clip(): return Rect
 

LayeredDirty.change_layer

spriteのレイヤーを変更します。
change_layer(sprite, new_layer): return None

この命令を実行するためには、引数に指定するspriteがこのクラスに格納されていなければなりません。プログラム内部では、格納されているかのチェックは行われません。

changes the layer of the sprite
change_layer(sprite, new_layer): return None

sprite must have been added to the renderer. It is not checked.


LayeredDirty.set_timing_treshold

閾値をミリ秒単位で設定します。
set_timing_treshold(time_ms): return None

既定では1000./80が設定されています。この 80 とは、全体描写モードへの切り替えにかかるフレームレートです。

sets the treshold in milliseconds
set_timing_treshold(time_ms): return None

Default is 1000./80 where 80 is the fps I want to switch to full screen mode.


pygame.sprite.GroupSingle

Spriteオブジェクトを一つだけ格納できるGroupです。
pygame.sprite.GroupSingle(sprite=None): return GroupSingle

このクラスではSpriteオブジェクトを一つだけ格納できます。Spriteを新たに追加するたびに、格納されているSpriteは削除されます。

このGroupは、GroupSingle.spriteという内部に格納したSpriteを制御するための特殊なプロパティを持っています。空のGroupを作成する場合は引数にNoneを指定します。引数にSpriteを設定して、GroupSingleにSpriteを格納することもできます。

Group container that holds a single Sprite
pygame.sprite.GroupSingle(sprite=None): return GroupSingle

The GroupSingle container only holds a single Sprite. When a new Sprite is added, the old one is removed.

There is a special property, GroupSingle.sprite, that accesses the Sprite that this Group contains. It can be None when the Group is empty. The property can also be assigned to add a Sprite into the GroupSingle container.


pygame.sprite.spritecollide

Groupが格納しているSpriteの中から、指定したSpriteと接触しているものを探します。
pygame.sprite.spritecollide(sprite, group, dokill, collided = None): return Sprite_list

Groupが格納している全てのSpriteの中から、指定したSpriteと接触しているものの一覧を戻り値として返します。各SpriteのSprite.rect属性同士を比較して、接触しているどうかの判別を行います。

dokill引数にはbool型の値を設定します。Trueを設定した場合は、取得したSpriteの一覧はGroupから削除されます。

collided 引き数には、二つのspriteが接触しているかどうかを調べるコールバック関数を設定します。このコールバック関数は、spritesを二つ引数として設定でき、それらが接触しているどうかを示すbool型の戻り値を返すようにしてください。collided引数を設定しない場合は、sprite全てにその範囲を表すrect属性を設定するようにしてください。この関数では、spriteの持つrect属性を使って当たり判定を調べます。

collided引数に設定できるコールバック関数は以下のものです:

    collide_rect, collide_rect_ratio, collide_circle,
    collide_circle_ratio, collide_mask
find Sprites in a Group that intersect another Sprite
pygame.sprite.spritecollide(sprite, group, dokill, collided = None): return Sprite_list

Return a list containing all Sprites in a Group that intersect with another Sprite. Intersection is determined by comparing the Sprite.rect attribute of each Sprite.

The dokill argument is a bool. If set to True, all Sprites that collide will be removed from the Group.

The collided argument is a callback function used to calculate if two sprites are colliding. it should take two sprites as values, and return a bool value indicating if they are colliding. If collided is not passed, all sprites must have a "rect" value, which is a rectangle of the sprite area, which will be used to calculate the collision.

collided callables:

    collide_rect, collide_rect_ratio, collide_circle,
    collide_circle_ratio, collide_mask

pygame.sprite.collide_rect

rect属性を使って二つのspriteの当たり判定を行います。
pygame.sprite.collide_rect(left, right): return bool

二つのspriteが接触しているかどうかを調べます。pygameのRect.colliderectを使って当たり判定の計算を行います。この関数は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的に作られています。引数に使う Sprite には"rect" 属性が設されていなければなりません。

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

collision detection between two sprites, using rects.
pygame.sprite.collide_rect(left, right): return bool

Tests for collision between two sprites. Uses the pygame rect colliderect function to calculate the collision. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" attributes.

New in pygame 1.8.0


pygame.sprite.collide_rect_ratio

rectの大きさを指定した比率に変換したうえで、二つのspriteの当たり判定を調べます。
pygame.sprite.collide_rect_ratio(ratio): return collided_callable

rect属性の大きさを変換したうえで、二つのspriteの当たり判定を調べるためのコーラブルクラスです。

この関数の戻り値は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的としています。

ratio引数には浮動少数値を設定します。 ratioが1.0ならば大きさは同じ、 2.0ならば大きさは2倍、0.5ならば大きさは半分となります。

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

collision detection between two sprites, using rects scaled to a ratio.
pygame.sprite.collide_rect_ratio(ratio): return collided_callable

A callable class that checks for collisions between two sprites, using a scaled version of the sprites rects.

Is created with a ratio, the instance is then intended to be passed as a collided callback function to the *collide functions.

A ratio is a floating point number - 1.0 is the same size, 2.0 is twice as big, and 0.5 is half the size.

New in pygame 1.8.1


pygame.sprite.collide_circle

円に変換した大きさを使って、二つのspriteの当たり判定を行います。
pygame.sprite.collide_circle(left, right): return bool

各spriteの中心を起点として円を作り、その円同士が重なるかを調べることによって当たり判定を行います。spritesが"radius"属性を持っていればそれを元にして円が作られます。それがない場合は、"rect"属性のサイズを完全に囲むことができる大きさの円が作成されます。この関数は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的に作られています。引数に使う Sprite には"rect" 属性と、可能であれば"radius"属性が設定されていなければなりません。

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

collision detection between two sprites, using circles.
pygame.sprite.collide_circle(left, right): return bool

Tests for collision between two sprites, by testing to see if two circles centered on the sprites overlap. If the sprites have a "radius" attribute, that is used to create the circle, otherwise a circle is created that is big enough to completely enclose the sprites rect as given by the "rect" attribute. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "radius" attribute.

New in pygame 1.8.1


pygame.sprite.collide_circle_ratio

radius属性の大きさを指定した比率に変換したうえで、二つのspriteの当たり判定を調べます。
pygame.sprite.collide_circle_ratio(ratio): return collided_callable

radius属性の大きさを指定した比率に変換したうえで、二つのspriteの当たり判定を調べるためのコーラブルクラスです。

この関数の戻り値は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的としています。

radius引数には浮動少数値を設定します。radiusが1.0ならば大きさは同じ、 2.0ならば大きさは2倍、0.5ならば大きさは半分となります。

このコーラブルクラスでは、ratio引数に指定した比率で円の半径サイズを変更し、spriteを中心として円が重なっているかどうかを調べることによって当たり判定を調べます。spritesが"radius"属性を持っていればそれを元にして円が作られます。それがない場合は、"rect"属性のサイズを完全に囲むことができる大きさの円が作成されます。この関数の戻り値は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的としています。引数に使う Sprite には"rect" 属性と"radius"属性が設定されていなければなりません。

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

collision detection between two sprites, using circles scaled to a ratio.
pygame.sprite.collide_circle_ratio(ratio): return collided_callable

A callable class that checks for collisions between two sprites, using a scaled version of the sprites radius.

Is created with a floating point ratio, the instance is then intended to be passed as a collided callback function to the *collide functions.

A ratio is a floating point number - 1.0 is the same size, 2.0 is twice as big, and 0.5 is half the size.

The created callable tests for collision between two sprites, by testing to see if two circles centered on the sprites overlap, after scaling the circles radius by the stored ratio. If the sprites have a "radius" attribute, that is used to create the circle, otherwise a circle is created that is big enough to completely enclose the sprites rect as given by the "rect" attribute. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "radius" attribute.

New in pygame 1.8.1


pygame.sprite.collide_mask

mask属性を使って二つのspriteの当たり判定を行います。
pygame.sprite.collide_mask(SpriteLeft, SpriteRight): return bool

bitmaskが重なっているかどうかを調べることによって、二つのspriteの当たり判定を行います。spriteが "mask" 属性を持っていた場合はそれがマスクとして使われ、持っていない場合はspriteの画像を基にしてマスクが作成されます。この関数は、spritecollideで当たり判定を調べるためのコールバック関数として使うことを目的に作られています。引数に使う Sprite には"rect" 属性と"mask"属性が定義されていなければなりません。

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

collision detection between two sprites, using masks.
pygame.sprite.collide_mask(SpriteLeft, SpriteRight): return bool

Tests for collision between two sprites, by testing if thier bitmasks overlap. If the sprites have a "mask" attribute, that is used as the mask, otherwise a mask is created from the sprite image. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" and an optional "mask" attribute.

New in pygame 1.8.0


pygame.sprite.groupcollide

二つの Group間で、接触している全てのSpriteを探します。
pygame.sprite.groupcollide(group1, group2, dokill1, dokill2): return Sprite_dict

この関数では、二つのGroup内のSpriteが接触しているかどうかを調べます。 各SpriteのSprite.rect属性同士を比較して、接触しているかどうかの判別を行います。

Group1内のSpriteが戻り値として取得されます。取得された各Spriteには、それと接触しているgroup2内のSpriteの一覧が格納されています。

dokill1 引数かdokill2 引数に True が設定された場合は、該当するGroupからは取得したSpritesの一覧が削除されます。

補足
dokill2 引数にTrueを設定すると重複カウントがされなくなります。
Group1内の一つのSpriteに接触していると判定されれば、他のSpriteに接触していてもそこではカウントされません。
Spriteの削除は最後にまとめて行われるのではなく、接触判定時に合わせて行われているようです。
find all Sprites that collide between two Groups
pygame.sprite.groupcollide(group1, group2, dokill1, dokill2): return Sprite_dict

This will find intersections between all the Sprites in two groups. Intersection is determined by comparing the Sprite.rect attribute of each Sprite.

Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.

If either dokill argument is True, the intersecting Sprites will be removed from their respective Group.


pygame.sprite.spritecollideany

指定した Sprite が、Group内にあるSpriteのどれかと接触しているかを調べます。
pygame.sprite.spritecollideany(sprite, group): return bool

指定した Sprite が、Group内にあるSpriteのどれかと接触しているかどうかを調べます。各SpriteのSprite.rect属性同士を比較して、接触しているかどうかの判別を行います。

処理が単純なので、 pygame.sprite.spritecollide 命令よりも素早い当たり判定ができます。

simple test if a Sprite intersects anything in a Group
pygame.sprite.spritecollideany(sprite, group): return bool

Test if the given Sprite intersects with any Sprites in a Group. Intersection is determined by comparing of the Sprite.rect attribute of each Sprite.

This collision test can be faster than pygame.sprite.spritecollide - find Sprites in a Group that intersect another Sprite since it has less work to do.


pygame.sprite.spritecollideany

指定した Sprite が、Group内にあるSpriteのどれかと接触しているかを調べます。
pygame.sprite.spritecollideany(sprite, group): return bool

指定した Sprite が、Group内にあるSpriteのどれかと接触しているかどうかを調べます。各SpriteのSprite.rect属性同士を比較して、接触しているかどうかの判別を行います。

処理が単純なので、 pygame.sprite.spritecollide 命令よりも素早い当たり判定ができます。

simple test if a Sprite intersects anything in a Group
pygame.sprite.spritecollideany(sprite, group): return bool

Test if the given Sprite intersects with any Sprites in a Group. Intersection is determined by comparing of the Sprite.rect attribute of each Sprite.

This collision test can be faster than pygame.sprite.spritecollide - find Sprites in a Group that intersect another Sprite since it has less work to do.