Sign in to follow this  
walle

How to move enemies in space invaders

Recommended Posts

Well...I'm doing a space invaders clone as a learning experience...but now I'm stuck, I don't know how to manage and move the enemies. There should be three diferent types of enemies...sorted in five rows 2-2-1 with eleven colums...and they should move from the left to the right drop down increse speed and do to the left, and so on untill all ships are gone or one ship have reached the player. Shooting...my thoughts is that every half second one random ship from the last row shoot... Well about the managing part...thinking about having five vectors representing the rows and fill them with enemies...that way it's simple to get the last row for the shooting... Well...what I want is some opinions on my thoughts or other solutions... Thanks

Share this post


Link to post
Share on other sites
You are correct, a vector is the way to go. I'd recommend making your own Enemy class, followed by an EnemyCollection class managing the group of enemies. Another thing to note is how you're going to do multiple levels. If you design for two different stages, you'll be fine.

Share this post


Link to post
Share on other sites
Well I have a enemy class...a enemycollection? Yes...I'll do one =) but still not clear on how to move them and so...

Share this post


Link to post
Share on other sites
if you put them in 5 different vectors, it might be a little harder to manage them.

i havent played space invaders in a long time, but ill try my best.

it may be better to have a EnemyManager class that can manage each of the enemies. and because some of the enemies behave differently, you are going to want different behavoir classes. the behavoir can be a sort of an interface


class EnemyBehavior
{
//common behavoir
void behave( Enemy enemy ) = 0;
}

class WiggleBehavoir : public EnemyBehavoir
{
void behave( Enemy enemy );
}



this way, you can make their behavoir independent of the enemy itself, and just make new implementations of the behave() method, for moving, shooting, etc. you can assign a behavior to an enemy and add it to your EnemyManager like this.


Enemy newEnemy = new Enemy( parms );
//set some values...
newEnemy->setBehavoir( new WiggleBehavior() );
enemyManager.addEnemy( newEnemy );



the enemy manager could then gather information from the enemies, regarding position, row number, number of surroudning enemies left, number of total enemies left, and assign new behavoirs accordingly. like if they are too close to the player and need to go to the back again, do enemy->setBehavoir( retreatBehavoir() ) or something of the like. this way, your enemies can be dynamic.

hopefully i answered your question.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by xSKOTTIEx
class EnemyBehavior
{
//common behavoir
void behave( Enemy enemy ) = 0;
}


You probably want to make that function prototype work on void behave( Enemy & enemy ), otherwise the behaviour would be working on its own local copy of the object [wink]. Other than that, nice idea.

The main issue would still be how you decide wether to change direction or not...

Share this post


Link to post
Share on other sites
I wouldn't recommend using a vector, I would recommend a linked list. Since you will be inserting and deleting into the middle it, a list will be much faster than a vector. Since its basically an array, everytime you insert or delete from the middle of a vector the proceeding elements have to be moved in memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by xSKOTTIEx
class EnemyBehavior
{
//common behavoir
void behave( Enemy enemy ) = 0;
}


You probably want to make that function prototype work on void behave( Enemy & enemy ), otherwise the behaviour would be working on its own local copy of the object [wink]. Other than that, nice idea.

The main issue would still be how you decide wether to change direction or not...


yeah... i definately forgot the pointers there. thats what i get when i code in java at work.

Share this post


Link to post
Share on other sites
I recently wrote a Space Invaders clone in Java, so here's what I did for the enemy movement:

* To handle the step motion of the enemy aliens, I used a variable that counts down from a number, decrements that number each frame of animation, then moves all the aliens by some amount when the counter variable reaches zero. For example, if you initialize the counter to 60, and your game is running at 30fps, it would move the aliens every two seconds.

* To handle the direction of the aliens, I determined the position of the left- or right-most alien, depending on the direction, and switched the direction when the x-value of the left- or right-most alien reaches some threshold. Oh, and add to the x-values of all the aliens to move them downward when that threshold is reached.

* I also determine the y-position of the bottom-most alien, then end the game when its level with the player.

I just used one collection for all the enemies, as it seemed simpler that way. I haven't figured out how to fire bullets from the enemies yet, I suppose you could store the bottom-most aliens in each column in an array, then fire a bullet from a random alien in that array every second or so.

I hope this helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this