[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 6:07 AM, Ben Collier <bmcollier@xxxxxxxxx> wrote:
Hi Luke,

Thanks for that. From the code you've got there it looks like a list should work fine. I haven't got the code I'd been trying to hand (essentially assigning a new sprite to the list, but it sounds as though it was a python problem, not pygame)
 
If you want to add a new sprite to a list, you can simply use the "append" method of the list.
a = [1,2,3,4,5]
a.append(6)
print a

results in
[1,2,3,4,5,6]

Also look at "extend",
a = [1,2,3,4,5]
b = [6,7,8]

print a.append(b)
[1,2,3,4,5,[6,7,8]]

print a.extend(b)
[1,2,3,4,5,6,7,8]

So extend is for tacking a list to the end of a list, and append is for adding an element (which can be a list if you want.)

I shouldn't have any problems with this from now on. Thanks very much. Fingers crossed!

From your code, you could reference sprites[5] to get the sixth sprite, right?
Yep, exactly.  If you have names for your sprites, you can throw them in a dictionary instead, so you would be able to reference them as:

sprites['spaceship'].x = 15

or whatever, but if they're just 150 bullets, for example, and you want to move them to the right on the screen, it'd be easier just to use a list and say
for bullet in bullets:
    bullet.x += 5

And of course there's nothing keeping you from having a dictionary of lists, so you could do
sprites['spaceship'].x = 15
for bullet in sprites['bullets']:
    bullet.x += 5

Or whatever.
(I'm not sure if this is how you control sprite's positions, I don't use Sprite very much and I mostly use Pyglet these days (but that will change again soon.))

Hope that helps,
-Luke

P.S. if you're not too familiar with Python or just starting out, come join the Python Tutor mailing list, I'm a contributor over there and we talk about a lot of general Python stuff (and not just really low-level stuff either, some of the stuff they get into I don't even understand!)  If you need some great, free materials to learn, just ask over there and everyone will be happy to direct you to the best resources available for starting out!
 

Thanks

Ben

2009/10/26 Luke Paireepinart <rabidpoobear@xxxxxxxxx>



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