Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "Character.h"
  4. using namespace std;
  5.  
  6. #ifdef _DEBUG
  7. #pragma comment(lib, "sfml-window-d.lib")
  8. #pragma comment(lib, "sfml-system-d.lib")
  9. #pragma comment(lib, "sfml-graphics-d.lib")
  10. #else
  11. #pragma comment(lib, "sfml-window.lib")
  12. #pragma comment(lib, "sfml-system.lib")
  13. #pragma comment(lib, "sfml-graphics.lib")
  14. #endif
  15.  
  16. bool didHit(Character charOne, Character charTwo);
  17. int main()
  18. {
  19. sf::RenderWindow window(sf::VideoMode(800, 600), "DV1594 Gruppuppgift: didHit");
  20. Character firstChar(100.f, 100.f, 80.f, 120.f, sf::Color::Yellow);
  21. Character secondChar(350.f, 250.f, 100.f, 90.f, sf::Color::Green);
  22. secondChar.setMovable(false);
  23.  
  24.  
  25. sf::Clock clock;
  26. sf::Time elapsedTimeSinceLastUpdate = sf::Time::Zero;
  27. sf::Time timePerFrame = sf::seconds(1.f / 60.f);
  28. while (window.isOpen())
  29. {
  30. sf::Event event;
  31. while (window.pollEvent(event))
  32. {
  33. if (event.type == sf::Event::Closed)
  34. {
  35. window.close();
  36. }
  37. }
  38. elapsedTimeSinceLastUpdate += clock.restart();
  39. while (elapsedTimeSinceLastUpdate >= timePerFrame)
  40. {
  41. elapsedTimeSinceLastUpdate -= timePerFrame;
  42. firstChar.move();
  43. if (didHit(firstChar, secondChar))
  44. {
  45. std::cout << "HIT!!" << std::endl;
  46. }
  47. else
  48. {
  49. std::cout << "NO HIT!!" << std::endl;
  50. }
  51.  
  52. }
  53. window.clear();
  54. window.draw(secondChar);
  55. window.draw(firstChar);
  56. window.display();
  57. }
  58.  
  59. getchar();
  60. return 0;
  61. }
  62.  
  63. bool didHit(Character firstChar, Character secondChar)
  64. {
  65. if (firstChar.getXUpperLeftCorner() < secondChar.getXUpperLeftCorner() + secondChar.getWidth() &&
  66. firstChar.getXUpperLeftCorner() + firstChar.getWidth() > secondChar.getXUpperLeftCorner() &&
  67. firstChar.getYUpperLeftCorner() < secondChar.getYUpperLeftCorner() + secondChar.getHeight() &&
  68. firstChar.getYUpperLeftCorner() + firstChar.getHeight() > secondChar.getYUpperLeftCorner())
  69. {
  70. return true;
  71. }
  72. else
  73. {
  74. return false;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement