Hi everyone, I'm trying to make a Space Invaders clone with pygame (python SDL library, if you hadn't guessed ;) ). The problem I'm having is that when the space invaders move down and change direction they break formation (some move left/right to far).

Here is what I think is the offending code:

import pygame
import constants
from constants import *
from random import randint
from bullet import Bullet

class Enemy(object):
    def __init__(self, image1, image2, x, y):
        self.x = x
        self.y = y
        self.images = [image1, image2]
        self.frame = 0;
        self.xdir = 1
        self.xSpeed = SPACE*2
        self.last_time = 0
        self.bullets = []

    def move(self, bottomLine=False):
        ticks = pygame.time.get_ticks()
        if(ticks - self.last_time >= 500):
            self.frame = (not self.frame)
            self.x += self.xdir * self.xSpeed;
            self.last_time = ticks
        
        if(randint(0, 5000) == 0 and bottomLine):
            self.bullets.append(Bullet(self.x+ENEMY_WIDTH, self.y+ENEMY_HEIGHT, 1))
    
    def checkIfOutOfScreen(self):
        if((self.x >= SCREEN_WIDTH-ENEMY_WIDTH) or (self.x < 0)):
            return True
        
        return False
    
    def moveDown(self):
        self.xdir *= -1
        self.y += self.xSpeed
        
        self.frame = (not self.frame)
        self.x += self.xdir * self.xSpeed;
        self.last_time = pygame.time.get_ticks()   
    
    def show(self, screen):
        screen.blit(self.images[self.frame], (self.x, self.y))
        
class EnemyContainer(object):
    def __init__(self):
        self.enemies = []
        
    def addEnemy(self, enemy):
        self.enemies.append(enemy)   
        
    def handleEnemies(self, screen, enemycount, time_passed_seconds):
        down = False  
        for enemy in self.enemies:
            if(enemy.checkIfOutOfScreen()):
                down = True
                break  
            
        for enemy in self.enemies:
            if(down):
                enemy.moveDown()  
            else:  
                if(self.enemies.index(enemy) >= enemycount):   
                    #print self.enemies.index(enemy)           
                    enemy.move(True)
                else:
                    enemy.move()
        
        for enemy in self.enemies: 
            for bullet in enemy.bullets:
                bullet.move(time_passed_seconds)
                bullet.show(screen)  
            enemy.show(screen)
                
    def checkIfDead(self, player):
        for bullet in player.bullets:
            for enemy in self.enemies:
                if(bullet.collide((enemy.x, ENEMY_WIDTH, enemy.y, ENEMY_HEIGHT))):
                    self.enemies.remove(enemy)
                    player.bullets.remove(bullet)
                    
        for enemy in self.enemies:
            for bullet in enemy.bullets:
                if(bullet.collide((player.x, player.image.get_width(), 
                                  player.y, player.image.get_height()))):
                    return True
                
        return False

Can anyone tell me why they break formation?

Played the game twice and I did not have the problem with formation being broken.

Weird... when I play it they do move, as shown by the picture (after a few changes they are all over the place)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.