Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <SFML\Graphics.hpp>
  2. #include <iostream>
  3.  
  4.  
  5. int main()
  6. {
  7.  
  8.  
  9. sf::RenderWindow window(sf::VideoMode(599, 490, 32), "Screen Menu");
  10.  
  11. const float gravity = 1;
  12. int groundHeight = 440;
  13. sf::Vector2f velocity(sf::Vector2f(0,0));
  14.  
  15. sf::RectangleShape rect(sf::Vector2f(20,20));
  16. rect.setPosition(0,0);
  17. rect.setFillColor(sf::Color::Red);
  18.  
  19. float moveSpeed = 0.1f, jumpSpeed = 0.5f;
  20.  
  21.  
  22. while (window.isOpen()) {
  23. sf::Event event;
  24.  
  25. while(window.pollEvent(event)) {
  26. switch (event.type) {
  27.  
  28. case sf::Event::Closed:
  29. window.close();
  30. break;
  31. }
  32. }
  33.  
  34. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  35. velocity.x = moveSpeed;
  36. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  37. velocity.x = -moveSpeed;
  38. else
  39. velocity.x = 0;
  40. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  41. velocity.y = -jumpSpeed;
  42.  
  43.  
  44.  
  45. if(rect.getPosition().y + rect.getSize().y < groundHeight || velocity.y < 0)
  46. {
  47. velocity.y += gravity;
  48. }
  49. else
  50. {
  51. rect.setPosition(rect.getPosition().x, groundHeight - rect.getSize().y);
  52. velocity.y = 0;
  53. }
  54. rect.move(velocity.x, velocity.y);
  55.  
  56. window.clear();
  57. window.draw(rect);
  58. window.display();
  59.  
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement