Pygame - 组中的精灵数量 [英] Pygame - number of sprites in a group
问题描述
如何确定一个组中有多少个精灵?len() 应该工作,但只是......没有.代码:
How do I determine how many sprites are in a group? len() should work, but just... doesn't. Code:
print(len(sprites))
print('sprites',sprites)
输出:
0
('sprites', <Group(1 sprites)>)
(是的,我确实创建了一个名为精灵"的组)
(Yes, I did make a group called 'sprites')
我将精灵"重命名为aliveSprites",以防万一这是问题所在.没有运气.代码如下:
I renamed "sprites" to "aliveSprites", just in case that was the issue. No luck. Here's the code:
print(len(aliveSprites.sprites()))
if len(aliveSprites.sprites()) == 0:
thing = test()
aliveSprites.add(thing)
thing.rect.x = 100
thing.rect.y = 300
print('sprites',aliveSprites)
推荐答案
尝试(看起来很丑)
len(sprites.sprites)
Pygame Sprites Group 确实支持 len 操作.
Pygame Sprites Group does support the len operation.
pygame.sprite.Group
一个简单的 Sprite 对象容器.可以继承这个类来创建具有更具体行为的容器.构造函数将任意数量的 Sprite 参数添加到组中.该组支持以下标准 Python 操作:
A simple container for Sprite objects. This class can be inherited to create containers with more specific behaviors. The constructor takes any number of Sprite arguments to add to the Group. The group supports the following standard Python operations:
in test if a Sprite is contained
len the number of Sprites contained
bool test if any Sprites are contained
iter iterate through all the Sprites
这篇关于Pygame - 组中的精灵数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!