title: How can I make my snake run or move continue moving in c++?
body: <p>I am a beginner in c++ and i made my first game, a snake game. I made it without any graphics library.</p>
  <p>So far the things were good, my snake was just running fine and eating the fruit and score is also increasing.</p>
  <p>At the moment my snake just runs while the key is pressed, but now I want to run my snake continuously and just change its direction with keys as we have seen in old snake games.</p>
  <p>So far I have tried many things from my side, like a loop and all, but the things just didn't work in the way I wanted it to be.</p>
  <p><strong>There is my code-</strong></p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;conio.h&gt;
  #include&lt;Windows.h&gt;
  using namespace std;
  bool gameOver;
  const int width = 20;
  const int height = 20;
  int x, y, fruitX, fruitY, score;
  int tailX[100], tailY[100];
  int n_Tail;
  enum eDirection {STOP = 0, LEFT , RIGHT ,UP , DOWN};
  eDirection dir;
  
  void setup()
  {
      gameOver = false;
      dir = STOP; 
      x = width / 2;
      y = height / 2;
      fruitX = rand() % width;
      fruitY = rand() % height;
      score = 0;
  }
  
  void draw()
  {
      system(&quot;cls&quot;);
      for(int i = 0 ;i &lt; width+1; i++)
      {
          cout &lt;&lt; &quot;#&quot;; //for Upper wall
      }
      cout &lt;&lt; &quot;\n&quot;;
  
      for (int i  = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width ; j++)
          {
              if (j==0)
              {
                  cout &lt;&lt;&quot;#&quot;;
  
              }
              if (i == y &amp;&amp; j == x)
              {
                  cout &lt;&lt;&quot;0&quot;;
              }
               else if (i == fruitY &amp;&amp; j == fruitX)
              {
                  cout &lt;&lt;&quot;f&quot;;
                  width - 1;
              }
              else if (j== width -1)
              {
                  cout &lt;&lt; &quot;#&quot;;            
              }
              else
              {
                  bool print = false;
                  for (int k = 0; k &lt;n_Tail; k++)     
                  {
                      if (tailX[k] == j &amp;&amp; tailY[k] ==  i )
                      {
                          cout &lt;&lt; &quot;o&quot;;
                          print = true;
                      }
                  }
                  if (!print)
                  {
                     cout &lt;&lt;&quot; &quot;;
                      
                  }
              }
              
          }
          cout &lt;&lt; &quot;\n&quot;;
      }
                                      
      for (int i = 0; i &lt; width+1; i++)
      {
          cout &lt;&lt; &quot;#&quot;; //for lower wall 
      }
      cout &lt;&lt;&quot;\n&quot;;
      cout &lt;&lt; &quot;Score = &quot; &lt;&lt; score;
  
  }
  
  void input()
  {
      
          switch (_getch())
          {
  
              case 'a': dir = LEFT;
                  break;
  
              case 'w': dir = UP;
                  break;
  
              case 's': dir = DOWN;
                  break;
  
              case 'd': dir = RIGHT;
                  break;
          }
      
  
  }
  
  void logics()
  {
      int prevX = tailX[0];
      int prevY = tailY[0];
      int prev2X, prev2Y;
      tailX[0] = x;
      tailY[0] = y;
  
      for (int i = 1; i &lt; n_Tail; i++)
      {   
  
          prev2X = tailX[i];
          prev2Y = tailY[i];
          tailX[i] = prevX;
          tailY[i] = prevY;
          prevX = prev2X;
          prevY = prev2Y;
          
                  
  
      }
      
          
      switch (dir)
      {
      case LEFT:
          x--;
          break;
      case RIGHT:
          x++;
          break;
      case UP:
          y--;
          break;
      case DOWN:
          y++;
          break;
      default:
          y--;
  
  
  
      }
          
      for (int i = 0; i &lt; n_Tail; i++)
      {
          if (tailX[i] == x &amp;&amp; tailY[i] == y)
          {
              gameOver = true;
          }
      }
      //if (x&gt; width||x&lt;0||y&gt;height||y&lt;0)
      //{
      //gameOver = true;
      //}
      if (x &gt; width-2)x = 0; else if (x &lt; 0)x = width - 2;
      if (y &gt; height-1)y = 0; else if (y &lt; 0)y = height - 1;
      {
  
      }
  
  
      if (x == fruitX &amp;&amp; y == fruitY)
      {
          score = score + 10;
          fruitX = rand() % width;
          fruitY = rand() % height;
          n_Tail++;
  
      }
  }
  int main()
  {
      setup();
      while (!gameOver)
      {
          draw();
          input();
          logics();
          Sleep(10);
  
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/RcbpV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RcbpV.jpg" alt="Game while running " /></a></p>
  <p>someone please help me with this so i can continue learning c++ peacefully.</p>