# Problem with Space Invaders Game

• 12-19-2010, 07:59 PM
andyman99008
Problem with Space Invaders Game
Hi, I'm having trouble making the aliens move properly in my space invaders game. They can move left and right, but when it gets to either side, only the aliens that hit the edge move back/down, then after a while they just move on top of the other images.

I was wondering how I could make all the aliens move the same, even though they are all separate objects(needed for collision).

I even tried creating a separate rectangle that moves as the aliens should move, then I added each alien to an array list at the x and y position of the rectangle + the size of the alien. This didn't work either.

I'm tearing my hair out here! I will post whatever code you think would let you help!
• 12-19-2010, 08:03 PM
Fubarable
It's all in the game logic. You likely have an Alien class that encapsulates the alien concept including its position and such. Do you need to create an AlienPack object that holds a collection of Aliens and has the logic that allows them to move in concert, that allows removal of an Alien when it has been hit, etc...?
• 12-19-2010, 08:09 PM
andyman99008
Yes thats what I want to do. But I can't think what I need to do for the Aliens to move in unison.

Code:

```aliensX+=aliensXSpeed if aliensX < 0 or > BOARD_WIDTH { aliensXSpeed = -aliensXSpeed alienY+=ALIEN_JUMP; }```
I'm stuck on that basic logic...can you give me any pointers?
• 12-19-2010, 08:11 PM
Fubarable
Again, if you have an AlienPack class, with a method such as move(), then this class could handle all the logic by iterating through each Alien held in the collection and moving them as it does so. Then it can test if an Alien has reached a boundary, and if so, reverse the direction for all the Aliens held in it.
• 12-19-2010, 08:25 PM
andyman99008
Ok so if my images are 30 wide and 30 tall.

Code:

```iterate through arraylist for each alien alien.move() move(){ alienX+=image width if(alienX+image width > BOARD_WIDTH or alienX<0) reverse direction }```
Does this make any sense?

EDIT: god dammit, I just can't seem to even think the logic through, I have no idea how to make them move as one...If I iterate through and move each one, then that is moving them separately
• 12-19-2010, 09:05 PM
andyman99008
Ok, well, I just tried making this class, and it does exactly the same thing as before....

• 12-19-2010, 09:12 PM
Fubarable
Don't worry too much, and in fact sometimes it's a good idea to walk away from the computer for a while. But you could even give your Alien#move method a parameter, perhaps an int to tell the Alien how far to move and which direction (if negative to the left, if positive, to the right), and have the AlienPack hold an int called direction, that it passes to each Alien when it calls move. Then AlienPack can change this variable when the pack reaches an edge.

This is only one possible suggestion, but certainly not the only or even best way to do this. Again, take a walk, clear your mind before coming back to this. Then experiment, try different options, have fun with the code.

Luck!
• 12-19-2010, 09:22 PM
andyman99008
Ye, I'm going to take a break...I just tried to think it through from the start again and couldn't even remember what I was trying to do. Ha.

Thanks for your help, really appreciated.
• 12-19-2010, 10:20 PM
andyman99008
Wow, I can't thank you enough, that little bit of advice about leaving the pc actually helped!
Heres what I came up with, what do you think? Is the logic sound?

Code:

```while array contains an alien; get the x coord if x > (board_width-alien width) && direction != -1 direction = -1;    then iterate again set the y value +15 //do the same for moving left but replace board_width-alien_width with x<alien_width && direction!=1 then for moving along my x I have iterate through check if alien is visible then call a different move method in my Alien class alien.move(direction); //the move method in alien alienx+=direction;```
Again, thanks for all your help