Reputation: 601
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)
Output:
0
('sprites', <Group(1 sprites)>)
(Yes, I did make a group called 'sprites')
EDIT: 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)
Upvotes: 3
Views: 6652
Reputation: 304355
You need to call sprites() to get the list of sprites
print(len(sprites.sprites()))
Upvotes: 4
Reputation: 767
Try (as ugly as it looks)
len(sprites.sprites)
Pygame Sprites Group does support the len operation.
pygame.sprite.Group
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
Upvotes: 3