erzis

Untitled

Dec 27th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4. int main(){
  5.  
  6. enum Direction{Down, Left, Up, Right};
  7.  
  8. sf::Vector2i source(0, Down);
  9.  
  10. sf::RenderWindow window(sf::VideoMode(800, 800), "Adventure game");
  11. window.setFramerateLimit(60);
  12.  
  13. sf::Texture texture;
  14. texture.loadFromFile("tiles/char2.png");
  15.  
  16. sf::Sprite player(texture);
  17.  
  18. while(window.isOpen())
  19. {
  20. sf::Event event;
  21. while(window.pollEvent(event))
  22. {
  23. if(event.type == sf::Event::Closed)
  24. window.close();
  25. }
  26.  
  27. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  28. {
  29. source.y = Up;
  30. player.move(0, -10);
  31. }
  32. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  33. {
  34. source.y = Down;
  35. player.move(0, 10);
  36. }
  37. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  38. {
  39. source.y = Right;
  40. player.move(10, 0);
  41. }
  42. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  43. {
  44. source.y = Left;
  45. player.move(-10, 0);
  46. }
  47.  
  48. player.setTextureRect(sf::IntRect(source.x * 100, source.y * 130, 130, 130));
  49. window.clear(sf::Color::White);
  50. window.draw(player);
  51. window.display();
  52. }
  53. return EXIT_SUCCESS;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment