Python Pygame-组中精灵的数量
如何确定一组中有多少精灵?len()应该可以用,但是。。。不。 代码: 输出:Python Pygame-组中精灵的数量,python,pygame,Python,Pygame,如何确定一组中有多少精灵?len()应该可以用,但是。。。不。 代码: 输出: 0 ('sprites', <Group(1 sprites)>) 试试看(尽管看起来很难看) Pygame Sprites组不支持len操作 pygame.sprite.Group 精灵对象的简单容器。可以继承该类来创建具有更具体行为的容器。构造函数接受任意数量的Sprite参数以添加到组中。该组支持以下标准Python操作: in test if a Sprite is containe
0
('sprites', <Group(1 sprites)>)
试试看(尽管看起来很难看)
Pygame Sprites组不支持len操作
pygame.sprite.Group
精灵对象的简单容器。可以继承该类来创建具有更具体行为的容器。构造函数接受任意数量的Sprite参数以添加到组中。该组支持以下标准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
你需要打电话获取精灵列表
print(len(sprites.sprites()))
奇怪的是,.sprites给了我一个“TypeError:类型为'instancemethod'的对象没有len()”,但是.sprites()对输出没有影响…我建议更改您的组名,并作为第一步尝试。如果这仍然不起作用,你可以尝试一些不太推荐的东西,比如len(精灵[1]),考虑到群组是一个名称和精灵列表。我尝试过更改名称-见原始帖子上的编辑-但没有成功。显然-我只是尝试了这个-[1]或[0]或其他任何东西(我猜它叫索引)对组不起作用,并生成错误。另外,我检查了-只要我在组中使用.sprites(),它就乱七八糟了(总是有0个项目,不管原始内容如何。)所以[1]只会产生一个“列表索引超出范围”错误。。。我不知道该怎么办。(我是pygame新手)等等!我刚弄明白那是什么。没有关系。很抱歉
pygame.sprite.Group
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
print(len(sprites.sprites()))