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

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



Yeah, you can send the whole thing along if you want me to take a look at it. 
In particular, the value of "speed" looks to be critical, why don't you just 
hardcode it? I did a lot of stuff with movement, panning, scrolling, etc in 
both axis a few months ago on my side-scrolling platform game, maybe I can be 
of some assistance.

It seems like the way you're doing movement, it would be very easy for your 
movement to get out of sync with your direction-swapping code, making the 
enemy turn too soon (as it's doing now) or too late (going off the screen). A 
more flexible and easily controlled method would be to compare the enemy's 
coords against screen resolution to find out where it is on the screen. But 
it's hard to make judgements on the results of your code without knowing the 
value of "speed".

You can send the script(s) as attachments to mattb@xxxxxxxxxx if you want 
(attachments will avoid formatting issues with copying/pasting the code to/
from an e-mail client).

	-Matt Bailey

On Wednesday 06 July 2005 18:12, D. Hartley set 1,000 monkies in front of 
keyboards and came up with the following:
> 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