如何访问 Python 中 groupcollide() 返回的列表中的每个项目属性?

How to access each items attributes in a list returned by groupcollide() in Python?

# Variable containing a list of collided objects.
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)

# I want to access on of the collided objects (aliens) attribute (aliens.points)

if collisions:
    for aliens in collisions.values():
        print(aliens)
        # print(collisions.values()) returns: dict_values([[<Alien Sprite(in 0 groups)>]])
        print(aliens.points) # doesnt work
        

当前终端输出:

[<Alien Sprite(in 0 groups)>]

终端错误输出:

AttributeError: 'list' object has no attribute 'points'

groupcollide 是这样工作的:

Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.

所以你可以做到

for bullet in collisions: # each bullet
    for alien in collision[bullet]: # each alien that collides with that bullet
        print(alien.points)