Advertisement
Guest User

Game.cpp

a guest
Aug 23rd, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. //Create window, and limit frame rate
  7. sf::RenderWindow window (sf::VideoMode(800, 600, 32), "Game", sf::Style::Default);
  8. window.setFramerateLimit(30);
  9.  
  10. //Declare image
  11. sf::Texture texture;
  12. //Load image
  13. if(!texture.loadFromFile("Sprites/main.png"))
  14. {
  15. return 1;
  16. }
  17.  
  18. //Creates and places the main sprite
  19. sf::Sprite sprite;
  20. sprite.setPosition(400, 300);
  21.  
  22. //Defines the rects that will be used to switch character view
  23. sf::IntRect front(2, 2, 17, 22);
  24. sf::IntRect back (22, 2, 16, 21);
  25. sf::IntRect left (2, 27, 16, 23);
  26. sf::IntRect right (23, 27, 16, 23);
  27.  
  28. //Starts clock
  29. sf::Clock clock;
  30.  
  31.  
  32. //Main window loop
  33. while(window.isOpen())
  34. {
  35. sf::Event event;
  36.  
  37. sf::Time time = clock.restart();
  38.  
  39. while(window.pollEvent(event))
  40. {
  41. if(event.type == sf::Event::Closed)
  42. {
  43. window.close();
  44. }
  45.  
  46. if(event.key.code == sf::Keyboard::Insert)
  47. {
  48. sf::Image screenshot = window.capture();
  49. screenshot.saveToFile("Screenshot.png");
  50. }
  51. }
  52.  
  53. //Movement
  54. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  55. {
  56. sprite.setTextureRect(front);
  57. sprite.move(0, -14 * time.asSeconds());
  58. }
  59.  
  60. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  61. {
  62. sprite.setTextureRect(back);
  63. sprite.move(0, 14 * time.asSeconds());
  64. }
  65.  
  66. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  67. {
  68. sprite.setTextureRect(right);
  69. sprite.move(16 * time.asSeconds(), 0);
  70. }
  71.  
  72. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  73. {
  74. sprite.setTextureRect(left);
  75. sprite.move(-16 * time.asSeconds(), 0);
  76. }
  77.  
  78. //Draw sequence
  79. window.clear(sf::Color(255, 0, 0)); //(Red, Green, Blue, (optional) Alpha) Alpha is transperency
  80.  
  81. //Draw....
  82.  
  83. window.draw(sprite);
  84.  
  85. window.display();
  86. }
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement