Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- using namespace std;
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(800, 600), "Project");
- sf::RectangleShape player;
- sf::RectangleShape projectile;
- player.setSize(sf::Vector2f(50, 50));
- projectile.setSize(sf::Vector2f(50, 50));
- player.setFillColor(sf::Color::Green);
- projectile.setFillColor(sf::Color::Red);
- player.setPosition(300, 400);
- int x = 370; int y = 400;
- projectile.setPosition(x, y);
- sf::FloatRect boundingBox = player.getGlobalBounds();
- sf::Event event;
- while (window.isOpen())
- {
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::Right)
- x++;
- else if (event.key.code == sf::Keyboard::Left)
- x--;
- }
- }
- projectile.setPosition(x, y);
- sf::FloatRect otherboundingbox = projectile.getGlobalBounds();
- if (boundingBox.intersects(otherboundingbox))
- cout << "The two bodies are colliding" << endl;
- window.clear(sf::Color::White); //Clear
- window.draw(player);
- window.draw(projectile);
- window.display(); //Display
- }
- int pause; cin >> pause; //Pause the program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement