Advertisement
Felanpro

Gravity SFML

Apr 7th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. int main()
  2. {
  3.     sf::RenderWindow window(sf::VideoMode(800, 600), "In Development");
  4.     window.setFramerateLimit(70);
  5.     window.setVerticalSyncEnabled(true);
  6.  
  7.     sf::Time time = sf::seconds(0);
  8.     sf::Clock timer;
  9.  
  10.     sf::Vector2f velocity(0, 3);
  11.     int gravity = 1;
  12.  
  13.     sf::RectangleShape square;
  14.     square.setSize(sf::Vector2f(40, 40));
  15.     square.setFillColor(sf::Color::Blue);
  16.     square.setPosition(400, 0);
  17.  
  18.     sf::Event event;
  19.     while (window.isOpen())
  20.     {
  21.         while (window.pollEvent(event))
  22.         {
  23.             if (event.type == sf::Event::Closed)
  24.                 window.close();
  25.         }
  26.  
  27.         time = timer.getElapsedTime();
  28.         if (time.asSeconds() > .3)
  29.         {
  30.             timer.restart();
  31.             velocity.y += gravity;
  32.         }
  33.  
  34.         square.move(velocity);
  35.  
  36.         window.clear(sf::Color::White); //Clear
  37.  
  38.         window.draw(square);
  39.  
  40.         window.display(); //Display
  41.     }
  42.  
  43.     int pause; cin >> pause; //Pause the program
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement