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

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



Shandy,

The reason I was setting Enemy.initialx/y there was because I was
using the createEnemies function to update the enemies' self.initialx
values (when they are created) from 0 to whatever the starting
position is, that way later I can reset each enemy's position to its
initialx/y.

Otherwise, I didnt even HAVE an initialx/y, and when I created the new
enemies, I just did this:

enemyship_sprites.add(Enemy((cols*60)+20, (rows*40)+30, level))

The only reason to have  'newx' or 'initialx' was to find a way to
store each ship's original position so that I could later return the
ship to that original position.

At this point, it's still returning ALL the enemyships to 0,0.

(although it IS recording their initialx/y or newx/y values - I can
see that from the print statement.  It's just that when I do the

                    for enemy in (enemyship_sprites.sprites()):
                        print enemy.initialx, enemy.initialy
                        enemy.rect.centerx = enemy.initialx
                        enemy.rect.centery = enemy.initialy

code, it resets them all to 0,0 (the default value of
initialx/initialy which I was trying to update in the createEnemies
function by doing

>     Enemy.initialx = (cols*60)+20
>     Enemy.initialy = (rows*40)+30
>     enemyship_sprites.add(Enemy(Enemy.initialx, Enemy.initialy, level))
>     print Enemy.initialx,
>     print Enemy.initialy

Is there a way I can update this value and store it for each enemyship
instance so I can refer to it later and reset the ship back to that
position?  If I do

     newX = (cols*60)+20
     newY = (cols*60)+20
     newEnemy = Enemy(newX, newY, level)
     enemyship_sprites.add( newEnemy )

It does make the createEnemies function somewhat clearer, but does not
update the initialx/initialy.

Please let me know if I'm being unclear about what I'm trying to do.

Thanks,
Denise

On 7/6/05, Shandy Brown <sjbrown@xxxxxxxxx> wrote:
> Hi Denise.
> 
> I would just generally recommend the rule of thumb of not using class
> attributes unless you really need them (implementing a Singleton or
> something).  You're using class attributes again here:
> 
> >     Enemy.initialx = (cols*60)+20
> >     Enemy.initialy = (rows*40)+30
> >     enemyship_sprites.add(Enemy(Enemy.initialx, Enemy.initialy, level))
> >     print Enemy.initialx,
> >     print Enemy.initialy
> 
> Try to avoid writing Enemy.initalx anywhere in your code.  You could
> rewrite the above as this:
> 
>      newX = (cols*60)+20
>      newY = (cols*60)+20
>      newEnemy = Enemy(newX, newY, level)
>      enemyship_sprites.add( newEnemy )
> 
> It's also a bit more readable like this, I think.  (Another rule of thumb:
> never sacrifice clarity for brevity)
> 
> -sjbrown
>