def collide_simple_with_sprite(self, sprite, collision_detector):
"""
Collides a Sprite (that only obeys simple physics rules) with a TiledTileLayer and solves all detected collisions.
The Sprite needs to have the properties vx and vy, which are interpreted as the Sprite's velocity.
Ignores slopes.
:param Sprite sprite: the Sprite to test for collisions against a TiledTileLayer
:param callable collision_detector: the collision detector method to use (this is set in the Sprite's Stage's options)
"""
tile_start_x, tile_end_x, tile_start_y, tile_end_y = self.get_overlapping_tiles(sprite)
xy, v = Stage.estimate_sprite_direction(sprite)
# very simple algo: look through tile list (in no particular order) and return first tile that collides
# None if no colliding tile found
for tile_x in range(tile_start_x, tile_end_x + 1):
for tile_y in range(tile_start_y, tile_end_y + 1):
tile_sprite = self.tile_sprites[tile_x, tile_y]
if not tile_sprite:
continue
col = collision_detector(sprite, tile_sprite, collision_objects=None,
direction=xy, direction_veloc=v, original_pos=(sprite.rect.x, sprite.rect.y))
if col:
return col
return None
评论列表
文章目录