pygame.sprite
pygame module with basic game object classes
— Simple base class for visible game objects.
— A subclass of Sprite with more attributes and features.
— A container class to hold and manage multiple Sprite objects.
— Same as pygame.sprite.Group
— Same as pygame.sprite.Group
— Group sub-class that tracks dirty updates.
— RenderUpdates sub-class that draws Sprites in order of addition.
— LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
— LayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.
— Group container that holds a single sprite.
— Find sprites in a group that intersect another sprite.
— Collision detection between two sprites, using rects.
— Collision detection between two sprites, using rects scaled to a ratio.
— Collision detection between two sprites, using circles.
— Collision detection between two sprites, using circles scaled to a ratio.
— Collision detection between two sprites, using masks.
— Find all sprites that collide between two groups.
— Simple test if a sprite intersects anything in a group.

该模块包含几个在游戏中使用的简单类。有一个主Sprite类和几个包含Sprite的Group类。在使用pyGame时,这些类的使用完全是可选的。这些类相当轻量级,只为大多数游戏中常见的代码提供了一个起点。

Sprite类旨在用作游戏中不同类型对象的基类。还有一个简单地存储精灵的基类Group。游戏可以创建新类型的Group类,这些类在它们包含的特别定制的Sprite实例上操作。

基本的Sprite类可以将其包含的Sprite绘制到Surface。这个 Group.draw() 方法要求每个Sprite都有一个 Surface.image 属性和一个 Surface.rect 。这个 Group.clear() 方法需要这些相同的属性,并且可以用来擦除所有带有背景的Sprite。还有更高级的群体: pygame.sprite.RenderUpdates() 和 pygame.sprite.OrderedUpdates() 。

最后,此模块包含多个碰撞功能。这有助于在具有相交边界矩形的多个组中查找精灵。要找到碰撞,Sprite需要有一个 Surface.rect 已分配属性。

这些组旨在高效地移除和添加雪碧。它们还允许进行廉价的测试,以查看某个组中是否已经存在雪碧。给定的精灵可以存在于任意数量的组中。游戏可以使用一些组来控制对象渲染,并使用一组完全不同的组来控制交互或玩家移动。考虑将Sprite保留在有组织的组中,而不是向派生的Sprite类添加类型属性或bool。这将允许稍后在游戏中更容易地进行查找。

精灵和组管理它们与 add() 和 remove() 方法:研究方法。这些方法可以接受单个或多个目标作为成员。这些类的默认初始值设定项还接受单个目标或目标列表作为初始成员资格。在组中重复添加和删除相同的雪碧是安全的。

虽然可以设计不是从下面的Sprite和AbstractGroup类派生的Sprite和Group类,但强烈建议您在添加Sprite或Group类时扩展这些类。

精灵不是线程安全的。因此,如果使用线程,请自己锁定它们。

pygame.sprite.Sprite
Simple base class for visible game objects.
Sprite(*groups) -> Sprite
— method to control sprite behavior
— add the sprite to groups
— remove the sprite from groups
— remove the Sprite from all Groups
— does the sprite belong to any groups
— list of Groups that contain this Sprite

可见游戏对象的基类。派生类将希望重写 Sprite.update() 并将一个 Sprite.image 和 Sprite.rect 属性。初始值设定项可以接受要添加到的任意数量的Group实例。

将子类化Sprite时,请确保在将Sprite添加到组之前调用基本初始值设定项。例如:

class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()
update()
method to control sprite behavior
update(*args, **kwargs) -> None

该方法的默认实现不做任何事情;它只是一个您可以覆盖的方便的“钩子”。此方法由 Group.update() 无论你给出什么论据。

如果不在Group类中使用同名的便利性方法,则不需要使用此方法。

add()
add the sprite to groups
add(*groups) -> None

可以将任意数量的Group实例作为参数传递。雪碧将被添加到它尚未加入的组中。

remove()
remove the sprite from groups
remove(*groups) -> None

可以将任意数量的Group实例作为参数传递。Sprite将从其当前所属的组中删除。

kill()
remove the Sprite from all Groups
kill() -> None

精灵将从包含它的所有组中移除。这不会改变雪碧的状态。调用此方法后,可以继续使用Sprite,包括将其添加到组中。

alive()
does the sprite belong to any groups
alive() -> bool

如果Sprite属于一个或多个组,则返回True。

groups()
list of Groups that contain this Sprite
groups() -> group_list

返回包含此Sprite的所有组的列表。

pygame.sprite.DirtySprite
A subclass of Sprite with more attributes and features.
DirtySprite(*groups) -> DirtySprite

附加的DirtySprite属性及其默认值:

脏=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 therefore not repainted again

混合模式=0

its the special_flags argument of blit, blendmodes

SOURCE_RECT=无

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

可见=1

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

层=0

(READONLY value, it is read when adding it to the
LayeredDirty, for details see doc of LayeredDirty)
pygame.sprite.Group
A container class to hold and manage multiple Sprite objects.
Group(*sprites) -> Group
— list of the Sprites this Group contains
— duplicate the Group
— add Sprites to this Group
— remove Sprites from the Group
— test if a Group contains Sprites
— call the update method on contained Sprites
— blit the Sprite images
— draw a background over the Sprites
— remove all Sprites

Sprite对象的简单容器。可以继承此类以创建具有更具体行为的容器。构造函数接受任意数量的Sprite参数以添加到Group中。该组支持以下标准的Python操作:

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

该集团中的雪碧仅在Python3.6及更高版本上订购。在Python3.6下,在Sprite上绘制和迭代没有特定的顺序。

sprites()
list of the Sprites this Group contains
sprites() -> sprite_list

返回该组包含的所有精灵的列表。您还可以从组中获取迭代器,但不能在修改组时对其进行迭代。

copy()
duplicate the Group
copy() -> Group

使用与原始精灵相同的所有精灵创建新的组。如果您具有子类化的Group,则新对象将具有与原始对象相同的(子)类。只有当派生类的构造函数接受与Group类相同的参数时,这才有效。

add()
add Sprites to this Group
add(*sprites) -> None

将任意数量的雪碧添加到此组中。这将仅添加尚未成为该组成员的精灵。

每个Sprite参数也可以是包含Sprite的迭代器。

remove()
remove Sprites from the Group
remove(*sprites) -> None

从组中删除任意数量的雪碧。这只会删除已经是该组成员的精灵。

每个Sprite参数也可以是包含Sprite的迭代器。

has()
test if a Group contains Sprites
has(*sprites) -> bool

如果组包含所有给定子画面,则返回True。这类似于在组上使用“In”运算符(“If Sprite in group:...”),该操作符测试单个精灵是否属于某个组。

每个Sprite参数也可以是包含Sprite的迭代器。

update()
call the update method on contained Sprites
update(*args, **kwargs) -> None

调用 update() 方法应用于组中的所有精灵。Sprite基类有一个UPDATE方法,该方法接受任意数量的参数,但不执行任何操作。传递给 Group.update() 将被传递给每一款雪碧。

方法获取返回值。 Sprite.update() 方法。

draw()
blit the Sprite images
draw(Surface) -> List[Rect]

将包含的Sprite绘制到Surface参数。这使用了 Sprite.image 属性,并且 Sprite.rect 这个职位。

小组不以任何顺序保存精灵,因此抽签顺序是任意的。

clear()
draw a background over the Sprites
clear(Surface_dest, background) -> None

擦除上一次使用的Sprite Group.draw() 打电话。通过使用背景填充绘制的精灵位置来清除目标曲面。

背景通常是与目标曲面尺寸相同的曲面图像。但是,它也可以是带有两个参数的回调函数;目标Surface和要清除的区域。后台回调函数将被调用多次,每次清除。

下面是一个示例回调,它将用纯红色清除Sprite:

def clear_callback(surf, rect):
    color = 255, 0, 0
    surf.fill(color, rect)
empty()
remove all Sprites
empty() -> None

从该组中删除所有精灵。

pygame.sprite.RenderPlain
Same as pygame.sprite.Group

此类是的别名 pygame.sprite.Group() 。它没有额外的功能。

pygame.sprite.RenderClear
Same as pygame.sprite.Group

此类是的别名 pygame.sprite.Group() 。它没有额外的功能。

pygame.sprite.RenderUpdates
Group sub-class that tracks dirty updates.
RenderUpdates(*sprites) -> RenderUpdates
— blit the Sprite images and track changed areas

此类派生自 pygame.sprite.Group() 。它有一个扩展的 draw() 方法,该方法跟踪屏幕的更改区域。

draw()
blit the Sprite images and track changed areas
draw(surface) -> Rect_list

将所有精灵绘制到曲面,与 Group.draw() 。此方法还返回屏幕上已更改的矩形区域的列表。返回的更改包括受以前版本影响的屏幕区域 Group.clear() 电话。

应将返回的RECT列表传递给 pygame.display.update() 。这将有助于提高软件驱动显示模式的性能。这种类型的更新通常只在具有非动画背景的目的地上有帮助。

pygame.sprite.OrderedUpdates()
RenderUpdates sub-class that draws Sprites in order of addition.
OrderedUpdates(*spites) -> OrderedUpdates

此类派生自 pygame.sprite.RenderUpdates() 。它会保持将精灵添加到组中以进行渲染的顺序。这使得在组中添加和删除精灵的速度比常规组稍慢。

pygame.sprite.LayeredUpdates
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -> LayeredUpdates
— add a sprite or sequence of sprites to a group
— returns a ordered list of sprites (first back, last top).
— draw all sprites in the right order onto the passed surface.
— returns a list with all sprites at that position.
— returns the sprite at the index idx from the groups sprites
— removes all sprites from a layer and returns them as a list.
— returns a list of layers defined (unique), sorted from bottom up.
— changes the layer of the sprite
— returns the layer that sprite is currently in.
— returns the top layer
— returns the bottom layer
— brings the sprite to front layer
— moves the sprite to the bottom layer
— returns the topmost sprite
— returns all sprites from a layer, ordered by how they where added
— switches the sprites from layer1 to layer2

此组完全兼容 pygame.sprite.SpriteSimple base class for visible game objects. 。

您可以使用‘DEFAULT_LAYER’和一个整数通过kwargs设置默认层。默认层为0。

如果您添加的精灵具有ATTRIBUTE_LAYER,则将使用该层。如果**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然后使用默认层来添加精灵。

New in pygame 1.8.

add()
add a sprite or sequence of sprites to a group
add(*sprites, **kwargs) -> None

如果 sprite(s) 有一个属性层,然后用于该层。如果**kwargs包含‘Layer’,则 sprite(s) 将添加到该参数(覆盖精灵层属性)。如果两者都未通过,则 sprite(s) 将添加到默认层。

sprites()
returns a ordered list of sprites (first back, last top).
sprites() -> sprites
draw()
draw all sprites in the right order onto the passed surface.
draw(surface) -> Rect_list
get_sprites_at()
returns a list with all sprites at that position.
get_sprites_at(pos) -> colliding_sprites

最底层的精灵,最底层的精灵。

get_sprite()
returns the sprite at the index idx from the groups sprites
get_sprite(idx) -> sprite

如果idx不在范围内,则引发IndexOutOfBound。

remove_sprites_of_layer()
removes all sprites from a layer and returns them as a list.
remove_sprites_of_layer(layer_nr) -> sprites
layers()
returns a list of layers defined (unique), sorted from bottom up.
layers() -> layers
change_layer()
changes the layer of the sprite
change_layer(sprite, new_layer) -> None

必须已将精灵添加到渲染器中。它未被选中。

get_layer_of_sprite()
returns the layer that sprite is currently in.
get_layer_of_sprite(sprite) -> layer

如果找不到精灵,它将返回默认层。

get_top_layer()
returns the top layer
get_top_layer() -> layer
get_bottom_layer()
returns the bottom layer
get_bottom_layer() -> layer
move_to_front()
brings the sprite to front layer
move_to_front(sprite) -> None

将精灵置于最前面,将精灵层更改为最顶层(添加到该层的末尾)。

move_to_back()
moves the sprite to the bottom layer
move_to_back(sprite) -> None

将精灵移动到最下面的层,将其移动到所有其他层的后面,并添加一个附加层。

get_top_sprite()
returns the topmost sprite
get_top_sprite() -> Sprite
get_sprites_from_layer()
returns all sprites from a layer, ordered by how they where added
get_sprites_from_layer(layer) -> sprites

返回层中的所有精灵,按它们的添加方式排序。它使用线性搜索,精灵不会从层中移除。

switch_layer()
switches the sprites from layer1 to layer2
switch_layer(layer1_nr, layer2_nr) -> None

层编号必须存在,未选中。

pygame.sprite.LayeredDirty
LayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.
LayeredDirty(*spites, **kwargs) -> LayeredDirty
— draw all sprites in the right order onto the passed surface.
— used to set background
— repaints the given area
— clip the area where to draw. Just pass None (default) to reset the clip
— clip the area where to draw. Just pass None (default) to reset the clip
— changes the layer of the sprite
— sets the threshold in milliseconds
— sets the threshold in milliseconds

此组需要 pygame.sprite.DirtySpriteA subclass of Sprite with more attributes and features. 或具有以下属性的任何精灵:

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

它使用脏标志技术,因此比 pygame.sprite.RenderUpdatesGroup sub-class that tracks dirty updates. 如果您有许多静态精灵。它还可以在脏RECT更新和全屏绘图之间自动切换,因此您不必担心哪个更快。

与 pygame.sprite.GroupA container class to hold and manage multiple Sprite objects. 。您可以通过kwargs指定一些其他属性:

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

New in pygame 1.8.

draw()
draw all sprites in the right order onto the passed surface.
draw(surface, bgd=None) -> Rect_list

你也可以通过背景。如果已经设置了背景,则bgd参数不起作用。

clear()
used to set background
clear(surface, bgd) -> None
repaint_rect()
repaints the given area
repaint_rect(screen_rect) -> None

Screen_rect在屏幕坐标中。

set_clip()
clip the area where to draw. Just pass None (default) to reset the clip
set_clip(screen_rect=None) -> None
get_clip()
clip the area where to draw. Just pass None (default) to reset the clip
get_clip() -> Rect
change_layer()
changes the layer of the sprite
change_layer(sprite, new_layer) -> None

必须已将精灵添加到渲染器中。它未被选中。

set_timing_treshold()
sets the threshold in milliseconds
set_timing_treshold(time_ms) -> None

已弃用:请改用SET_TIMING_THRESHOLD()。

Deprecated since pygame 2.1.1.

set_timing_threshold()
sets the threshold in milliseconds
set_timing_threshold(time_ms) -> None

默认为1000.0/80.0.这意味着,如果更新方法更新屏幕的时间太长,以至于帧速率降到每秒80帧以下,则将使用翻转方法而不是更新方法来绘制屏幕。

New in pygame 2.1.1.

引发

TypeError -- 如果 time_ms 不是整型或浮点型

pygame.sprite.GroupSingle()
Group container that holds a single sprite.
GroupSingle(sprite=None) -> GroupSingle

GroupSingle容器仅包含一个Sprite。添加新的Sprite时,旧的将被移除。

有一种特殊的财产, GroupSingle.sprite ,它访问该组包含的Sprite。当集团为空时,可以为None。还可以分配该属性以将Sprite添加到GroupSingle容器中。

pygame.sprite.spritecollide()
Find sprites in a group that intersect another sprite.
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list

返回一个列表,其中包含与另一个精灵相交的组中的所有精灵。交点是通过比较 Sprite.rect 每个精灵的属性。

杜基尔的论点是无稽之谈。如果设置为True,则将从组中移除所有碰撞的精灵。

Collided参数是一个回调函数,用于计算两个精灵是否发生冲突。它应该接受两个精灵作为值,并返回一个布尔值,指示它们是否发生碰撞。如果没有传递碰撞,则所有精灵必须有一个“rect”值,它是精灵区域的一个矩形,将用于计算碰撞。

碰撞的可伸缩材料:

collide_rect, collide_rect_ratio, collide_circle,
collide_circle_ratio, collide_mask

示例:

# See if the Sprite block has collided with anything in the Group block_list
# The True flag will remove the sprite in block_list
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)

# Check the list of colliding sprites, and add one to the score for each one
for block in blocks_hit_list:
    score +=1
pygame.sprite.collide_rect()
Collision detection between two sprites, using rects.
collide_rect(left, right) -> bool

测试两个精灵之间的碰撞。使用PYGAME RECT Colliderect函数计算碰撞。打算作为冲突回调函数传递给*Collide函数。精灵必须具有“RECT”属性。

New in pygame 1.8.

pygame.sprite.collide_rect_ratio()
Collision detection between two sprites, using rects scaled to a ratio.
collide_rect_ratio(ratio) -> collided_callable

一个可调用的类,它使用精灵矩形的缩放版本检查两个精灵之间的冲突。

以一定的比率创建,则该实例将作为冲突回调函数传递给*Collide函数。

比率是浮点数-1.0的大小相同,2.0的大小是它的两倍,而0.5的大小是它的一半。

New in pygame 1.8.1.

pygame.sprite.collide_circle()
Collision detection between two sprites, using circles.
collide_circle(left, right) -> bool

测试两个精灵之间的碰撞,方法是测试以精灵为中心的两个圆是否重叠。如果精灵具有“Radius”属性,则用于创建圆,否则将创建一个足够大的圆,以完全包围由“rect”属性给出的精灵RECT。打算作为冲突回调函数传递给*Collide函数。精灵必须有一个“rect”和一个可选的“Radius”属性。

New in pygame 1.8.1.

pygame.sprite.collide_circle_ratio()
Collision detection between two sprites, using circles scaled to a ratio.
collide_circle_ratio(ratio) -> collided_callable

一个可调用的类,使用精灵半径的缩放版本检查两个精灵之间的冲突。

是用浮点比率创建的,则该实例将作为冲突回调函数传递给*Collide函数。

比率是浮点数-1.0的大小相同,2.0的大小是它的两倍,而0.5的大小是它的一半。

创建的可调用测试用于两个精灵之间的碰撞,方法是在按存储的比率缩放圆半径后,测试以精灵为中心的两个圆是否重叠。如果精灵具有“Radius”属性,则用于创建圆,否则将创建一个足够大的圆,以完全包围由“rect”属性给出的精灵RECT。打算作为冲突回调函数传递给*Collide函数。精灵必须有一个“rect”和一个可选的“Radius”属性。

New in pygame 1.8.1.

pygame.sprite.collide_mask()
Collision detection between two sprites, using masks.
collide_mask(sprite1, sprite2) -> (int, int)
collide_mask(sprite1, sprite2) -> None

通过测试两个精灵之间的位掩码是否重叠来测试它们之间的冲突(使用 pygame.mask.Mask.overlap()Returns the point of intersection )。如果精灵有一个 mask 属性,则将其用作掩码,否则从子画面的 image (使用 pygame.mask.from_surface()Creates a Mask from the given surface )。精灵必须有一个 rect 属性; mask 属性是可选的。

返回遮罩之间的第一个冲突点。冲突点从 sprite1 的掩码的左上角(始终为(0,0))。冲突点是掩码内的一个位置,与 sprite1 。

此函数旨在作为 collided 组冲突函数的回调函数(请参见 spritecollide() , groupcollide() , spritecollideany() )。

备注

若要提高性能,请创建并设置 mask 适用于将使用此功能检查碰撞的所有精灵。否则,每次调用此函数时,它都会创建新的掩码。

备注

每次更改精灵的图像时(例如,如果使用新图像或旋转现有图像),都需要重新创建新的蒙版。

# Example of mask creation for a sprite.
sprite.mask = pygame.mask.from_surface(sprite.image)
返回

遮罩之间的第一个冲突点或 None 如果没有冲突

返回类型

tuple(int, int) or NoneType

New in pygame 1.8.0.

pygame.sprite.groupcollide()
Find all sprites that collide between two groups.
groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict

这将在两个组中的所有精灵之间找到碰撞。冲突是通过比较 Sprite.rect 属性,或者使用Collided函数(如果它不是None)。

Group1中的每个Sprite都被添加到返回词典中。每个项目的值是组2中相交的精灵的列表。

如果任一dokill参数为True,则碰撞的精灵将从其各自的组中移除。

Collided参数是一个回调函数,用于计算两个精灵是否发生冲突。它应该接受两个精灵作为值,并返回一个布尔值,指示它们是否发生碰撞。如果没有传递碰撞,则所有精灵必须有一个“rect”值,它是精灵区域的一个矩形,将用于计算碰撞。

pygame.sprite.spritecollideany()
Simple test if a sprite intersects anything in a group.
spritecollideany(sprite, group, collided = None) -> Sprite 与返回的精灵发生碰撞。
spritecollideany(sprite, group, collided = None) -> None 无碰撞

如果精灵与组中的任何一个精灵发生碰撞,则返回该组中的一个精灵。如果没有碰撞,则不会返回任何内容。

如果您不需要的所有功能 pygame.sprite.spritecollide() 函数,这个函数会更快一点。

Collided参数是一个回调函数,用于计算两个精灵是否发生冲突。它应该接受两个精灵作为值,并返回一个布尔值,指示它们是否发生碰撞。如果没有传递碰撞,则所有精灵必须有一个“rect”值,它是精灵区域的一个矩形,将用于计算碰撞。




Edit on GitHub