title: How draw a ball when i press a mouse button in C++/SFML
body: <p>I'm doing a game in C++ with SFML and I want to draw on the screen an ice-ball and see it "walking" on the screen when I press the left mouse button. But when I press the button it doesn't draw the ice-ball. Have you got some answers to fix the problem?</p>
  
  <p>Main:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;SFML/Graphics.hpp&gt;
  #include &lt;string&gt;
  
  int main(int argc, char* argv[])
  {
  
      sf::RenderWindow window;
      window.create(sf::VideoMode(1320, 840), "Game");
      window.setFramerateLimit(60);
      window.setVerticalSyncEnabled(true);
      sf::Color color(255, 255, 255, 100);
  
      sf::Time time = sf::seconds(2);
      sf::Clock clock;
      sf::Time time2 = clock.getElapsedTime();
  
      std::cout &lt;&lt; time.asSeconds() &lt;&lt; std::endl;
      std::cout &lt;&lt; time2.asSeconds() &lt;&lt; std::endl;
      //clock.restart();
  
      //TEXTURE
      sf::Texture bgTexture;
      if (!bgTexture.loadFromFile("file", sf::IntRect(0, 0, 1320, 840)))
      { std::cout &lt;&lt; "Error: background not loaded" &lt;&lt; std::endl; }
  
      sf::Texture pTexture;
      if (!pTexture.loadFromFile("file", sf::IntRect(0, 0, 32, 32)))
      { std::cout &lt;&lt; "Error: player not loaded" &lt;&lt; std::endl; }
  
      sf::Texture ibTexture;
      if (!ibTexture.loadFromFile("file"))
      { std::cout &lt;&lt; "Error: ice-ball not loaded" &lt;&lt; std::endl; }
  
      //SPRITE
      sf::Sprite background;
      background.setTexture(bgTexture);
  
      sf::Sprite player;
      player.setTexture(pTexture);
      player.setScale(2, 2);
      player.setPosition(628, 388);
  
      sf::Sprite iceBall;
      iceBall.setTexture(ibTexture);
      iceBall.setScale(1.5, 1.5);
  
      //FONT
      sf::Font font;
      if (!font.loadFromFile("file"))
      { std::cout &lt;&lt; "Error: font not loaded" &lt;&lt; std::endl; }
  
      //TEXT
      sf::Text commands;
      commands.setFont(font);
      commands.setCharacterSize(54);
      commands.setColor(sf::Color::Black);
      commands.setString("D=Right \nA=Left \nW=Up \nS=DWON \n(also the arrows)");
      commands.setPosition(213, 123);
  
      while (window.isOpen()) {
          sf::Event event;
          while (window.pollEvent(event)) {
              if (event.type == sf::Event::Closed)
              { window.close(); }
  
              if (event.type == sf::Event::KeyPressed) {
                  if (event.key.code == sf::Keyboard::D || event.key.code == sf::Keyboard::Right)
                  { player.move(10, 0); }
                  else if (event.key.code == sf::Keyboard::A || event.key.code == sf::Keyboard::Left)
                  { player.move(-10, 0); }
                  else if (event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::Up)
                  { player.move(0, -10); }
                  else if (event.key.code == sf::Keyboard::S || event.key.code == sf::Keyboard::Down)
                  { player.move(0, 10); }
              }
  
              /*if (event.type == sf::Event::MouseMoved)
              {
                  std::cout &lt;&lt; "X: " &lt;&lt; event.mouseMove.x &lt;&lt; " y: " &lt;&lt; event.mouseMove.y &lt;&lt; std::endl;
              }*/
  
              if (event.type == sf::Event::MouseButtonPressed) {
                  if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
                      std::cout &lt;&lt; "Left mouse button pressed" &lt;&lt; std::endl;
                      iceBall.setPosition(event.mouseMove.x, event.mouseMove.y);
                      window.draw(iceBall);
                      iceBall.move(event.mouseMove.x++, event.mouseMove.y++);
  
                  }
              }
          }
          window.clear(color);
  
          window.draw(background);
          window.draw(player);
          window.draw(commands);
  
          window.display();
      }
  
      return 0;
  }
  </code></pre>