#include #include using namespace std; int main() { float Py; const float gravitation = 1; int Collision , GroundHeight = 499 , JumpSpeed = 5; sf::Vector2f Vel (sf::Vector2f (0,0)); sf::RectangleShape top(sf::Vector2f(500, 1)); top.setPosition(0,1); sf::RectangleShape ground(sf::Vector2f(500, 1)); ground.setPosition(0,499); sf::RectangleShape rect1(sf::Vector2f(120, 50)); sf::RectangleShape rect2(sf::Vector2f(120, 50)); rect1.setFillColor(sf::Color::Blue); rect1.setSize(sf::Vector2f(20, 20)); rect1.setPosition(0,50); rect2.setSize(sf::Vector2f(100, 100)); rect2.setPosition(100,100); rect2.setFillColor(sf::Color::Red); sf::Clock zeit; sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!"); window.setFramerateLimit(60); while (window.isOpen()) { float Pbottom = rect1.getPosition().y + rect1.getSize().y; float Pleft = rect1.getPosition().x ; float Pright = rect1.getPosition().x + rect1.getSize().x; float Ptop = rect1.getPosition().y; float Xbottom = rect2.getPosition().y + rect2.getSize().y; float Xleft = rect2.getPosition().x ; float Xright = rect2.getPosition().x + rect2.getSize().x; float Xtop = rect2.getPosition().y; float Gbottom = ground.getPosition().y + ground.getSize().y; float Gleft = ground.getPosition().x ; float Gright = ground.getPosition().x + ground.getSize().x; float Gtop = ground.getPosition().y; float Tbottom = top.getPosition().y + top.getSize().y; float Tleft = top.getPosition().x ; float Tright = top.getPosition().x + top.getSize().x; float Ttop = top.getPosition().y; if(Pright < Tleft || Pleft > Tright || Ptop > Tbottom || Pbottom < Ttop){ Collision = 0; }else { Collision = 1; cout <<"Collision"; } if(Pright < Xleft || Pleft > Xright || Ptop > Xbottom || Pbottom < Xtop){ Collision = 0; }else { Collision = 1; cout <<"Collision"; if(Collision == 1){ rect1.setPosition(0,0); } } //if(Pright < Gleft || Pleft > Gright || Ptop > Gbottom || Pbottom < Gtop){ //Collision = 0; //}else { // Collision = 1; //cout <<"Collision"; //} if(Collision == 1){ rect1.setPosition(0,0); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {rect1.move(-5 , 0);} if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {rect1.move(0 , -5);} if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {rect1.move(0 , 5);} if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {rect1.move(5 , 0);} else Vel.x = 0; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){ Vel.y = -JumpSpeed; rect1.move(Vel.x,Vel.y); } if(rect1.getPosition().y + rect1.getSize().y < GroundHeight){ Vel.y += gravitation; rect1.move(0,Vel.y); }else{ rect1.setPosition(rect1.getPosition().x , GroundHeight - rect1.getSize().x); } sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::Black); window.draw(top); window.draw(rect1); window.draw(rect2); window.draw(ground); window.display(); } return 0; }