Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "Character.h"
- #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;
- }
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
- window.close();
- }
- window.clear();
- window.draw(secondChar);
- window.draw(firstChar);
- window.display();
- }
- return 0;
- }
- bool didHit(Character charOne, Character charTwo)
- {
- // replace this with the code from your group
- float oneX = charOne.getXUpperLeftCorner();
- float secX = charTwo.getXUpperLeftCorner();
- float secWidth = charTwo.getWidth();
- if (oneX > secX - charOne.getWidth() && oneX < (secX + secWidth) && charOne.getYUpperLeftCorner() > charTwo.getYUpperLeftCorner() - charOne.getHeight() && charOne.getYUpperLeftCorner() < charTwo.getYUpperLeftCorner() + charTwo.getHeight()) {
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment