Is this what you want?
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cmath>
#include <sstream>
#include <string>
static inline std::string int2Str(float x)
{
std::stringstream type;
type << x;
return type.str();
}
#define PI 3.14159265
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600), "ShootingGun");
sf::Image imgGun;
imgGun.LoadFromFile("bar.png");
imgGun.SetSmooth(false);
sf::Sprite Gun(imgGun);
float x = 400, y = 300, angle = 0, lenght = 5;
App.SetFramerateLimit(200);
while(App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
{
App.Close();
}
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Up)
{
x = x + lenght * cos(angle*PI/180);
y = y - lenght * sin(angle*PI/180);
}
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Down)
{
x = x - lenght * cos(angle*PI/180);
y = y + lenght * sin(angle*PI/180);
}
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Right)
{
angle += 1;
Gun.Rotate(1);
}
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Left)
{
angle -= 1;
Gun.Rotate(-1);
}
}
Gun.SetPosition(x,y);
App.Clear();
sf::String text("x: " + int2Str(x) + "y: " + int2Str(y) + "angle: " + int2Str(angle)
+ "sprite angle: " + int2Str(Gun.GetRotation()), sf::Font::GetDefaultFont());
text.SetPosition(0.f,0.f);
App.Draw(text);
App.Draw(Gun);
App.Display();
}
return 0;
}
In math you would do this:
x = x + cos(angle)*speed
y = y + sin(angle)*speed
But because in SFML (like in most libraries/languages) the y-coordinate goes down instead of going up like in math, you have to negate the y-coordinate change. So what you actually want to do when the user presses up is this:
x = x + cos(angle)*speed
y = y - sin(angle)*speed
And when going backwards, you negate them both
x = x - cos(angle)*speed
y = y + sin(angle)*speed