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

Re: [pygame] Creating large numbers of sprites in array or other structure





On Mon, Oct 26, 2009 at 4:43 AM, Ben Collier <bmcollier@xxxxxxxxx> wrote:
Hi,

I'm fairly new to pygame, certainly haven't used it much, and up until now have been creating sprites by defining an extension to the sprite class and then instantiating each sprite by assigning it as, for example:

newsprite = NewSprite(250,250)

...with the arguments that the init method uses in brackets.

If I want to create, say, 150 sprites, and simply add each one to a group rather than having to have a distinct reference for each one, how do I do it? I can't seem to find an example online and I've not had much luck adding them to an array.
 
Why have you not been able to add them to a list?  (by the way, we call them "lists" not arrays in Python)
Can you not just say
locations = [(250, 250), (100,100), (300, 300)]
sprites = [NewSprite(location) for location in locations]
spritegroup = NewSpriteGroup()
for sprite in sprites:
    spritegroup.add(sprite)

Note this is pseudocode as I don't remember the exact syntax to initialize sprites, etc. but you should get the idea.  Is there a problem doing it this way?

(In the future, it would be better to say "I have tried it this way, and it did not work because" or "i have tried it this way and I am not sure why it did not work but here is what it did" or something like this, so we have some indication of what you have tried, and why it didn't work.
See http://catb.org/~esr/faqs/smart-questions.html )

-Luke