1

In a simple pygame zero 2D game, I have a list of Actors that I'm looping through to ensure that they don't run off the side of the screen. However, going right, only the leftmost item in the list (the first one) is triggering the direction change--the rest run off the screen. Strangely, it works fine when they're moving left.

I'm running this code (except the function at the bottom) in the update() function. The enemies are 48 pixels wide.

Edit: I looked in the debugger and it seems the for loop never gets to the second enemy in the list. It's just constantly checking the first one.

WIDTH = 800
HEIGHT = 600
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
...
for enemy in enemies:
        if enemy.x >= WIDTH - 50 or enemy.x <= 50:
            # change enemy direction
            enemy_direction *= -1
            break
        else:
            animate_sideways(enemies)
...
def animate_sideways(enemies):
    for enemy in enemies:
        enemy.x += enemy_direction

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
beachCode
  • 3,202
  • 8
  • 36
  • 69
  • 1
    Could you please tell us what the size of the enemies are? – Lashen Dharmadasa Jan 02 '23 at 06:40
  • Sure. The enemies are 48 pixels wide. – beachCode Jan 02 '23 at 06:41
  • What do you do with enemy_direction? – Adid Jan 02 '23 at 06:48
  • I added some more of the code to clarify. thx – beachCode Jan 02 '23 at 06:55
  • 1
    My guess is that the inside of the if clause will be triggered twice for some of the actors (leading to no effect) as the enemies are already outside of the area and will not immediately have left after the direction has been changed. The left-most enemy is the one which does exit immediately due to being closest to the bounds, therefore does not cause this issue. – Peter Warrington Jan 02 '23 at 06:55
  • I suspected that might be happening as well. I tried to test it by "bouncing" the Actors back, but it didn't help. # bounce back into the allowed space for enemy in enemies: enemy.x += (enemy_direction * 50) – beachCode Jan 02 '23 at 07:05
  • You should give a direction property to each enemy. – White Wizard Jan 02 '23 at 07:16
  • I get your point. At this time, I have a simple group of enemies. Eventually, I'll need to do that for the groups, but I don't want these moving separately. Everything is fine--except the right-side border. – beachCode Jan 02 '23 at 07:29

1 Answers1

2

You can not determine the direction in which the enemies should move and move the enemies in the same loop. First you need to set the common direction of movement. After that you can move all enemies in this direction. animate_sideways itself has a loop that moves all enemies and must be called after the direction is changed:

# Determine whether the common direction of movement must be changed 
for enemy in enemies:
    if enemy.x >= WIDTH - 50 or enemy.x <= 50:
        # change enemy direction
        enemy_direction *= -1
        break

# Move all enemies in the same direction
animate_sideways(enemies)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174