botgob

TestDidHit

Nov 22nd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "Character.h"
  4.  
  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.  
  18. int main()
  19. {
  20. sf::RenderWindow window(sf::VideoMode(800, 600), "DV1594 Gruppuppgift: didHit ");
  21. Character firstChar(100.f, 100.f, 80.f, 120.f, sf::Color::Yellow);
  22. Character secondChar(350.f, 250.f, 100.f, 90.f, sf::Color::Green);
  23. secondChar.setMovable(false);
  24.  
  25. sf::Clock clock;
  26. sf::Time elapsedTimeSinceLastUpdate = sf::Time::Zero;
  27. sf::Time timePerFrame = sf::seconds(1.f/60.f);
  28.  
  29. while (window.isOpen())
  30. {
  31. sf::Event event;
  32. while (window.pollEvent(event))
  33. {
  34. if (event.type == sf::Event::Closed)
  35. {
  36. window.close();
  37. }
  38. }
  39.  
  40. elapsedTimeSinceLastUpdate += clock.restart();
  41. while (elapsedTimeSinceLastUpdate >= timePerFrame)
  42. {
  43. elapsedTimeSinceLastUpdate -= timePerFrame;
  44. firstChar.move();
  45.  
  46. if (didHit(firstChar, secondChar))
  47. {
  48. std::cout << "HIT!!" << std::endl;
  49. }
  50. else
  51. {
  52. std::cout << "NO HIT!!" << std::endl;
  53. }
  54. }
  55. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
  56. window.close();
  57. }
  58. window.clear();
  59. window.draw(secondChar);
  60. window.draw(firstChar);
  61. window.display();
  62. }
  63.  
  64.  
  65. return 0;
  66. }
  67.  
  68. bool didHit(Character charOne, Character charTwo)
  69. {
  70. // replace this with the code from your group
  71. float oneX = charOne.getXUpperLeftCorner();
  72. float secX = charTwo.getXUpperLeftCorner();
  73. float secWidth = charTwo.getWidth();
  74. if (oneX > secX - charOne.getWidth() && oneX < (secX + secWidth) && charOne.getYUpperLeftCorner() > charTwo.getYUpperLeftCorner() - charOne.getHeight() && charOne.getYUpperLeftCorner() < charTwo.getYUpperLeftCorner() + charTwo.getHeight()) {
  75. return true;
  76. }
  77. return false;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment