Advertisement
Felanpro

Character bouncing

Mar 22nd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. int direction;
  2. int x, y;
  3.  
  4. int main()
  5. {
  6.  
  7.     sf::RenderWindow window(sf::VideoMode(800, 600), "Generic name for a window");
  8.     window.setFramerateLimit(70);
  9.     window.setVerticalSyncEnabled(true);
  10.  
  11.     sf::RectangleShape player(sf::Vector2f(40, 40));
  12.     player.setFillColor(sf::Color::Cyan);
  13.  
  14.     sf::Event event;
  15.     while (window.isOpen())
  16.     {
  17.         window.clear(sf::Color::White);
  18.         while (window.pollEvent(event))
  19.         {
  20.             if (event.type == sf::Event::Closed)
  21.                 window.close();
  22.  
  23.             if (event.type == sf::Event::KeyPressed)
  24.             {
  25.                 if (event.key.code == sf::Keyboard::A)
  26.                 {
  27.                     direction = 1;
  28.                 }
  29.                 else if (event.key.code == sf::Keyboard::W)
  30.                 {
  31.                     direction = 2;
  32.                 }
  33.                 else if (event.key.code == sf::Keyboard::D)
  34.                 {
  35.                     direction = 3;
  36.                 }
  37.                 else if (event.key.code == sf::Keyboard::S)
  38.                 {
  39.                     direction = 4;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         if (direction == 1)
  45.         {
  46.             if (x == 0)
  47.                 direction = 3;
  48.             else
  49.                 x -= 10;
  50.         }
  51.         else if (direction == 2)
  52.         {
  53.             if (y == 0)
  54.                 direction = 4;
  55.             else
  56.                 y -= 10;
  57.         }
  58.         else if (direction == 3)
  59.         {
  60.             if ((x + 40) == 800)
  61.                 direction = 1;
  62.             else
  63.                 x += 10;
  64.         }
  65.         else if (direction == 4)
  66.         {
  67.             if ((y + 40) == 600)
  68.                 direction = 2;
  69.             else
  70.                 y += 10;
  71.         }
  72.  
  73.         player.setPosition(x, y);
  74.  
  75.         window.draw(player);
  76.  
  77.         window.display();
  78.     }
  79.  
  80.     int pause; cin >> pause; //Pause the program
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement