HI, I'm using C++ and SFML, I wanted to make a zombie game where 2 players start at the middle of a map and zombies come from the sides. I have already done my man's animation and screen scrolling, but I don't know how to do this. I am confident with the rest of c++ and sfml, but I would like someone to explain to me, in detail, how I could make multiple enemies, how to shoot multiple projectiles, make the enemies die when they are hit 3 times.....

You should check out my Old Blog (also linked in my sig). I detail the steps for making a top-down 2d game similar to what you are suggesting (player starts in map, enemies spawn from an "Enemy Spawn", bullets shooting everywhere).

(Here's the link to the entry on adding enemies to my game: Click Here)

In general, my Enemy Spawn's are considered an item, and I update it like this:

Which basically says, if there are still enemies to release, and if it's time to release a new enemy, then create a new enemy, and add him to the list of active enemies. Here's what the enemies actually do (the full Enemy class here):

If I understand correctly, the original poster is asking how to handle an arbitrary number of objects such as enemies, provided he has the correct logic to handle one.

You can stick all the objects of that type in a single container, like std::vector<Enemy> allEnemies. Whenever you create a new enemy, you add it into that container like so: allEnemies.push_back(... your new enemy object ...). Whenever an enemy dies, you remove it from that container. To handle things like drawing all of them and having all of them take action, you just loop through all of the objects in the container on every game tick, like so:

Thank you two, I find your posts very helful. BeerNuts,I don't really understand some of your code, but I get the basics. Stroppy Katamari, yours really gives a good insight on what to do, but still, can you get me out of doubts:

- So to create enemies, should I just use my player class and add an AI function to it? How could I do this AI? After that should I just create, say, 10 enemies in an object array and loop through their functions just like the normal player(only that using AI instead of handle Input).
- And how could I make projectiles, you still havent talked about that.

- So to create enemies, should I just use my player class and add an AI function to it?

Where in the code you should create new enemies depends on the logic you intend the game to have. Is the player avatar in the game world actually creating enemies? Probably not.

For instance, if you want enemies to spawn randomly over time, just put a function in your main game loop which does that. Assuming you want an average of one new enemy every 3 seconds, and your game runs with 60 logic ticks a second, the function should spawn an enemy 1/(3*60) of the time and do nothing the rest of the time.

- And how could I make projectiles, you still havent talked about that.

Who's shooting? If the player, then the player class' game logic should make new projectiles. If an enemy is shooting, the enemy class' game logic should make new projectiles. Put a tick() function on everything in the game and just call that function on every object once per game tick, just like the draw() function above. Enemies' tick() function will contain the enemies' AI, moving them around and doing stuff. Simple projectiles' tick() function will just keep moving the projectile in the direction it was fired, and perhaps count down to a time or distance where the projectile dissipates and removes itself from the game.

You can handle projectiles like you handle the enemies: place them in their own container when you create them, remove them from the container when they are supposed to die.

For projectiles you may start simply. As stated before you may keep a vector or list of your projectiles and add projectile(s) when someone is shooting or remove projectile(s) when projectile has hit something or has timed out) objects as necessary.

Remember that your player / enemy doesn't own the projectiles. Keep all the projectiles in the one and same list.

I would recommend you to make Player and Zombie both subclasses of a base class (that could be named Person, for example). Person should have functions like Draw() and Update() (these are usually needed for every displayable object), but only the Player should have function HandleInput() and others. You can then make an array of Zombies or an array of pointers to Persons (because of variable size of the derived classes) and then call Draw() and Update() in respective loops.

Bullets are just like Zombies: You create their constructor, Draw() and Update() function. You make a container of Bullets and loops to call Draw() and Update(). And when the player or somebody else fires their gun, you construct a new Bullet using their position, heading et cetera.

Ok, thank you for all these ideas, but could someone explain to me how to make a vector containing items and how to add and delete elements from it. Also how could I make the projectiles functions. Give me some code please!

Ok... that's not helping at all. Can you just show me how I would make my code. I have a person class containing all functions for a zombie, what i want to do is create a vector, make a certain amount of zombies(person class) inside them according to a rand() function, initialize them all and then during my main loop to update them. I thought of this code, but it just makes my player go really laggy and no zombies appear:

Ok... that's not helping at all. Can you just show me how I would make my code. I have a person class containing all functions for a zombie, what i want to do is create a vector, make a certain amount of zombies(person class) inside them according to a rand() function, initialize them all and then during my main loop to update them. I thought of this code, but it just makes my player go really laggy and no zombies appear:

Any help or other ideas? Also, can you also tell me how to do projectiles?

mypel,

I've given you a link to look directly at my source code (you can download the full source from my Old Blog), and you've been given the webpage that explains vectors. You need to do some work yourself.

Your code looks OK, but there's still some issues. You don't need to declare how big a vector is, it is dynamic, so you can just push_back zombie's as you need them. ALso, don't create a new player pointer for the zombies just push an instance of them onto th vector, like this:

Also, you're not looping through the zombies at all. Zombie0 is just the last zombie created in your loop. You need to be access the zombies form the vector. Again, the vector page will help, but here's what I would do:

Thank you, but now its got worse, my program just crashes straight away, I don't know what to do... my code is probably full of bugs, Please, if someone could have a look over it and help me, that would be great! The files are here http://uploading.com/files/get/bd1494c6/moving%2Bman.rar

Please help, I have tried vectors with other games and they work, but there's a problem with this one.

What IDE are you using? You need to learn to use the debugger, and find out what line is causing the crash? If you run it with the debugger, it will stop on the line that causes the crash.

Worse case, you throw some prints inside your code, see which line gets print and which doesn't. Then, work from there.

Honestly I think you need to step back from the game scene, and work on playing with objects in vectors, and looping through the objects and doing things to them in a simple program. Once you understand how it works, then you can move to the next thing.

I have stepped back... I've done things like having a circle move around the screen and if you press a another one appears and if you press x it dissapears. I've also done an age, name and gender database using vectors...... that's not the problem anymore! I did what you did by commenting things out, and found the source of the problem, it is in the function tick(), and that always worked when I was working with 1 zombie...