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

Author Topic: Making a slow moving sprite bullet?  (Read 1928 times)

0 Members and 1 Guest are viewing this topic.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
Making a slow moving sprite bullet?
« on: March 30, 2015, 11:23:16 am »
Hi again, sorry to be spamming but I am completely new to SFML and struggling quite a bit. How does one make a slow moving bullet sprite in SFML C++ code? I have a basis done that works but it obviously moves waaaaayyy too fast. WOuld like to know how to implement a more slow moving bullet as I intend to create a very basic super mario like game.

Here is my code:

#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
int main()
{
        enum Direction{ Down , Left, Right, Up};
        sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");
        sf::Texture playerTexture;
        sf::Texture bulletTexture;
        sf::Sprite gameCharacter;
        sf::Sprite bullet;
        sf::Vector2i source(1, 32);
       
        if (!playerTexture.loadFromFile("Player.png"))
                window.setTitle("Error!");
        if (!bulletTexture.loadFromFile("Bullet.png"))
                window.setTitle("Error!");
        bullet.setTexture(bulletTexture);
        gameCharacter.setTexture(playerTexture);

        while (window.isOpen())
        {
                window.clear(sf::Color::Blue);
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Escape)
                                        window.close();
                                else if (event.key.code == sf::Keyboard::LShift)
                                {
                                       
                                        for (float i = gameCharacter.getPosition().x; i <= window.getSize().x; i+=.02){
                                                bullet.setPosition(i, gameCharacter.getPosition().y);
                                                window.draw(bullet);
                                        }
                                }


                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        gameCharacter.move(0, -1);
                        if (gameCharacter.getPosition().y < 0)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x, 0));
                        source.y = Up;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        gameCharacter.move(0, 1);
                        if (gameCharacter.getPosition().y > window.getSize().y - gameCharacter.getGlobalBounds().height)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x ,window.getSize().y - gameCharacter.getGlobalBounds().height));
                        source.y = Down;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        gameCharacter.move(1, 0);
                        if (gameCharacter.getPosition().x > window.getSize().x - gameCharacter.getGlobalBounds().width)
                                gameCharacter.setPosition(sf::Vector2f(window.getSize().x - gameCharacter.getGlobalBounds().width, gameCharacter.getPosition().y));
                        source.y = Right;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        gameCharacter.move(-1,0);
                        if (gameCharacter.getPosition().x < 0)
                                gameCharacter.setPosition(sf::Vector2f(0, gameCharacter.getPosition().y));
                        source.y = Left;
                }


                source.x++;
                if (source.x * 32 >= playerTexture.getSize().x)
                        source.x = 0;
                gameCharacter.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                window.draw(gameCharacter);
                window.display();
                window.clear();
        }
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Making a slow moving sprite bullet?
« Reply #1 on: March 30, 2015, 11:25:11 am »
Search for "fixed time step", this topic comes up again and again :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: