Advertisement
Felanpro

Gravity Model SFML

Apr 7th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 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::RectangleShape square;
  8.     square.setSize(sf::Vector2f(50, 50));
  9.     square.setFillColor(sf::Color::Blue);
  10.     square.setPosition(400, 300);
  11.  
  12.     sf::Vector2f velocity(0, 0);
  13.  
  14.     int jumpSpeed = 10, gravity = 1;
  15.  
  16.     sf::Event event;
  17.     while (window.isOpen())
  18.     {
  19.         while (window.pollEvent(event))
  20.         {
  21.             if (event.type == sf::Event::Closed)
  22.                 window.close();
  23.         }
  24.  
  25.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  26.         {
  27.             velocity.y = -jumpSpeed;
  28.         }
  29.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  30.         {
  31.             velocity.x = jumpSpeed;
  32.         }
  33.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  34.         {
  35.             velocity.x = -jumpSpeed;
  36.         }
  37.         else
  38.         {
  39.             velocity.y += gravity;
  40.             velocity.x = 0;
  41.         }
  42.  
  43.         square.move(velocity);
  44.  
  45.         if (square.getPosition().y >= 500)
  46.         {
  47.             square.setPosition(square.getPosition().x, 500);
  48.         }
  49.  
  50.         window.clear(sf::Color::White); //Clear
  51.  
  52.         window.draw(square);
  53.  
  54.         window.display(); //Display
  55.     }
  56.  
  57.     int pause; cin >> pause; //Pause the program
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement