Advertisement
Felanpro

Move character

Mar 21st, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. int width = 800;
  2. int height = 600;
  3.  
  4. int x = width/2;
  5. int y = height/2;
  6.  
  7. int main()
  8. {
  9.     sf::RenderWindow window(sf::VideoMode(width, height), "Generic name for a window");
  10.     window.setFramerateLimit(60);
  11.  
  12.     sf::CircleShape circle(70.f);
  13.     circle.setFillColor(sf::Color::Black);
  14.     circle.setOutlineThickness(15.f);
  15.     circle.setOutlineColor(sf::Color::Green);
  16.  
  17.     sf::Event event;
  18.     while (window.isOpen())
  19.     {
  20.         window.clear(sf::Color::White);
  21.  
  22.         while (window.pollEvent(event))
  23.         {
  24.             if (event.type == sf::Event::Closed)
  25.                 window.close();
  26.  
  27.             if (event.type == sf::Event::KeyPressed)
  28.             {
  29.                 if (event.key.code == sf::Keyboard::Up)
  30.                 {
  31.                     if (y - 10 < 0)
  32.                         ;
  33.                     else
  34.                         y -= 10;
  35.                 }
  36.                 else if (event.key.code == sf::Keyboard::Down)
  37.                 {
  38.                     if (y + 10 > height)
  39.                         ;
  40.                     else
  41.                         y += 10;
  42.                 }
  43.                 else if (event.key.code == sf::Keyboard::Left)
  44.  
  45.                 {
  46.                     if (x - 10 < 0)
  47.                         ;
  48.                     else
  49.                         x -= 10;
  50.                 }
  51.                 else if (event.key.code == sf::Keyboard::Right)
  52.                 {
  53.                     if (x + 10 > width)
  54.                         ;
  55.                     else
  56.                         x += 10;
  57.                 }
  58.             }
  59.         }
  60.         circle.setPosition(x, y);
  61.  
  62.         window.draw(circle);
  63.  
  64.         window.display();
  65.     }
  66.  
  67.     int pause; cin >> pause; //Pause the program
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement