[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: HELP PLEASE: sprite collision - object not iterable error



On Wed, Feb 22, 2012 at 2:32 AM, Scott <scott@xxxxxxxxxxxxxxx> wrote:
> I replaced Monster with MGroup
> and Player with PGroup. Error was -
[...]
>    spritecollide = sprite.rect.colliderect
> AttributeError: 'Group' object has no attribute 'rect'
>
> I guess I'm misusing the PGroup/MGroup? Any advice? Thanks in advance.

There are four relevant functions in the sprite module: collide_rect,
spritecollide, groupcollide and spritecollideany:

collide_rect is for checking for a collision between a sprite and
another sprite (returns bool)
spritecollide is for checking for a collision between a sprite and a
group (returns a list of sprites from the group)
groupcollide is for checking for a collision between a group and
another group (returns a dict of lists of sprites)
spritecollideany is for checking for a collision between a sprite and
a group when you don't care *which* sprite(s) in the group collided,
just that *any* did (returns bool)

So you could do:

Returning a bool:
pygame.sprite.collide_rect(monster, player)
pygame.sprite.spritecollideany(player, MGroup)
pygame.sprite.spritecollideany(monster, PGroup)

Returning a collection saying what collided:
pygame.sprite.spritecollide(player, MGroup)
pygame.sprite.spritecollide(monster, PGroup)
pygame.sprite.groupcollide(PGroup, MGroup)