Skip to main content
7 events
when toggle format what by license comment
May 2, 2011 at 16:39 comment added user744 if all you need is a single arbitrary sprite from the group, you can use iter(grp).next() and be ready to catch StopIteration if it's empty. But I honestly can't think of a use case for grabbing an arbitrary sprite from a group.
May 2, 2011 at 15:53 comment added user3956 Understood and agreed. Note that what I want to do is take any single sprite from the group. The code above does that and is not an aberration. If there's a better solution to achieve this, I will accept it. (in the question I mention a "position" or index but it's not important actually, getting any single sprite from the group is enough) I'm wondering if your answer to the linked question (shuffle group) would not work for this purpose as well...
May 2, 2011 at 14:25 comment added user744 Removing a sprite is guaranteed, at the very least, to shift all the objects past it down by one. Removing many elements may make the dictionary shrink, rehashing. Adding elements is guaranteed to make the dictionary grow, rehashing. Indices into the key view/list - which are what you get when you make indices into the sprite group - are not reliable.
May 2, 2011 at 14:18 comment added user3956 hmm, it looks very well ordered to me. As a test I tried removing a sprite from its group (and printing the sprites names in the group before and after removing it) the sprites order is kept and I can get any sprite inside the group by "position" with the code above... but I understand a sprite group is here to keep all the "similar" sprites together for easier update and not to index them.
May 2, 2011 at 14:03 comment added user744 Groups are not ordered so "the first sprite in the group" is a useless designator. What you probably actually want is "the player sprite" or "the enemy ship" or something. Store those when you make them, don't rely on being able to pull them back out of a group by the weakest handle imaginable.
May 2, 2011 at 13:58 comment added user3956 I'm not trying to index sprites inside a group. I just want to get the first or second sprite in the group, whatever it is and whenever it has been added to the group. (thanks for return None)
May 2, 2011 at 13:37 history answered user744 CC BY-SA 3.0