Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "Character.h"
- using namespace std;
- #ifdef _DEBUG
- #pragma comment(lib, "sfml-window-d.lib")
- #pragma comment(lib, "sfml-system-d.lib")
- #pragma comment(lib, "sfml-graphics-d.lib")
- #else
- #pragma comment(lib, "sfml-window.lib")
- #pragma comment(lib, "sfml-system.lib")
- #pragma comment(lib, "sfml-graphics.lib")
- #endif
- bool didHit(Character charOne, Character charTwo);
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(800, 600), "DV1594 Gruppuppgift: didHit");
- Character firstChar(100.f, 100.f, 80.f, 120.f, sf::Color::Yellow);
- Character secondChar(350.f, 250.f, 100.f, 90.f, sf::Color::Green);
- secondChar.setMovable(false);
- sf::Clock clock;
- sf::Time elapsedTimeSinceLastUpdate = sf::Time::Zero;
- sf::Time timePerFrame = sf::seconds(1.f / 60.f);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
- }
- elapsedTimeSinceLastUpdate += clock.restart();
- while (elapsedTimeSinceLastUpdate >= timePerFrame)
- {
- elapsedTimeSinceLastUpdate -= timePerFrame;
- firstChar.move();
- if (didHit(firstChar, secondChar))
- {
- std::cout << "HIT!!" << std::endl;
- }
- else
- {
- std::cout << "NO HIT!!" << std::endl;
- }
- }
- window.clear();
- window.draw(secondChar);
- window.draw(firstChar);
- window.display();
- }
- getchar();
- return 0;
- }
- bool didHit(Character firstChar, Character secondChar)
- {
- if (firstChar.getXUpperLeftCorner() < secondChar.getXUpperLeftCorner() + secondChar.getWidth() &&
- firstChar.getXUpperLeftCorner() + firstChar.getWidth() > secondChar.getXUpperLeftCorner() &&
- firstChar.getYUpperLeftCorner() < secondChar.getYUpperLeftCorner() + secondChar.getHeight() &&
- firstChar.getYUpperLeftCorner() + firstChar.getHeight() > secondChar.getYUpperLeftCorner())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement