PyGame Space Invaders Game - Making aliens move together
<p>I've created a Space Invaders clone in Python using the PyGame modules but I'm running into some difficulty getting them to move down together when reaching the edge of the game screen. </p>

<p>How would I make it so when the aliens reach the edge of the game screen they all simultaneously change direction and drop down a level?</p>

<pre><code>import pygame
import random

class spaceInvader(pygame.sprite.Sprite):

    def __init__(self):
        self.image = pygame.image.load("spaceInvader.png")
        self.x = 200
        self.y = 390

        self.shots = []

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_RIGHT]:
            self.x+=dist
        elif key[pygame.K_LEFT]:
            self.x-=dist

    def draw(self, surface):
        surface.blit(self.image,(self.x,self.y))
        for s in self.shots:
            s.draw(screen)

class Alien(pygame.sprite.Sprite):

    def __init__(self,x,y,direction,alienType):
        pygame.sprite.Sprite.__init__(self)

        self.AlienType = alienType
        self.Direction = direction

        if alienType == 1:
            alienImage = pygame.image.load("alien1.png")
            self.Speed = 1
            self.Score = 5

        if alienType == 2:
            alienImage = pygame.image.load("alien2.png")
            self.Score = 15
            self.Speed = 1

        if alienType == 3:
            alienImage = pygame.image.load("alien3.png")
            self.Score = 10
            self.Speed = 1

        if alienType == 4:
            alienImage = pygame.image.load("alien4.png")
            self.Score = 20
            self.Speed = 1

        if alienType == 5:
            alienImage = pygame.image.load("alien5.png")
            self.Score = 25
            self.Speed = 1

        self.image = pygame.Surface([26, 50])
        self.image.set_colorkey(black)
        self.image.blit(alienImage,(0,0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def moveAliens(self):
        if self.Direction == "right":
            self.rect.x += self.Speed
        if self.Direction == "left":
            self.rect.x -= self.Speed

pygame.init()

pygame.mouse.set_visible(False)
screen = pygame.display.set_mode([400,400])

allAliens = pygame.sprite.Group()
spaceInvader = spaceInvader()

pygame.display.set_caption("Space Attack")
background_image = pygame.image.load("Galaxy.png").convert()
pygame.mouse.set_visible(True)
done = False
clock = pygame.time.Clock()
black =( 0, 0, 0)
white =( 255,255,255)
red = (255, 0, 0)
score = 0
enemies = []

#For X coords
spawnPositions = [50,100,150,200,250,300,350]

yCoord = 10

for n in range(5):
    for i in range(len(spawnPositions)):
        xCoord = spawnPositions[i] 
        alienType = random.randint(1,5)
        alien = Alien(xCoord, yCoord,"right", alienType)
        allAliens.add(alien)
    yCoord = yCoord + 15

loop = 0
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    for alien in (allAliens.sprites()):
        if alien.rect.x &lt; 0:
            alien.rect.y = alien.rect.y + 15
            alien.Direction = "right"
        if alien.rect.x &gt; 395:
            alien.rect.y = alien.rect.y + 15
            alien.Direction = "left"
        loop =+1

    for alien in (allAliens.sprites()):
        alien.moveAliens()

    spaceInvader.handle_keys()
    screen.blit(background_image,[0,0])
    spaceInvader.draw(screen)
    allAliens.draw(screen)
    pygame.display.flip()
    clock.tick(20)

pygame.quit()
</code></pre>

<p>Thanks.</p>

<p>Your problem lies here:</p>

<pre><code>for alien in (allAliens.sprites()):
    if alien.rect.x &lt; 0:
        alien.rect.y = alien.rect.y + 15
        alien.Direction = "right"
    if alien.rect.x &gt; 395:
        alien.rect.y = alien.rect.y + 15
        alien.Direction = "left"
    loop =+1
</code></pre>

<p>I assume the aliens are currently individually dropping down? </p>

<p>You need to change this so that when one alien triggers these if statements, all alien's <code>y</code> and <code>direction</code> are appropriately set, not just the one hitting the side.</p>

python python-2.7 pygame