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

Re: [pygame] pygame.sprite.Group problem



I think this may be the clue:

http://www.pygame.org/docs/ref/sprite.html#Group.sprites

I think in Python2.6 and later, it will grab an Iterator, rather than a list... I'm not sure. Try doing lst = enemiesGroup.sprites(), then random.choice(lst)

On Wed, Aug 26, 2009 at 6:54 PM, Yanom Mobis <yanom@xxxxxxxxxxxxxx> wrote:
so, here is my main function:


def main()
    enemiesGroup.add(EnemyThingie("enemy.png", random.randint(-100, 100),random.randint(-100, 100), 4, 4))  #this error gets raised if the game isn't started  with one enemy
    print enemiesGroup.sprites()

    while 1:
        clock.tick(10)
        for event in pygame.event.get():
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                exit()
        for x in range(0, EnemiesPerFrame):
            enemiesGroup.add(EnemyThingie("enemy.png", random.randint(-100, 100),random.randint(-100, 100), 4, 4))
        screen.fill((0,0,0))
        towersGroup.draw(screen)
        for item in towersGroup:
            pick = random.choice(enemiesGroup.sprites())
            item.shoot(pick, screen)
            pick.kill() #replace this with a .take_damage() function later. even better, build that part into shoot() func.
        enemiesGroup.draw(screen)
        for item in enemiesGroup:
            item.moveEnemyThingie()
        pygame.display.flip()
    return 0

problem is, i run it and get this:

[<EnemyThingie sprite(in 1 groups)>, <EnemyThingie sprite(in 1 groups)>]
Traceback (most recent call last):
  File "Game.py", line 94, in <module>
    if __name__ == '__main__': main(1)
  File "Game.py", line 85, in main
    pick = random.choice(enemiesGroup.sprites())
  File "/usr/lib/python2.6/random.py", line 261, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range


The error at the bottom tells me it thinks there are no sprites in enemiesList group, but the print() statement output at the top shows you there are two.




--
Visit my blog at http://oddco.ca/zeroth/zblog