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

Re: [pygame] Referencing instances by memory address



On 2010.7.12 7:09 PM, éæå wrote:
I was storing them by id() because it was an easy unique number to use
and meant that I wouldn't need to dynamically generate unique numbers
for each instance. The main thing I need them for is referencing
individual bullets for collision detection. I also need to delete the
dictionary reference, because otherwise, collision detection with no
longer "existing" bullets continues, causing a sort of invisible barrier
for bullets in the future.

Have you considered using a list rather than a dictionary? It'd work like this:

for bullet in list_of_bullets:
  bullet.keep = checkforcollision() ## Sets a flag on each bullet
  if bullet.time_left == 0:
    bullet.keep = False
list_of_bullets = [b for b in list_of_bullets if b.keep]

The last line there would be needed because you can get errors if you remove entries from a list while iterating through it.