[Tutor] Iterating backwards

Kent Johnson kent37 at tds.net
Fri Jan 11 18:22:07 CET 2008

James Newton wrote:

> I have created a series of pygame sprites.  Visually, these sprites
> represent counters and may overlap.  I want to determine which sprite
> the user clicked on.
> This seems to me to be a fairly standard feature of a game interface, so
> I imagine that there is already a standard technique for achieving it. 

I would think so too, but a quick look at the docs and tutorials didn't 
turn up anything....

> I'd like to start at the top-most sprite and work backwards
> through the sprites in the group, and then use "break" to stop as soon
> as I find a sprite whose rect collides with the mouse position.
> In my code below, I've tried replacing the line...
>     for vCounter in counters: # the pygame.sprite.RenderPlain() group
> ... with:
>     vRange = range(len(counters))
>     vRange.reverse()
>     for ii in vRange:
>         vCounter = counters[ii]
> However this results in an error:
>     #TypeError: 'Group' object is unindexable

I guess counters is iterable but not a sequence. Try this:
   for vCounter in reversed(list(counters)):


More information about the Tutor mailing list