SFML community forums
Help => General => Topic started by: newn on November 16, 2010, 04:28:07 pm
-
Hi everyone. I'm wondering, what is the best way to create a few lines of enemies? I'm programming a game, where you are on the bottom of the screen and there's maybe 3 lines of enemies and let's say 5 columns. You need to shoot at them. So I am wondering, what is the best way to create those enemies? I doubt it's the way to copy-paste loads of code...
Using SFMl 1.6.
Thanks!
-
Google for std containers. For this case I would recommend std::list, it's basically like an array, but you can create and delete elements.
-
That sounds nice. But how could I make the value to be a sprite? I cannot make something like... x = EnemySprite;...
int value1 = 10;
int value2 = -3;
list1.push_back (value1);
list1.push_back (value2);
list1.push_back (5);
list1.push_back (1);
There for example... And how is it possible, to put in the sprites and collision boxes in there?
-
Hmmm, no one been doing such a thing? I mean, no one needed multiple enemies of the same kind to appear on a 2D screen like this?
-
can't you make a list of sprites?
-
So make a list like this:
sf::Sprite enemy1(enemy);
and then...
list1.push_back (enemy);
list1.push_back (enemy);
list1.push_back (enemy);
list1.push_back (enemy);
Copy many of such lines and then just
App.Draw(enemy1);
App.Display();
or something else should be added? I think, there should, but I've no idea what.
-
You can create a list of Sprites like this:
std::list<sf::Sprite> enemies;
And them you can add enemies to the list like this:
for (int i = 0; i < 30; i++) {
enemies.push_back(sf::Sprite(YourImage));
}
When you want to draw all of these enemies you go through the list, pick one sprite, draw it, and then take the next one:
for (std::list<sf::Sprite>::iterator i = enemies.begin(); i != enemies.end(); ++i) {
App.Draw(i);
}
-
sf::Sprite enemy1(enemy);
and then...
list1.push_back (enemy);
list1.push_back (enemy);
list1.push_back (enemy);
list1.push_back (enemy);
Copy many of such lines and then just
App.Draw(enemy1);
App.Display();
or something else should be added? I think, there should, but I've no idea what.
I don't know, how far you are at programming...
but you should take a look at, classes, templates, loops etc.
At first, you have to define a Template for the List. That means, you have to define, what "type of element" it can hold.
For Example:
std::list<sf::Sprite> EnemyList; //Create a std::list, that can store sf::Sprites
sf::Sprite EnemySprite(enemyimage); //Create a Sprite
/* Now you have to fill the list, therefor you can use a for loop, instead of copying your code againd and again */
int enemy_rows = 3; //How many rows should there be?
int enemys_per_row = 15; //How many Enemys should be in one row?
for(int i = 0; i < enemy_rows; i++)
{
for(int j = 0; j < enemys_per_row; j++)
{
EnemySprite.SetPosition(j*64, i*64); //For example, if you want 64 pixels space between every sprite
EnemyList.push_back(EnemySprite);
}
}
App.Clear();
for(std::list<sf::Sprite>::iterator it = EnemyList.begin(); it!=EnemyList.end(); it++)
{
App.Draw(*it);
}
App.Display();
It's not perfect and totally correct, but it should get you in the right way.
If you don't understand, what I did there, you should first learn more C++
-
Thanks. I understood, what you wrote here. Most of the part, the other - Google's answered. :) It's working. It's tricky to make it work with another library, when you are a beginner.