Duplicating A Sprite
I'm having trouble with getting new sprites to be added. I'm looking for something along the lines of: def duplicate(sprites): for d in sprites: if d.energy >= d.max
Solution 1:
In general, you need to implement the duplicate method and construct a new instance of the Sprite object in the method.
Another solution is to use the Python copy module. deepcopy can create a deep copy of an object. Unfortunately this cannot be used for pygame.sprite.Sprite objects, as theimage attribute is a pygame.Surface, which cannot be copied deeply. Therefore, a deepcopy of a Sprite will cause an error.
Unless you have nor any other attribute that needs to be copied deeply, you can make a shallow copy of the Sprite. The rect attribute is a pygame.Rect object. The copy of the Sprite needs its own rectangle, so you have to generate a new rectangle instance. Fortunately a pygame.Rect object can be copied by pygame.Rect.copy:
importcopynew_d = copy.copy(d)
new_d.rect = d.rect.copy()
Post a Comment for "Duplicating A Sprite"