Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame: drawing order for sprite group with sprite.RenderPlain

I've got a sprite group which needs to be drawn in a certain order so its sprites overlap as they should.

However even when sorting the group using operator module function (sorted(self.sprites, key=attrgetter('y','x')) the order is wrong.

How can I fix this behaviour?

like image 592
Hedge Avatar asked Feb 18 '23 07:02

Hedge


1 Answers

Straightforwardly, you can't:

The Group does not keep sprites in any order, so the draw order is arbitrary.

Use an OrderedUpdates group instead:

This class derives from pygame.sprite.RenderUpdates - Group class that tracks dirty updates. It maintains the order in which the Sprites were added to the Group for rendering. This makes adding and removing Sprites from the Group a little slower than regular Groups.

Alternatively, you can keep different 'layers' of sprites in different groups, keeping the order of groups correct.

like image 99
9000 Avatar answered Feb 21 '23 00:02

9000