def add_sprite(self, sprite, group_name):
"""
Adds a new single Sprite to an existing or a new pygame.sprite.Group.
:param Sprite sprite: the Sprite to be added to this Stage (the Sprite's position is defined in its rect.x/y properties)
:param str group_name: the name of the group to which the GameObject should be added (group will not be created if it doesn't exist yet)
:return: the Sprite that was added
:rtype: Sprite
"""
# if the group doesn't exist yet, create it
if group_name not in self.sprite_groups:
self.sprite_groups[group_name] = pygame.sprite.Group()
sprite.stage = self # set the Stage of this GameObject
self.sprite_groups[group_name].add(sprite)
self.sprites.append(sprite)
sprite.sprite_groups.append(self.sprite_groups[group_name])
# add each single Sprite to the sorted (by render_order) to_render list and to the "all"-sprites list
# - note: the to_render list also contains entire TiledTileLayer objects
if sprite.do_render:
self.to_render.append(sprite)
self.to_render.sort(key=lambda x: x.render_order)
# trigger two events, one on the Stage with the object as target and one on the object with the Stage as target
self.trigger_event("added_to_stage", sprite)
sprite.trigger_event("added_to_stage", self)
return sprite
评论列表
文章目录