def build_sprite_surface(self):
"""
Builds the image (pygame.Surface) for this tile layer based on all found tiles in the layer.
"""
surf = pygame.Surface((self.pytmx_layer.width * self.pytmx_tiled_map.tilewidth, self.pytmx_layer.height * self.pytmx_tiled_map.tileheight),
flags=pygame.SRCALPHA)
# rendered collision layer
if self.type != Sprite.get_type("none") and (DEBUG_FLAGS & DEBUG_RENDER_COLLISION_TILES):
# red for normal collisions, light-blue for touch collisions
color = DEBUG_RENDER_COLLISION_TILES_COLOR_DEFAULT if self.type & Sprite.get_type("default") else DEBUG_RENDER_COLLISION_TILES_COLOR_OTHER
for (x, y, image), (_, _, gid) in zip(self.pytmx_layer.tiles(), self.pytmx_layer.iter_data()):
surf.blit(image.convert_alpha(), (x * self.pytmx_tiled_map.tilewidth, y * self.pytmx_tiled_map.tileheight))
tile_props = self.pytmx_tiled_map.get_tile_properties_by_gid(gid) or {}
# normal collision tiles
if not tile_props.get("no_collision"):
pygame.draw.rect(surf, color, pygame.Rect((x * self.pytmx_tiled_map.tilewidth, y * self.pytmx_tiled_map.tileheight),
(self.pytmx_tiled_map.tilewidth, self.pytmx_tiled_map.tileheight)), 1)
# "normal" layer (and no debug rendering)
else:
for x, y, image in self.pytmx_layer.tiles():
surf.blit(image.convert_alpha(), (x * self.pytmx_tiled_map.tilewidth, y * self.pytmx_tiled_map.tileheight))
pygame_sprite = pygame.sprite.Sprite()
pygame_sprite.image = surf
pygame_sprite.rect = surf.get_rect()
return pygame_sprite
评论列表
文章目录