Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Space Invaders - enemies. Best way to create them?  (Read 7377 times)

0 Members and 2 Guests are viewing this topic.

newn

  • Guest
Space Invaders - enemies. Best way to create them?
« 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!

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Space Invaders - enemies. Best way to create them?
« Reply #1 on: November 16, 2010, 05:03:51 pm »
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.

newn

  • Guest
Space Invaders - enemies. Best way to create them?
« Reply #2 on: November 16, 2010, 05:26:45 pm »
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?

newn

  • Guest
Space Invaders - enemies. Best way to create them?
« Reply #3 on: November 17, 2010, 01:25:40 pm »
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?

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
Space Invaders - enemies. Best way to create them?
« Reply #4 on: November 17, 2010, 03:20:04 pm »
can't you make a list of sprites?
John Carmack can Divide by zer0.

newn

  • Guest
Space Invaders - enemies. Best way to create them?
« Reply #5 on: November 17, 2010, 04:49:21 pm »
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.

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Space Invaders - enemies. Best way to create them?
« Reply #6 on: November 17, 2010, 05:57:10 pm »
You can create a list of Sprites like this:
Code: [Select]
std::list<sf::Sprite> enemies;
And them you can add enemies to the list like this:
Code: [Select]
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:
Code: [Select]
for (std::list<sf::Sprite>::iterator i = enemies.begin(); i != enemies.end(); ++i) {
    App.Draw(i);
}

Fkscorpion

  • Newbie
  • *
  • Posts: 4
    • View Profile
Space Invaders - enemies. Best way to create them?
« Reply #7 on: November 17, 2010, 05:59:10 pm »
Quote
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:
Code: [Select]

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++

newn

  • Guest
Space Invaders - enemies. Best way to create them?
« Reply #8 on: November 17, 2010, 10:31:27 pm »
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.