如何确保精灵按我的喜好顺序放置在前景/中间地带/背景中?

2024-09-25 02:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我已经为一个pygame游戏编写了一些代码,并在屏幕上发布了一些图片,还有一些是通过SpriteGroup添加的。在我的主线中,这是按程序顺序进行的——然而,从正面到背面的图像顺序似乎在某种程度上是随机的。你知道吗

检查了代码之后,我注意到冲突的图像都在同一个SpriteGroup中。你知道吗

是否可以定义SpriteGroup中图像的顺序?你知道吗


Tags: 代码图像游戏定义屏幕顺序图片pygame
1条回答
网友
1楼 · 发布于 2024-09-25 02:35:20

是的,但是您必须使用^{}组:

class pygame.sprite.LayeredUpdates

LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -> LayeredUpdates

This group is fully compatible with pygame.sprite.SpriteSimple base class for visible game objects..

You can set the default layer through kwargs using ‘default_layer’ and an integer for the layer. The default layer is 0.

If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.

相关问题 更多 >