首页 文章

Pygame - 一组精灵的数量

提问于
浏览
1

如何确定组中有多少精灵? len()应该工作,但只是......不行 . 码:

print(len(sprites))
print('sprites',sprites)

输出:

0
('sprites', <Group(1 sprites)>)

(是的,我确实创建了一个名为'sprites'的小组)

编辑:我将“sprites”重命名为“aliveSprites”,以防万一 . 没运气 . 这是代码:

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)

2 回答

  • 3

    尝试(看起来很难看)

    len(sprites.sprites)
    

    Pygame Sprites Group确实支持len操作 .

    pygame.sprite.Group
    

    Sprite对象的简单容器 . 可以继承此类以创建具有更多特定行为的容器 . 构造函数将任意数量的Sprite参数添加到Group中 . 该组支持以下标准Python操作:

    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
    
  • 2

    你需要调用sprites()来获取精灵列表

    print(len(sprites.sprites()))
    

相关问题