sprite.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:Projects 作者: it2school 项目源码 文件源码
def __call__(self, left, right):
        """detect collision between two sprites using scaled circles

        pygame.sprite.collide_circle_radio(ratio)(left, right): return bool

        Tests for collision between two sprites by testing whether two circles
        centered on the sprites overlap after scaling the circle's 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 sprite's 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.

        """

        ratio = self.ratio
        xdistance = left.rect.centerx - right.rect.centerx
        ydistance = left.rect.centery - right.rect.centery
        distancesquared = xdistance ** 2 + ydistance ** 2

        if hasattr(left, "radius"):
            leftradius = left.radius * ratio
        else:
            leftrect = left.rect
            leftradius = ratio * 0.5 * ((leftrect.width ** 2 + leftrect.height ** 2) ** 0.5)
            # store the radius on the sprite for next time
            setattr(left, 'radius', leftradius)

        if hasattr(right, "radius"):
            rightradius = right.radius * ratio
        else:
            rightrect = right.rect
            rightradius = ratio * 0.5 * ((rightrect.width ** 2 + rightrect.height ** 2) ** 0.5)
            # store the radius on the sprite for next time
            setattr(right, 'radius', rightradius)

        return distancesquared <= (leftradius + rightradius) ** 2
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号