[Tutor] Fwd: A more Pythonic way to do this

D. Hartley denise.hartley at gmail.com
Wed Jul 6 01:35:58 CEST 2005


This question began on the tutor mailing list, but I am now seeing
that it is pygame-specific, so I am forwarding it here as well. I
apologize if anyone gets a duplicate post.

As it turns out, my "holder" classes are all subclasses of type
RenderClear (which it says is "basic Group class you will want to
use."  My "group" was "enemyship_sprites," which I discovered could
not be iterated over.  However, if I used
"enemyship_sprites.sprites()" instead, I *can* iterate over that - it
creates a list of all the sprites in my group (according to the docs),
so I could use this in len(enemyship_sprites.sprites()) to count the
number of enemy ships I had on the screen and so on.

So what I am trying to do is take all of the enemy ships left on the
screen that have not been killed in this particular level (i.e., when
one of them drops too low on the screen and kills the hero - a space
invaders-style game), and reset it to its own starting position for
that level.  So I created variables in my Enemy class called initialx
and initialy (given default values of 0 and then updated when the
Enemy is actually drawn on screen), and when the bad guys drop down
too low, I inserted this for loop:

                    for each in (enemyship_sprites.sprites()):
                        each.rect.centerx = Enemy.initialx
                        each.rect.centery = Enemy.initialy

Then I draw the enemyship_sprites like I always did. (Before, it just
redrew ALL the enemies for that level, effectively restarting the
level with a full screen of baddies. I want to take just the ones that
havent been killed yet and return them to their original position).

My problem at first was I was just doing  for each in
(enemyship_sprites): - which would not work because I can't iterate
over it. Now apparently I *am* iterating over the list of sprites I
have at this point (I think, if I am doing it correctly, it should be
the list of enemies which has not yet been killed on this level), and
should be returning each to its original starting position. Since I
create initialx/initialy for each enemy when the screen full of
enemies is drawn (and I know I do create a new initialx/initialy for
each one - because I had it print the initialx, initialy when the
screen was drawn and I get 40 sets of values printed for the 40
enemies on the screen), wouldnt each instance of the Enemy class store
its own initialx initialy so that when I called
                        each.rect.centerx = Enemy.initialx
                        each.rect.centery = Enemy.initialy
.. it would return each to its own starting position?

Right now, it seems to be printing them all right on top of the other,
in one enemy position.  At first I thought I was redrawing only one
enemy, but I shot it, and there seems to be another underneath it. A
lot of others, actually.  So I can't even see if it is drawing just
the ones that havent been killed yet or a whole new batch.  And,
obviously, it's not relocating them correctly :P

Am I understanding this wrong? Is it only storing the last-computed
initialx/initialy and restoring all enemies there? Should I be using
something other than pygame.sprite.RenderClear as my 'super group'? Am
I going about this in the totally wrong way?

Any pointers/suggestions/things to try would be appreciated!

Thanks,
Denise

---------- Forwarded message ----------
From: Adam Bark <adam.jtm30 at gmail.com>
Date: Jul 5, 2005 3:58 PM
Subject: Re: [Tutor] A more Pythonic way to do this
To: "D. Hartley" <denise.hartley at gmail.com>


I mean an actual group from pygame.sprite either Group or
AbstractGroup. http://www.pygame.org/docs/ref/pygame_sprite.html#Group
Check out the sprite tutorial for more info.



On 7/5/05, D. Hartley <denise.hartley at gmail.com> wrote:
> Actually, I *have* a holder superclass: but idle is telling me I cant
> iterate over it.  Is this a different kind of group?
> Thanks,
> Denise
> 
> ---------- Forwarded message ----------
> From: Adam Bark < adam.jtm30 at gmail.com>
> Date: Jul 2, 2005 6:39 AM
> Subject: Re: [Tutor] A more Pythonic way to do this
> To: "D. Hartley" <denise.hartley at gmail.com> 
> 
> 
> I haven't actually used this myself but if you check the pygame docs
> you should be able to make a group and put all of the sprites in to
> the group. It effectively creates a list of sprites which you will be 
> able to iterate over.
> 
> 
> On 7/2/05, D. Hartley <denise.hartley at gmail.com> wrote:
> > Also, I'm trying to figure out a way to do the following:
> >
> > If the enemies drop too low, right now it just takes away a life and
> > redraws a screen full of enemies (effectively restarting the level).
> >
> > I'd like to make it so that if they drop too low and you die, it just 
> > takes those ones that you havent killed already and puts them back up
> > in their original starting positions.  I can figure out how to store
> > that "initial position" data, but I dont know how to move the 
> > havent-yet-been-killed sprites back there:
> >
> > what i WANT to do is:
> >
> >                     for each in enemyship_sprites:
> >                         each.rect.centerx = Enemy.initialx
> >                         each.rect.centery = Enemy.initialy
> >
> > (I created initialx in the init of enemy and update it when i
> > createEnemies at the beginning of the level)
> >
> > But I cant iterate over enemyship_sprites. 
> >
> > Any ideas would be appreciated!
> >
> > ~Denise
> >
> > On 7/1/05, Brian van den Broek <bvande at po-box.mcgill.ca> wrote:
> > > D. Hartley said unto the world upon 01/07/2005 18:56: 
> > > > Ok, this copy should work! Let me know if it doesnt.
> > > > ~Denise
> > > >
> > > >
> > >
> > > I had nothing attached nor in body other than the above.
> > > 
> > > Best,
> > >
> > > Brian vdB
> > >
> > >
> > >
> >
>


More information about the Tutor mailing list