Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

OK, so here's the lowdown of the problem I'm trying to solve.

I'm developing a game in PyGame that's a cross between Space Invaders and Columns. I'm trying to make the motion of the enemies similar to that of the aliens in Space Invaders; that is, they're all clustered in a grid, and if even one hits the side of the screen, the entire formation moves down and turns around. However, the motion of these aliens is continuous (as continuous as a monitor can be, anyway), not on a discrete grid like in the original.

The enemies are instances of an Enemy class, and in turn they're held by a 2D array in a enemysquadron module (which, if you don't use Python, is in this case essentially a singleton due to the way Python modules work).

Inside the Enemy class I have a class-scope velocity vector that is reversed every time an Enemy object touches the edge of the screen. This won't do, though, because as time goes on the enemies just become disorganized and jumbled (i.e. not in a grid as planned). I haven't implemented the Enemies going downward yet, so let's not worry about that right now.

Any tips?

share|improve this question
up vote 4 down vote accepted

Basically, the question is, why are your enemies getting disorganized and jumbled? You only describe one factor controlling their movement, so unless there's more relevant information you're not telling us, the only scenario I can think of where they'd lose alignment is one where you're flipping the vector right there in the middle of the movement code as soon as one of them hits the edge. This would cause loss of alignment because you're partway through processing your movement when you change vectors, so some of them have moved with the old vector and some will now move with the new vector.

If that's the case, try this instead: when an enemy hits the edge, turn on a flag, and at the end of your movement cycle, check if the flag is on; if it is, flip the vector and turn off the flag.

share|improve this answer
Ah ha, that's it! Thank you! – JesseTG Jul 9 '12 at 2:40
From Chaos comes the answer to organized movement. Poetic. – Byte56 Jul 9 '12 at 3:25
Holy shit, you just blew my mind. – JesseTG Jul 9 '12 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.