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

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



Matt,

I think most of the movement is handled in the enemy holder class, as follows:

class EnemySprites(pygame.sprite.RenderClear):
    def __init__(self, speed):
        pygame.sprite.RenderClear.__init__(self)       
        # this variable indicates if the enemies 
        # are moving to the left (-1) or right (1)
        self.direction = 1         
        # this variable controls if it's time to move the enemies 
        self.counter = 0        
        # this variable checks if it's time for the enemies to move down
        self.jump_counter = 0        
        # this one sets how fast the enemies move
        self.speed = speed
    def update(self):
        self.counter += 1
        if self.counter >= 50 - (self.speed * 5): # time to move the enemies?
            self.counter = 0
            self.jump_counter += 1
            go_down = False
            if self.jump_counter > 4: # time to move down and change direction?
                self.jump_counter = 0
                self.direction *= -1
                go_down = True
            # move the enemies
            pygame.sprite.RenderClear.update(self, self.direction, go_down)

then within the individual ship Enemy class, there's an update:

    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)

If those snippets dont make sense out of context, drop me a note and I
can send/paste the whole code (don't want to spam the list).

Thanks,
Denise
On 7/6/05, Matt Bailey <mattb@xxxxxxxxxx> wrote:
> Could you post your enemy movement code? (Woooo, sounds like some kind of top
> military secret or something.) I have a hunch as to what might be happening
> now that you're properly storing your initial XY coords, but can't know for
> sure without seeing the code.
> 
>        -Matt Bailey
> 
> On Wednesday 06 July 2005 15:38, D. Hartley set 1,000 monkies in front of
> keyboards and came up with the following:
> > 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
> 
>