main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <SFML/Window.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <list>
  4. #include <cstdint>
  5. #include <iostream>
  6. #include <iterator>
  7. class Player : public sf::Drawable
  8. {
  9. private:
  10. uint8_t hp;
  11. sf::RectangleShape collider;
  12. virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
  13. {
  14. target.draw(this->collider);
  15. }
  16. public:
  17. static const uint8_t maxHP = 5;
  18. Player() : hp(5), collider({50, 50})
  19. {
  20. this->collider.setFillColor(sf::Color::Green);
  21. }
  22. bool collision(const sf::RectangleShape& object)
  23. {
  24. return this->collider.getGlobalBounds().intersects(object.getGlobalBounds());
  25. }
  26. void move(sf::Vector2f vector)
  27. {
  28. this->collider.move(vector);
  29. }
  30. };
  31. class Game
  32. {
  33. private:
  34. sf::RenderWindow* window;
  35. Player player;
  36. std::list<sf::RectangleShape> objects;
  37. bool quit = false;
  38. void draw()
  39. {
  40. this->window->clear(sf::Color::Black);
  41. this->window->draw(this->player);
  42. for(const auto& object : this->objects)
  43. {
  44. this->window->draw(object);
  45. }
  46. this->window->display();
  47. }
  48. void processCollisions()
  49. {
  50. for (std::list<sf::RectangleShape>::iterator it = this->objects.begin(); it != this->objects.end();)
  51. {
  52. if(this->player.collision(*it))
  53. {
  54. std::cout << "collision" << std::endl;
  55. it = this->objects.erase(it);
  56. }
  57. ++it;
  58. }
  59. }
  60. void processEvents()
  61. {
  62. sf::Event event;
  63. while (window->pollEvent(event))
  64. {
  65. if(event.type == sf::Event::Closed)
  66. {
  67. this->quit = true;
  68. }
  69. else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
  70. {
  71. this->player.move(sf::Vector2f(0, -5));
  72. }
  73. else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
  74. {
  75. this->player.move(sf::Vector2f(0, 5));
  76. }
  77. else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
  78. {
  79. this->player.move(sf::Vector2f(-5, 0));
  80. }
  81. else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
  82. {
  83. this->player.move(sf::Vector2f(5, 0));
  84. }
  85. }
  86. }
  87. public:
  88. Game(sf::RenderWindow* window) : window(window)
  89. {
  90. sf::RectangleShape r1({20, 20});
  91. r1.setFillColor(sf::Color::Red);
  92. r1.setPosition({300, 300});
  93. sf::RectangleShape r2({40, 40});
  94. r2.setFillColor(sf::Color::Yellow);
  95. r2.setPosition({100, 200});
  96. sf::RectangleShape r3({10, 10});
  97. r3.setFillColor(sf::Color::Magenta);
  98. r3.setPosition({20, 600});
  99. sf::RectangleShape r4({20, 20});
  100. r4.setFillColor(sf::Color::Cyan);
  101. r4.setPosition({800, 700});
  102. this->objects.push_back(r1);
  103. this->objects.push_back(r2);
  104. this->objects.push_back(r3);
  105. this->objects.push_back(r4);
  106. }
  107. void run()
  108. {
  109. while(!this->quit)
  110. {
  111. this->processEvents();
  112. this->processCollisions();
  113. this->draw();
  114. }
  115. }
  116. };
  117. int main(int argc, char *argv[])
  118. {
  119. sf::RenderWindow window(sf::VideoMode(1280, 720), "sfml-template");
  120. window.setFramerateLimit(30);
  121. Game game(&window);
  122. game.run();
  123. window.close();
  124. return 0;
  125. }