1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <SFML/Graphics.hpp>
enum Direction { down, left, right, up};
int main()
{
//enum Direction { down, left, right, up};
sf::Vector2i source(1, down);
sf::RenderWindow window(sf::VideoMode(1080, 700), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
sf::Texture image;
sf::Sprite sprite;
image.loadFromFile("player2.png");
sprite.setTexture(image);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::KeyPressed)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
sprite.move(-1, 0);
source.y = left;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
sprite.move(1, 0);
source.y = right;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
sprite.move(0, -1);
source.y = up;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
sprite.move(0, 1);
source.y = down;
}
}
source.x++;
if(source.x * 30 >= image.getSize().x)
source.x = 0;
}
//window.draw(shape);
//sprite.setTextureRect(sf::IntRect(source.x * 18, source.y * 10,50,95 ));
sprite.setTextureRect(sf::IntRect(source.x * 30, source.y * 30,30,30));
window.draw(sprite);
window.display();
window.clear();
}
return 0;
} |
Partager