Advertisement
Felanpro

Jumping and Gravity

Apr 14th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. int width = 800;
  9. int height = 600;
  10.  
  11.  
  12. int main()
  13. {
  14. sf::RenderWindow window(sf::VideoMode(width, height), "In Development");
  15. window.setFramerateLimit(70);
  16. window.setVerticalSyncEnabled(true);
  17.  
  18. sf::RectangleShape player; //I WENT IN AND CHANGED A BIT OF THE CODE IN THIS CLASS RESTORE IT LATER ON
  19. player.setPosition(width / 2, height / 2);
  20. player.setSize(sf::Vector2f(50, 50));
  21. player.setFillColor(sf::Color::Cyan);
  22. sf::Vector2f velocity(0, 0);
  23. float gravity = .4;
  24.  
  25. sf::Time time = sf::seconds(0);
  26. sf::Clock clock;
  27. bool truth = false;
  28. bool increase_downward_acceleration = false;
  29.  
  30. sf::Event event;
  31. while (window.isOpen())
  32. {
  33. while (window.pollEvent(event))
  34. {
  35. if (event.type == sf::Event::Closed)
  36. window.close();
  37.  
  38. if (event.type == sf::Event::KeyPressed)
  39. {
  40. if (event.key.code == sf::Keyboard::Space)
  41. {
  42. if (player.getPosition().y > (height/2) - 200)
  43. {
  44. truth = true;
  45. increase_downward_acceleration = false;
  46. }
  47. }
  48. if (event.key.code == sf::Keyboard::Right)
  49. {
  50. velocity.x = 3;
  51. }
  52. else if (event.key.code == sf::Keyboard::Left)
  53. {
  54. velocity.x = -3;
  55. }
  56. }
  57. }
  58.  
  59. //Handle inputs
  60.  
  61. if (player.getPosition().y <= (height / 2) - 200)
  62. {
  63. increase_downward_acceleration = true;
  64. truth = false;
  65. }
  66. else if (truth == true)
  67. {
  68. velocity.y = -7;
  69. increase_downward_acceleration = false;
  70. }
  71.  
  72. cout << player.getPosition().y << endl;
  73.  
  74. if (increase_downward_acceleration)
  75. velocity.y += gravity;
  76.  
  77. if (player.getPosition().y >= (height / 2) + 50 && truth == false)
  78. {
  79. velocity.y = 0;
  80. velocity.x = 0;
  81. }
  82.  
  83. window.clear(sf::Color::White); //Clear
  84.  
  85. player.move(velocity);
  86. window.draw(player);
  87.  
  88. window.display(); //Display
  89. }
  90.  
  91. int pause; cin >> pause; //Pause the program
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement