[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] get all sprites?



Gerrit Holl wrote:
I have a 'Person' object, a subclass of Sprite, which should 'fall' (go
down) if no 'Wall' object (also a subclass of Sprite) is immediatly below.
I do have a group of all wall objects, but in a different module. Is it
necessary to give the 'falling' method of the Person instance (which checks
whether to fall or not) a Group instance containing all walls, or is there a
function which returns a Group instance containing *all* sprites (which I can
loop over to check for walls)?
you are correct, there is no way to do this. you are definitely on the right track by using multiple groups. and each sprite can be a member of multiple groups.

one of the nice benefits of using the groups properly is that there is no need to check through 'all' sprites looking for certain types. you can keep a group of exactly what you want and only examine them. this works faster in python, since there is no need to iterate and examine sprites that will not be needed.

of course, the tradeoff is that you end up needing to do a bit of management yourself. but the benefit is you can take it even further. if your "walls" are divided into logical areas, you could create subgroups for each wall area. then you only need to test the player collision against the area he is in.

if you look at the 'aliens' example, you see that for each type of sprite there is actually a class attribute listing the different groups the class instance should belong to. by doing it that way, you can just create instances of sprites anytime you want, and it will automatically know which groups to become a member of. that is probably easier than keeping a list of groups around everytime you want to create a new instance of a sprite.

sorry there's no quick and easy answer there. good luck with those platforms though!



____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org