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();
}
}