Removing objects

programus Programus at gmail.com
Wed Jan 23 22:01:05 EST 2008


On Jan 23, 2:59 pm, bladedpeng... at gmail.com wrote:
> I am writing a game, and it must keep a list of objects. I've been
> representing this as a list, but I need an object to be able to remove
> itself. It doesn't know it's own index. If I tried to make each object
> keep track of it's own index, it would be invalidated when any object
> with a lower index was deleted.  The error was that when I called
> list.remove(self), it just removed the first thing in hte list with
> the same type as what I wanted, rather than the object I wanted. The
> objects have no identifying charachteristics, other than thier
> location in memory
>
> So my question: How do I look something up in a list by it's location
> in memory? does python even support pointers?
>
> Is there a better way?

How about using pygame.sprite? ( http://www.pygame.org/docs/ref/sprite.html
)
The class pygame.sprite.Sprite has a kill() function, which can remove
itself from all groups that contains it. (A group is just like a list.
Just see pygame.sprite.Group)

And here is a short tutorial about this.
http://www.pygame.org/docs/tut/SpriteIntro.html



More information about the Python-list mailing list