Advertisement Jump to content
Sign in to follow this  

Make an array of pictures move?

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone wanna help? I now have a picture moving from the left to the right (I choose to make a picture move so I could see if the code worked properly). But now I want to make my array of enemies move at exactly the same path... But I have trouble with passing the coordinates of that picture to the array in the correct way. Here's the code:

Basically, I just want a block of enemies move from the left of the screen to the right of the screen, like in Galaxian or Space Invaders (without 'em going down, that is). So I made an array of pictures representing bad guys, which gave me a block... but how do I make them move...?

Thanks for the help.

Share this post

Link to post
Share on other sites
Why did you use a 2D array? you can just use a 1D array and loop through them. And you could make a struct that contains a xpos and a ypos and even place a surface in there. Something like:

Struct enemy


int xpos;

int ypos;

Surface *graphic;


And then what you need to do is make a boolean that decides the direction of all your bad guys are going in your array. and then change this boolean when they hit the end of the screen. Something like:

for(int i = 0; i < theamountofenemies; i++)


if(enemy.xpos > screenwidth - the width of your enemy)

{movingLeft = true; }

else if( enemy.xpos < 0)

{ moveLeft = false; }


{ enemy.xpos -= 1;}


{ enemy.xpos += 1;}


So what this code does is that when they hit the 0 (the right side of the screen), you change that boolean to false so that they move in opposite direction, until they hit the other side. And with the moveLeft boolean you decide if the x position of your enemy is going left or right. And you do that for each invader in your array.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!