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

Author Topic: Shooting Bullets System  (Read 9380 times)

0 Members and 1 Guest are viewing this topic.

AlaEddinexc

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Shooting Bullets System
« on: May 12, 2015, 07:52:42 pm »
Hey Guys i'm a  :-[ Beginner  :-[ in SFML , i'm making a top-down shooter game , i programmed the player and it is moving , but i don't know how to make it shoots bullets with the space key ! HELP ME !
(sorry for my crap English  ::) )
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Shooting Bullets System
« Reply #1 on: May 12, 2015, 07:57:24 pm »
And what have you done so far? Nobody can just write code and give it to you and say this is how you shoot bullets.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

AlaEddinexc

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Shooting Bullets System
« Reply #2 on: May 12, 2015, 08:02:17 pm »
That's My Code ..
#include <SFML/Graphics.hpp>

int main(){
///Window Parameters///////////////////////////////////////////////
    sf::ContextSettings settings;
    settings.antialiasingLevel = 13;
    sf::RenderWindow window(sf::VideoMode(800, 600), "AlaEddine", sf::Style::Default, settings);

    bool x;
    bool y;
///Loading PLAYER.PNG////////////////////////////
    sf::Texture pTexture;
    sf::Sprite player;
    if(pTexture.loadFromFile("player.png"))
        player.setPosition(x,y);
///loading bullet.png ////////////////////////////
    sf::Texture bTexture;
    sf::Sprite bullet;
    if(bTexture.loadFromFile("bullet.png"))
    ///Texturing Player.png/////
    player.setTexture(pTexture);
    ///Texturing Bullet.png/////
    bullet.setTexture(bTexture);
    bullet.setScale(-10000,-10000);

    while(window.isOpen())
    {

        sf::Event Event;
        while(window.pollEvent(Event))
        {

            ///Exit When Escape Pressed////////////////////////////////////////////////
        if(Event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
                window.close();
            }
        }
        /// //MOVING PLAYER//////////////////////////////
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
        {
            player.move(0,-1.5);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            player.move(0,1.5);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)){
            player.move(-1.5,0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            player.move(1.5,0);
        }
        ///ROTATION////////////////////////////////////////////
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            player.rotate(2);
                     }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            player.rotate(-2);
                   }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){

        bullet.setPosition(player.getPosition());
        bullet.setScale(1,1);
                    }
        window.draw(bullet);
        window.display();
        window.clear(sf::Color::Cyan);
        ///Showing Player To Screen
        window.draw(player);


    }
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Shooting Bullets System
« Reply #3 on: May 12, 2015, 08:56:48 pm »
And where exactly is your problem?

The idea of this forum is to help you with concrete questions regarding SFML, not to teach you game development. You'll find a lot of books or tutorials on the internet that do so. Just avoid video tutorials at all costs, they're usually poorly made (there are a few exceptions, like those from Vittorio Romeo).

You can also have a look at the source code of the official SFML book. The early chapters should be relatively easy to understand, Chapter 7 adds bullets.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: Shooting Bullets System
« Reply #4 on: May 13, 2015, 03:27:56 pm »
You will just end up memorizing the logic if we give you code.
I am writing space invader right now, and just finished writing the bullet and then i see your thread here.

You can save the bullet position in the vector

std::vector<sf::Vector2f>position;

if ( spacebar pressed )
{
     position.push_back(sf::Vector2f( /*  put the position here where bullets will appear */ );
     // draw it here
}

outside the event loop
you can now move it

if ( /* check here if the position is not equal to 0 */ )
{
      // move it now from the coordinates of position vector
      // draw it
}

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Shooting Bullets System
« Reply #5 on: May 13, 2015, 07:49:34 pm »
At a high level; what you need is:

Detect that the player has pressed a key.
Upon keypress create a new "bullet" object and store it along with all other bullet objects.
During each frame: Detect which bullets are no longer relevant (like; outside screen) and remove them. Move all bullets according to time since last frame. Detect collisions with bullets and mark them to be removed next frame. Draw all the remaining/active bullets.

The exact details of how to implement that are left as an exercise for the reader.