Friday, December 28, 2018

ai - Space Invaders-type game: Keeping the enemies aligned with each other as they turn around?


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?




Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...