pygame: drawing order for sprite group with sprite.RenderPlain

孤者浪人 提交于 2021-02-19 02:38:24

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13675741/pygame-drawing-order-for-sprite-group-with-sprite-renderplain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!