Sprite列表#

arcade.check_for_collision#

arcade.check_for_collision(sprite1: arcade.sprite.Sprite, sprite2: arcade.sprite.Sprite) bool[源代码]#

检查两个精灵之间是否存在冲突。

参数
  • sprite1 -- 第一个精灵

  • sprite2 -- 第二个精灵

退货

真或假取决于精灵是否相交。

返回类型

bool

arcade.check_for_collision_with_list#

arcade.check_for_collision_with_list(sprite: arcade.sprite.Sprite, sprite_list: arcade.sprite_list.sprite_list.SpriteList, method=0) List[arcade.sprite.Sprite][源代码]#

检查精灵和精灵列表之间的碰撞。

参数
  • sprite (Sprite) -- 要检查的Sprite

  • sprite_list (SpriteList) -- 要检查的SpriteList

  • method (int) -- 碰撞检查方法。0表示自动选择。(空间如果可用,GPU如果1500+精灵,否则简单)1是空间哈希如果可用,2是基于GPU的,3是简单的一切检查。默认为0。

返回

精灵冲突列表或空列表。

返回类型

list

arcade.check_for_collision_with_lists#

arcade.check_for_collision_with_lists(sprite: arcade.sprite.Sprite, sprite_lists: Iterable[arcade.sprite_list.sprite_list.SpriteList], method=1) List[arcade.sprite.Sprite][源代码]#

检查Sprite和SpriteList列表之间的冲突。

参数
  • sprite (Sprite) -- 要检查的Sprite

  • sprite_lists (List[SpriteList]) -- 要检查的SpriteList

  • method (int) -- 碰撞检查方法。1是空间散列(如果可用),2是基于GPU的,3是缓慢的CPU绑定检查-一切。默认为1。

返回

精灵冲突列表或空列表。

返回类型

list

arcade.get_closest_sprite#

arcade.get_closest_sprite(sprite: arcade.sprite.Sprite, sprite_list: arcade.sprite_list.sprite_list.SpriteList) Optional[Tuple[arcade.sprite.Sprite, float]][源代码]#

给定Sprite和SpriteList,返回最近的Sprite及其距离。

参数
  • sprite (Sprite) -- 目标精灵

  • sprite_list (SpriteList) -- 列表以搜索最近的精灵。

返回

包含最接近的精灵和最小距离的元组。如果精灵列表为空,则返回 None

返回类型

Optional[Tuple[Sprite, float]]

arcade.get_sprites_at_exact_point#

arcade.get_sprites_at_exact_point(point: Union[Tuple[float, float], List[float]], sprite_list: arcade.sprite_list.sprite_list.SpriteList) List[arcade.sprite.Sprite][源代码]#

获取Center_x、Center_y与给定点匹配的精灵列表。这不会返回与点重叠的精灵,中心必须完全匹配。

参数
  • point (Point) -- 指向检查

  • sprite_list (SpriteList) -- 要检查的SpriteList

返回

精灵冲突列表或空列表。

返回类型

list

arcade.get_sprites_at_point#

arcade.get_sprites_at_point(point: Union[Tuple[float, float], List[float]], sprite_list: arcade.sprite_list.sprite_list.SpriteList) List[arcade.sprite.Sprite][源代码]#

获取特定时间点的精灵列表。此函数用于查看是否有任何精灵与指定点重叠。如果某个精灵具有不同的center_x/center_y,但触及该点,则会返回该精灵。

参数
  • point (Point) -- 指向检查

  • sprite_list (SpriteList) -- 要检查的SpriteList

返回

精灵冲突列表或空列表。

返回类型

list

arcade.SpriteList#

class arcade.SpriteList(use_spatial_hash=None, spatial_hash_cell_size=128, is_static=False, atlas: TextureAtlas = None, capacity: int = 100, lazy: bool = False, visible: bool = True)[源代码]#

精灵列表的目的是批量绘制精灵列表。随着项目中精灵数量的增加,绘制单个精灵不会给您带来任何性能方面的好处。精灵列表包含许多利用您的图形处理器进行的低级优化。客观地说,一个精灵可以包含数以万计的精灵而不会有任何问题。不会渲染视区/窗口外的精灵。

如果精灵列表将用于碰撞,则启用空间散列是一个好主意。尤其是如果没有精灵在移动的话。这将进行冲突检查 很多 再快点。在技术术语中,冲突检查是 O(1) 启用了空间哈希,并且 O(N) 没有。但是,如果您有一个移动精灵列表,则在移动它们时更新空间散列的成本可能比使用空间碰撞检查节省的成本更高。这需要在个案的基础上进行分析。

有关高级选项,请查看arcade文档中的高级部分。

参数
  • use_spatial_hash (bool) -- 如果设置为True,这将使创建精灵和在SpriteList中移动精灵的速度变慢,但它将加快与SpriteList中的项的碰撞检测。非常适合在大型地图中使用静态墙/平台进行碰撞检测。

  • spatial_hash_cell_size (int) -- 空间哈希的像元大小(默认值:128)

  • is_static (bool) -- 已弃用。此参数不起作用。

  • atlas (TextureAtlas) -- (高级)此精灵列表的纹理贴图集。如果没有提供地图集,将使用全局/默认地图集。

  • capacity (int) -- (高级)内部缓冲区的初始容量。这是对这份清单所能容纳的精灵数量上限的建议。通常可以保留缺省值。

  • lazy (bool) -- (高级)启用惰性精灵列表可确保在第一次绘制调用或 initialize() 叫做。在线程中创建精灵列表时,这会很有用,因为只允许主线程与OpenGL交互。

  • visible (bool) -- 将其设置为False将导致不绘制SpriteList。当调用DRAW时,该方法将返回而不绘制。

property alpha: int#

获取或设置整个精灵列表的Alpha/透明度。这是一个从0到255的字节值,其中0是完全透明/不可见的,255是不透明的。

property alpha_normalized: float#

获取或设置列表中所有精灵的Alpha/透明度。这是一个从0.0到1.0的浮点数,其中0.0是完全透明/不可见的,1.0是不透明的。

这是在精灵列表颜色中设置Alpha值的快捷方式。

返回类型

float

append(sprite: arcade.sprite_list.sprite_list._SpriteType)[源代码]#

将新的精灵添加到列表中。

参数

sprite (Sprite) -- 要添加到列表中的Sprite。

property atlas: TextureAtlas#

获取此精灵列表的纹理图集

property buffer_angles: arcade.gl.buffer.Buffer#

获取精灵列表的内部OpenGL角度缓冲区。

该缓冲区包含一系列32位浮点数,表示每个精灵的旋转角度(以度为单位)。

此缓冲区附加到 geometry 具有名称的实例 in_angle

property buffer_colors: arcade.gl.buffer.Buffer#

获取此精灵列表的内部OpenGL颜色缓冲区。

该缓冲区包含一系列表示每个精灵的RGBA颜色的32位浮点。4 x浮点=RGBA。

此缓冲区附加到 geometry 具有名称的实例 in_color

property buffer_indices: arcade.gl.buffer.Buffer#

获取此精灵列表的内部索引缓冲区。

其他缓冲区中的数据顺序不匹配 spritelist[i] 。必须使用索引缓冲区来解析正确的顺序。它只包含一系列引用其他缓冲区中的位置的整数。

另请注意,此缓冲区的长度可能大于精灵的数量。依赖于 len(spritelist) 对于正确的长度。

此索引缓冲区附加到 geometry 实例,并将在呈现或转换时自动应用于输入缓冲区。

property buffer_positions: arcade.gl.buffer.Buffer#

获取该精灵列表的内部OpenGL位置缓冲区。

缓冲区包含具有x和y位置的32位浮点值。这些是每个精灵的中心位置。

此缓冲区附加到 geometry 具有名称的实例 in_pos

property buffer_sizes: arcade.gl.buffer.Buffer#

获取该精灵列表的内部OpenGL大小缓冲区。

缓冲区包含32位浮点宽度和高值。

此缓冲区附加到 geometry 具有名称的实例 in_size

property buffer_textures: arcade.gl.buffer.Buffer#

获取精灵列表的内部OpenGL纹理ID缓冲区。

此缓冲区包含一系列引用纹理ID的32位浮点数。此ID引用分配给此精灵列表的纹理图集中的纹理。该ID用于在文本贴图集提供的32位浮点纹理中查找纹理坐标。这个系统确保我们可以调整和重建纹理图集,而不必重建每个精灵列表。

此缓冲区附加到 geometry 具有名称的实例 in_texture

请注意,理想情况下,它应该是一个无符号整数,但出于兼容性的考虑,我们将它们存储为32位浮点数。我们在着色器中将它们强制转换为整数。

property center: Tuple[float, float]#

获取列表中所有精灵的平均中心坐标。

clear(deep: bool = True)[源代码]#

移除所有精灵,将精灵列表重置为初始状态。

这种方法的复杂性是 O(N) 带有深度清除(默认)。如果列表中的所有子画面都与列表本身一起被垃圾回收,则可以执行 O(1) 清除使用 ``deep=False`在使用此选项之前,请确保您清楚地知道自己在做什么。 任何挥之不去的精灵引用都会导致大量内存泄漏。这个 deep 选项将迭代所有精灵并移除它们对该精灵列表的引用。出于性能原因,Sprite和SpriteList具有循环引用。

property color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]#

获取或设置精灵列表颜色。这将影响列表中的所有精灵。还可以为各个精灵指定一种颜色。这些颜色被转换为浮点颜色(0.0->1.0)并相乘在一起。

精灵的最终颜色是::

texture_color * sprite_color * spritelist_color
返回类型

Color

property color_normalized: Tuple[float, float, float, float]#

获取或设置标准化形式的精灵列表颜色(0.0->1.0浮点数)。此属性的工作方式与 color

disable_spatial_hashing() None[源代码]#

关闭空间哈希。

draw(*, filter=None, pixelated=None, blend_function=None)[源代码]#

画出这张精灵的名单。

参数
  • filter -- 设置OpenGL滤镜的可选参数,如 gl.GL_NEAREST 以避免平滑。

  • pixelated -- True 对于像素化和 False 用于平滑内插。用于设置Filter=GL_NEAREST的快捷方式。

  • blend_function -- 设置用于绘制精灵列表的OpenGL混合函数的可选参数,例如‘arcade.Window.ctx.BLEND_Additive’或‘arcade.Window.ctx.BLEND_DEFAULT’

draw_hit_boxes(color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]] = (0, 0, 0, 255), line_thickness: float = 1)[源代码]#

绘制此列表中的所有命中框

enable_spatial_hashing(spatial_hash_cell_size=128)[源代码]#

启用空间散列。

extend(sprites: Union[list, arcade.sprite_list.sprite_list.SpriteList])[源代码]#

使用给定列表扩展当前列表

参数

sprites (list) -- 要添加到列表中的Sprite列表

property geometry: arcade.gl.vertex_array.Geometry#

返回该精灵列表的内部OpenGL几何体。这可用于使用精灵列表数据执行自定义着色器。

必须在顶点着色器中定义以下一项或多项输入:

in vec2 in_pos;
in float in_angle;
in vec2 in_size;
in float in_texture;
in vec4 in_color;
index(sprite: arcade.sprite.Sprite) int[源代码]#

返回精灵列表中精灵的索引

参数

sprite (Sprite) -- 要查找并返回其索引的Sprite

返回类型

int

initialize()[源代码]#

创建内部OpenGL资源。如果精灵列表是懒惰的,或者是在窗口/上下文之前创建的,则可以这样做。如果不调用此方法,则将在第一次抽出时进行初始化。这对于大多数人来说是可以接受的,但此方法使您能够预初始化以在渲染过程中潜在地取消初始停顿。

否则,就不会有任何效果。在另一个线程中调用此方法将导致OpenGL错误。

insert(index: int, sprite: arcade.sprite_list.sprite_list._SpriteType)[源代码]#

在给定索引处插入子画面。

参数
  • index (int) -- 要插入的索引

  • sprite (Sprite) -- 要插入的精灵

move(change_x: float, change_y: float) None[源代码]#

以相同的量移动列表中的所有精灵。根据精灵列表的大小,这可能是一个非常昂贵的操作。

参数
  • change_x (float) -- 更改所有x值的数量

  • change_y (float) -- 更改所有y值的数量

on_update(delta_time: float = 0.016666666666666666)[源代码]#

更新子画面。类似于更新,但也需要增量时间。

pop(index: int = - 1) arcade.sprite.Sprite[源代码]#

从列表中弹出最后一个子画面或给定的索引

参数

index (int) -- 要删除的精灵的索引,最后一项的默认索引为-1。

preload_textures(texture_list: List[Texture]) None[源代码]#

预加载将用于此精灵列表中的精灵的一组纹理。

参数

texture_list (array) -- 纹理列表。

remove(sprite: arcade.sprite_list.sprite_list._SpriteType)[源代码]#

从列表中删除特定的精灵。:param Sprite Sprite:要从列表中删除的项目

rescale(factor: float) None[源代码]#

相对于精灵列表中心重新缩放列表中的所有精灵。

reverse()[源代码]#

就地反转当前列表

shuffle()[源代码]#

就地混洗当前列表

sort(*, key=None, reverse: bool = False)[源代码]#

使用以下命令对精灵列表进行就地排序 < 精灵之间的比较。此函数类似于PYTHON list.sort

使用lambda::根据y轴位置对子画面进行排序的示例

# Normal order
spritelist.sort(key=lambda x: x.position[1])
# Reversed order
spritelist.sort(key=lambda x: x.position[1], reverse=True)

使用函数对精灵进行排序的示例::

# More complex sorting logic can be applied, but let's just stick to y position
def create_y_pos_comparison(sprite):
    return sprite.position[1]

spritelist.sort(key=create_y_pos_comparison)
参数
  • key -- 将子画面作为返回比较键的参数的函数

  • reverse (bool) -- 如果设置为 True 精灵将以相反的方式排序

swap(index_1: int, index_2: int)[源代码]#

按索引交换两个精灵:param int index_1:要交换的项目索引:param int index_2:要交换的项目索引

update() None[源代码]#

对列表中的每个精灵调用update()方法。

update_angle(sprite: arcade.sprite.Sprite)[源代码]#

由Sprite类调用以更新此子画面中的角度。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

update_animation(delta_time: float = 0.016666666666666666)[源代码]#

在精灵列表中的每个精灵中调用UPDATE_动画。

update_color(sprite: arcade.sprite.Sprite) None[源代码]#

由Sprite类调用以更新指定的Sprite的位置、角度、大小和颜色。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

update_height(sprite: arcade.sprite.Sprite)[源代码]#

由Sprite类调用以更新此子画面中的大小/比例。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

update_location(sprite: arcade.sprite.Sprite)[源代码]#

由Sprite类调用以更新此子画面中的位置。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

update_position(sprite: arcade.sprite.Sprite) None[源代码]#

设置在SpriteList中添加或插入的Sprite的初始位置时调用。

update_location 一旦精灵出现在列表中,就应该调用它来移动它们。

参数

sprite (Sprite) -- 要更新的Sprite。

update_size(sprite: arcade.sprite.Sprite) None[源代码]#

由Sprite类调用以更新此子画面中的大小/比例。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

update_texture(sprite) None[源代码]#

确保我们为下一批绘制更新此子画面的纹理

update_width(sprite: arcade.sprite.Sprite)[源代码]#

由Sprite类调用以更新此子画面中的大小/比例。批量绘制项目所必需的。

参数

sprite (Sprite) -- 要更新的Sprite。

property use_spatial_hash: bool#

控制此子画面列表是否使用空间哈希的布尔变量。如果启用了空间散列,则移动一个精灵所需的时间较长,而查看该精灵是否与另一个精灵发生碰撞所需的时间较短。

property visible: bool#

获取或设置此精灵列表的可见标志。如果可见的是 False 这个 draw() 没有任何效果。

返回类型

bool

write_sprite_buffers_to_gpu() None[源代码]#

确保调整缓冲区大小,并将新的精灵数据写入内部精灵缓冲区。

这是自动调用的 SpriteList.draw() ,但在使用自定义着色器时,我们需要强制执行此操作,因为我们可能尚未调用 SpriteList.draw() 因为精灵列表被修改了。

如果您添加、删除、移动或更改了任何精灵属性,此方法将同步GPU端的数据(缓冲区大小调整和写入新数据)。