Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. //GAME..H
  2. #ifndef GAME_H
  3. #define GAME_H
  4. #include <SFML/Graphics.hpp>
  5.  
  6. class Game
  7. {
  8.  
  9. public:
  10. Game();
  11. void run();
  12.  
  13. private:
  14. void processEvents();
  15. void update();
  16. void render();
  17. void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
  18. private:
  19. sf::RenderWindow mWindow;
  20. sf::CircleShape mCircle;
  21. bool mIsMovingUp;
  22. };
  23.  
  24.  
  25. #endif
  26.  
  27.  
  28. // END GAME.H
  29.  
  30.  
  31. //GAME.CPP
  32. #include "Book/Games.h"
  33. Game::Game()
  34. :mWindow(sf::VideoMode(640,480), "SFML PROGRAM")
  35. ,mCircle()
  36. ,mIsMovingUp(false)
  37. {
  38. mCircle.setRadius(40.f);
  39. mCircle.setPosition(100.f, 100.f);
  40. mCircle.setFillColor(sf::Color::Red);
  41.  
  42.  
  43. }
  44.  
  45. void Game::run()
  46. {
  47. while (mWindow.isOpen())
  48. {
  49. processEvents();
  50. update();
  51. render();
  52.  
  53. }
  54.  
  55.  
  56. }
  57.  
  58. void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
  59. {
  60. if (key == sf::Keyboard::W)
  61. mIsMovingUp = isPressed;
  62. }
  63.  
  64. void Game::processEvents()
  65. {
  66.  
  67. sf::Event event;
  68. while (mWindow.pollEvent(event))
  69. {
  70. switch (event.type)
  71. {
  72. case sf::Event::KeyPressed:
  73. handlePlayerInput(event.key.code, true);
  74. break;
  75. case sf::Event::KeyReleased:
  76. handlePlayerInput(event.key.code, false);
  77. break;
  78. case sf::Event::Closed:
  79. mWindow.close();
  80. break;
  81. }
  82. }
  83.  
  84. }
  85.  
  86. void Game::render()
  87. {
  88. mWindow.clear();
  89. mWindow.draw(mCircle);
  90. mWindow.display();
  91. }
  92.  
  93. void Game::update()
  94. {
  95. sf::Vector2f movement(0.f, 0.f);
  96. if (mIsMovingUp)
  97. movement.y -= 1.f;
  98. mCircle.move(movement);
  99.  
  100. }
  101.  
  102.  
  103. //END GAME.CPP
  104.  
  105.  
  106.  
  107. //MAIN.CPP
  108.  
  109. #include "Book/Games.h"
  110.  
  111.  
  112. int main()
  113. {
  114. Game game;
  115. game.run();
  116. }
  117.  
  118.  
  119. // END MAIN.CPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement