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

Author Topic: Space shooting Game  (Read 4239 times)

0 Members and 1 Guest are viewing this topic.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Space shooting Game
« on: November 12, 2014, 10:07:59 pm »
I am new to sfml and i am doing a space shooting game that should have  :
- a space ship that shoots bullets
- asteroids that move towards the ship

i have the ship and it is moving perfectly .
I am trying to create the bullets and make the ship fire the bullets.
I have no idea how to start to create the bullets or make the ship fire 

Can some please guide me as to how to do this ?
Or direct me to an example of code
PLEASE BE SPECIFIC !!
« Last Edit: November 12, 2014, 10:09:56 pm by Pacman »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Space shooting Game
« Reply #1 on: November 12, 2014, 10:11:52 pm »
Welcome to SFML :)

Please keep in mind that this forum is about SFML, and although we occasionally discuss game development topics, we generally like to keep discussions focused on the library. You should also read this post which gathers some basic rules. In short, the idea is not that we provide working source code for everything, but we are glad to help for specific questions ;)

This being said, you would want to use a STL container to store the bullets, and insert new elements every time a bullet is created. Please search the forum, this very topic has come up a lot. The source code of the SFML Game Development book could also help you.
« Last Edit: November 12, 2014, 10:18:51 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

paul.mihaita

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Space shooting Game
« Reply #2 on: November 13, 2014, 03:57:33 am »
You could also use linked lists for both asteroids and bullets, same as stl containers.You add an element to the list every time you want to (have an new bullet or an new asteroid)

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
    {
        while(acumulator > interval) //i test so that you cannot shoot many bullets per second
        {
            sf::Vector2f f=this->getPozitie();//get the position of the player
            f.x+=this->dimensiune.x-3;//place the 'bullet position' in front of the player, f is an empty variable
            f.y+=this->dimensiune.y/2-3;
            this->Arma.fuel(f);//this command adds one bullet to the list
            acumulator5-=intervall;
        }
    }

and this is the code for adding an new element
p=new glont;//the type of struct of the list
        p->sprite.setTexture(texture);//texture
        p->pozitie=poz;//position
        p->sprite.setPosition(p->pozitie);//set the position also to the sprite
        q->next=p;//setting the last bullet reference to this one

and the code for the struct
struct glont{
    sf::Sprite sprite;
    sf::Vector2f pozitie;
    glont *next=NULL;
};

i used lists, but stl containers are much more easy and 'bug preventive' to use
« Last Edit: November 13, 2014, 07:02:11 pm by paul.mihaita »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Space shooting Game
« Reply #3 on: November 13, 2014, 10:57:57 am »
Don't do what paul.mihaita just said. There's no reason to handcraft linked lists, the STL contains everything you need, including std::list. But typically you want to use std::vector.

And don't use new and delete, but RAII.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

paul.mihaita

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Space shooting Game
« Reply #4 on: November 13, 2014, 07:03:31 pm »
i've said that stl containers are much more easy to use but i just said how i did it:D and i used that kind of list because i wanted to understand the mechanic behind, and is an great learning exercise