Advertisement
Felanpro

Easiest and Fastest Way to implement gravity and jump (sfml)

Apr 14th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. float gravity = .3;
  2.  
  3.     sf::Event event;
  4.     while (window.isOpen())
  5.     {
  6.         while (window.pollEvent(event))
  7.         {
  8.             if (event.type == sf::Event::Closed)
  9.                 window.close();
  10.  
  11.             if (event.type == sf::Event::KeyPressed)
  12.             {
  13.                 if (event.key.code == sf::Keyboard::Space)
  14.                 {
  15.                     velocity.y = -8;
  16.                 }
  17.                 if (event.key.code == sf::Keyboard::Right)
  18.                 {
  19.                     velocity.x = 3;
  20.                 }
  21.                 else if (event.key.code == sf::Keyboard::Left)
  22.                 {
  23.                     velocity.x = -3;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         if (player.getPosition().y >= height / 2)
  29.         {
  30.             player.setPosition(player.getPosition().x, height / 2);
  31.         }
  32.         else
  33.         {
  34.             velocity.y += gravity;
  35.         }
  36.  
  37.         window.clear(sf::Color::White); //Clear
  38.  
  39.         player.move(velocity);
  40.         window.draw(player);
  41.  
  42.         window.display(); //Display
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement