pygame:使用 sprite.RenderPlain 绘制精灵组的顺序 [英] 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.
但是,即使使用运算符模块函数 (sorted(self.sprites, key=attrgetter('y','x')) 对组进行排序时,顺序也是错误的.
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?
推荐答案
直截了当,你可以't:
Group 不以任何顺序保存精灵,所以绘制顺序是随意.
The Group does not keep sprites in any order, so the draw order is arbitrary.
改用 OrderedUpdates 组:
这个类派生自 pygame.sprite.RenderUpdates
- Group 类跟踪脏更新.它保持精灵的顺序添加到组进行渲染.这使得添加和删除来自 Group 的 Sprite 比普通 Groups 慢一点.
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.
这篇关于pygame:使用 sprite.RenderPlain 绘制精灵组的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!