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

[pygame] A more Pythonic way to do this



So I received suggestions for doing this different ways from Shandy
and from Adam, and have tried both, and both reset the
havent-been-killed-yet enemies to the top of the screen.  I've
included both suggestions below.

However, *both* of them place the ships not back at their originaly x
position, but about 3 inches to the left. The entire body of them
moves left-right as before (and, it looks like, about the same
distance back and forth), but instead of from the left edge to the
right edge, they move from about three inches off the left side of the
screen and stop about three inches from the right edge of the screen
(i.e., close to the middle).

Is it not accurately storing the initialx? the y part seems to be
fine, it *is* moving them back up to the top?

Any suggestions for problem areas to look at or things to try would be
appreciated!

~Denise

---------- Forwarded message ----------
From: D. Hartley <denise.hartley@xxxxxxxxx>
Date: Jul 6, 2005 1:32 PM
Subject: Re: [pygame] Fwd: [Tutor] A more Pythonic way to do this
To: pygame-users@xxxxxxxx


Shandy,

So what I have now is...

class Enemy(pygame.sprite.Sprite):
   def __init__(self, startx, starty, level):
       pygame.sprite.Sprite.__init__(self)
*snip*
       self.rect.centerx = startx
       self.rect.centery = starty
       self.initialx = 0
       self.initialy = 0

So you're saying I should make it:

       self.rect.centerx = startx
       self.rect.centery = starty
       self.initialx = startx
       self.initialy = starty

..?

I did that, and it did seem to store them correctly, and when the
enemies got too low, it did reset them up higher (only the ones I
hadn't killed).  For some odd reason, the entire block of them wasnt
restarted where it had started, but about ... three inches to the
left.  I'm not sure why this is yet, I'll keep working on it.

But for the moment, at least it is only resetting the
havent-been-killed-yet enemies to the top, so that is progress!  It
doesnt seem to be storing the initial values QUITE right, as it's
placing them like i said three inches to the left (so they're moving
back and forth the same amount of space, just from three inches off
the left edge of the screen, and stopping about three inches from the
right edge of the screen)?

~Denise


From Adam:

From: Adam Bark <adam.jtm30@xxxxxxxxx>
Reply-To: Adam Bark <adam.jtm30@xxxxxxxxx>
To: "D. Hartley" <denise.hartley@xxxxxxxxx>
Date: Jul 6, 2005 1:29 PM
Subject: Re: [Tutor] A more Pythonic way to do this
Reply | Reply to all | Forward | Print | Add sender to Contacts list |
Trash this message | Report phishing | Show original
I changed the Enemy class to this:

# class for individual enemy ships
class Enemy(pygame.sprite.Sprite):
    def __init__(self, startx, starty, level):
        pygame.sprite.Sprite.__init__(self)
        if level == 1:
            self.image, self.rect = load_image('salad_ship.bmp', -1)
        elif level == 2:
            self.image, self.rect = load_image('carrot_ship.bmp', -1)
        elif level == 3:
            self.image, self.rect = load_image('broccoli_ship.bmp', -1)
        elif level == 4:
            self.image, self.rect = load_image('pizza_ship.bmp', -1)
        elif level == 5:
            self.image, self.rect = load_image('fry_ship.bmp', -1)
        elif level == 6:
            self.image, self.rect = load_image('burger_ship.bmp', -1)
        elif level == 7:
            self.image, self.rect = load_image('ruffles_ship.bmp', -1)
        elif level == 8:
            self.image, self.rect = load_image('kfc_ship.bmp', -1)
        else:
            self.image, self.rect = load_image('spoon_ship.bmp', -1)   
        self.rect.centerx = startx
        self.rect.centery = starty 
        self.origpos = (startx, starty)       
    def update(self, direction, go_down):
        jump = 40 # how much the enemies move to the right/left on
each jump
        if go_down:
            # if a ship is moving down on this round,
            # it doesn't move on the x-axys
            self.rect.move_ip((0, 5))
        else:
            # move a ship in the x-axys.
            # if direction=1, it moves to the right; -1 to the left
            self.rect.move_ip((jump * direction, 0))        
        # maybe it's time for a shot? :) 
        # the chances are 1/30
        dice = random.randint(0,30)
        global enemy_shot_sprites
        if dice == 1:
            shot = EnemyShot(self.rect.midtop)
            enemy_shot_sprites.add(shot)
    def refresh(self):
        self.rect.center = self.origpos

Then I changed the for loop to this:

for ship in ships:
    ship.refresh()

ships is enemyship_sprites.sprites()