I am a new user of PyGame and python in general. That being said, I have created some sprites and put them in a group. For example,
gems = pygame.sprite.Group()
for i in range (0,4):
gem = Gem()
all_sprite_list.add(gem)
gems.add(gem)
Now in my main I would like to manipulate one of the gem's in the group by hitting a specific key. I am currently accomplishing this by setting an id attribute to each gem and iterating through the group until I match the id with the one I want to manipulate. i.e.:
if event.type == pygame.KEYDOWN and event.key == pygame.K_F1:
for gem in gems:
if gem.id == 1:
gem.state = normal
elif event.type == pygame.KEYDOWN and event.key == pygame.K_F2:
for gem in gems:
if gem.id == 2:
gem.state = normal
I know this is a dirty dirty trick and I'm sure there is a better way instead of iterating through the group. I looked at the references and tried doing something like
gems.sprites().index(0)
with no avail. Any suggestions?