一文详解pygame.sprite的精灵碰撞

 更新时间:2023年01月24日 10:27:56   作者:peanutfish  
精灵其实在一个游戏程序中,精灵本质指的是一张张小尺寸的图片,比如游戏中的各种道具、人物、场景装饰等,它们都可以看做成一张张小的“精灵”图,下面这篇文章主要给大家介绍了关于pygame.sprite精灵碰撞的相关资料,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

前言

pygame中的精灵碰撞是可见游戏中用的最基础的东西,这里结合官方文档和小甲鱼的网站上的内容做个小总结,方便日后使用。

pygame.sprite.Sprite - 可见游戏对象的简单基类。

Sprite(*groups) -> Sprite

  • pygame.sprite.Sprite.add - 将精灵添加到组中
  • pygame.sprite.Sprite.remove - 从组中删除精灵
  • pygame.sprite.Sprite.kill - 从所有组中删除Sprite
  • pygame.sprite.Sprite.alive - 精灵属于任何组的检测
  • pygame.sprite.Sprite.groups - 包含此Sprite的组列表
  • pygame.sprite.Sprite.update - 控制精灵行为的方法

pygame.sprite.Group - 用于保存和管理多个 Sprite 对象的容器类。

Group(*sprites) -> Group

  • pygame.sprite.Group.sprites - 此组包含的 Sprite 列表
  • pygame.sprite.Group.copy - 复制组
  • pygame.sprite.Group.add - 将 Sprite 添加到此组
  • pygame.sprite.Group.remove - 从组中删除 Sprite
  • pygame.sprite.Group.has - 测试一个 Group 是否包含 Sprite
  • pygame.sprite.Group.update - 在包含的 Sprite 上调用 update 方法
  • pygame.sprite.Group.draw - blit Sprite 的图像
  • pygame.sprite.Group.clear - 在 Sprites 上绘制背景
  • pygame.sprite.Group.empty - 删除所有 Sprite`

上面两个基类是pygame中最常用,相当轻量级,只为大多数游戏常见的代码提供了一个起始点。

Sprite 类旨在用作游戏中不同类型对象的基类,为我们碰撞检测做准备。还有一个基本的 Group 类,它只存储 sprite 对象, 这样方便不同类型的精灵进行碰撞检测, 通常的操作 in / len / bool / iter 都对这个group使用。

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

pygame.sprite.spritecollide() - 在与另一个精灵相交的组中查找精灵

spritecollide(sprite, group, dokill, collided = None) -> Sprite_list

  • 返回一个sprite列表,其中包含与另一个 Sprite 相交的 Group 中的所有 Sprite 。 通过比较每个 Sprite 的 Sprite.rect 属性来确定交集。
  • dokill 参数是一个布尔值。如果设置为 True,则将从组中删除所有碰撞的 Sprite 。
  • collided 碰撞参数是一个回调函数,用于计算两个精灵是否发生碰撞。 它应该将两个精灵作为值,并返回一个 bool 值,指示它们是否发生碰撞。 如果未传递碰撞,则所有精灵必须具有 rect 值,该值是 sprite 区域的矩形,将用于计算碰撞。

可用的回调函数

collide_rect, collide_rect_ratio, collide_circle,
collide_circle_ratio, collide_mask
  • pygame.sprite.collide_rect - 使用 rects 检测两个精灵之间的碰撞。
    • Collision detection between two sprites, using rects. 使用函数pygame rect colliderect检测碰撞并将结果返回给*collide, 精灵必须具有 ‘rect’ 属性

collide_rect(left, right) -> bool

  • pygame.sprite.collide_rect_ratio - 使用按比例缩放的 rects 检测两个精灵之间的碰撞。
    • Collision detection between two sprites, using rects scaled to a ratio. 使用 ratio 创建,然后将实例作为碰撞回调函数传递给 *collide 函数。
      ratio 是浮点数 - 1.0 是相同的大小,2.0 是两倍大,0.5 是大小的一半。

collide_rect_ratio(ratio) -> collided_callable

  • pygame.sprite.collide_circle - 使用圆圈检测两个精灵之间的碰撞。
    • *Collision detection between two sprites, using circles.*测试两个精灵之间的碰撞,通过测试以查看精灵中心的两个圆是否重叠。

如果精灵具有 radius(半径) 属性,用于创建圆,否则会创建一个足够大的圆,以完全包围由 rect 属性给出的精灵矩形。作为碰撞回调函数传递给 *collide 函数。精灵必须具有 rect 和可选的 radius 属性

请添加图片描述

collide_circle(left, right) -> bool

  • pygame.sprite.collide_circle_ratio - 使用按比例缩放的圆圈检测两个精灵之间的碰撞。
    • Collision detection between two sprites, using circles scaled to a ratio. 使用浮点数 ratio 创建,然后将实例作为碰撞回调函数传递给 *collide 函数。ratio 是浮点数 - 1.0 是相同的大小,2.0 是两倍大,0.5 是大小的一半。

两个精灵之间的碰撞创建的可调用测试,通过测试以查看以精灵为中心的两个圆是否重叠,在通过存储的比例缩放圆半径之后。如果精灵具有 radius 半径属性,用于创建圆,否则会创建一个足够大的圆,以完全包围由 rect 属性给出的精灵矩形。打算作为碰撞回调函数传递给 *collide 函数。

精灵必须具有 rect 和可选的 radius 属性

collide_circle_ratio(ratio) -> collided_callable

  • pygame.sprite.collide_mask - 使用蒙版在两个精灵之间进行碰撞检测。
    • *Collision detection between two sprites, using masks. *返回 masks 碰撞的 mask 上的第一个点,如果没有碰撞,则返回 None 。

通过测试它们的 bitmasks( pygame.mask.Mask.overlap()) 是否重叠来测试两个精灵之间的碰撞。 如果精灵具有 mask 属性,该属性用作 mask,否则将从精灵图像创建 mask 。 作为碰撞回调函数传递给 *collide 函数。

精灵必须具有 rect 和可选的 mask 属性

如果要多次检查碰撞,应该考虑在加载时为精灵创建一个mask。这将提高性能,否则这可能是一个昂贵的功能,因为它会在每次检查碰撞时创建 mask 。

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

collide_mask(sprite1, sprite2) -> (int, int)

collide_mask(sprite1, sprite2) -> None

pygame.sprite.groupcollide - 查找在两个组之间发生碰撞的所有精灵。

  • Find all sprites that collide between two groups.
  • This will find collisions between all the Sprites in two groups. Collision is determined by comparing the Sprite.rect attribute of each Sprite or by using the collided function if it is not None.
    Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect. 像这个格式{group1_sprite: group2_sprite}
  • 如果*collide没有指定回调函数,则所有的sprite需要有rect属性

groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict

EG:
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
       # give radius and mask attribute
       self.rect = self.image.get_rect()
       self.radius = self.rect.width / 2
       self.mask = pygame.image.from_surface(self.image)


class File(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()
       self.radius = self.rect.width / 2
       self.mask = pygame.image.from_surface(self.image)
    
block_group = pygame.sprite.Group()
for i in range(5):
    block = Block(color, width, height)
    block_group.add(block)
    
    # there is another sprite group called file_group, which have same setting as block_group.
file_group = pygame.sprite.Group()
for i in range(5):
    file = File(color, width, height)
    file_group.add(file)
    
    # the collide check will like:
    hit_list1 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_rect
    hit_list2 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_rect_ratio(.75))
    hit_list3 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_circle
    hit_list4 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_circle_ratio(.25))
    hit_list5 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_mask
    # select the collided function whatever you like
    hit_list6 = pygame.sprite.spritecollide(block_group, file_group, False, False, pygame.sprite.collide_XXX)

总结

到此这篇关于pygame.sprite精灵碰撞的文章就介绍到这了,更多相关pygame.sprite精灵碰撞内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • dataframe 按条件替换某一列中的值方法

    dataframe 按条件替换某一列中的值方法

    今天小编就为大家分享一篇dataframe 按条件替换某一列中的值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python多继承原理与用法示例

    Python多继承原理与用法示例

    这篇文章主要介绍了Python多继承原理与用法,简单描述了Python多继承的相关概念、原理并结合实例形式分析了Python多继承的具体定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2018-08-08
  • Python编程实现超炫动态排序图

    Python编程实现超炫动态排序图

    这篇文章主要介绍了Python编程实现超炫动态排序图的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-10-10
  • Python实现将wav转amr,并转换成hex数组

    Python实现将wav转amr,并转换成hex数组

    这篇文章主要介绍了Python实现将wav转amr,并转换成hex数组方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 深入解析pandas数据聚合和重组

    深入解析pandas数据聚合和重组

    这篇文章主要介绍了pandas数据聚合和重组,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Python 代码性能优化技巧分享

    Python 代码性能优化技巧分享

    选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化来提高程序的执行效率
    2012-08-08
  • Python通过属性手段实现只允许调用一次的示例讲解

    Python通过属性手段实现只允许调用一次的示例讲解

    下面小编就为大家分享一篇Python通过属性手段实现只允许调用一次的示例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python fabric实现远程部署

    python fabric实现远程部署

    这篇文章主要为大家详细介绍了 python fabric实现远程部署,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 使用PDB模式调试Python程序介绍

    使用PDB模式调试Python程序介绍

    这篇文章主要介绍了使用PDB模式调试Python程序介绍,本文讲解了PDB模式的使用语法,着重讲解PDB模式下的常用命令,需要的朋友可以参考下
    2015-04-04
  • Python正则表达式匹配和提取IP地址

    Python正则表达式匹配和提取IP地址

    这篇文章主要介绍了Python正则表达式匹配和提取IP地址的实例代码,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06

最新评论

?


http://www.vxiaotou.com