title: How to move a rectangle on key press in sdl2 using a method
body: <p>I am using SDL library to create a pong game. I am at the point where I want the paddle to move up and down when the letter w is pressed on the keyboard. I am trying to move the rectangle when w is pressed. but it won't work. It worked before but it just keeps painting over the renderer instead of moving the whole rectangle.</p>
  
  <pre><code> #include &lt;iostream&gt;
  #include&lt;SDL.h&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  SDL_Window *window = NULL;
  SDL_Surface *screen = NULL;
  Uint32 white;
  Uint32 black;
  SDL_Renderer *renderer;
  const int HEIGHT = 700;
  const int WIDTH = 800;
  const int padH = 95;
  const int padW = 25;
  SDL_Event e;
  SDL_Rect player;
  SDL_Rect player2;
  SDL_Rect Ball;
  void init() 
  {
      SDL_Init(SDL_INIT_EVERYTHING);
      window = SDL_CreateWindow("Pong",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
      screen = SDL_GetWindowSurface(window);
      white = SDL_MapRGB(screen-&gt;format, 255, 255, 255);
      black= SDL_MapRGB(screen-&gt;format, 0,0,0);
      player.h = padH;
      player.w = padW;
      player2.h = padH;
      player2.w = padW;
  
      player.x = 20;
      player.y = HEIGHT / 2;
      player2.x = WIDTH-40;
      player2.y = HEIGHT / 2;
      Ball.h = 20;
      Ball.w = 20;
      Ball.x = WIDTH / 2;
      Ball.y = HEIGHT / 2;
  
  
  }
  
  void draw() 
  {
      SDL_SetRenderDrawColor(renderer,0,0,0,255);
      SDL_RenderClear(renderer);
  
      SDL_SetRenderDrawColor(renderer,255,255,255,255);
      SDL_RenderFillRect(renderer, &amp;Ball);
  
      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
      SDL_RenderFillRect(renderer, &amp;player);
  
      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
      SDL_RenderFillRect(renderer, &amp;player2);
  
      SDL_RenderPresent(renderer);
  
  
  }
  
  void gamelogic()
  {
      while (SDL_PollEvent(&amp;e) !=0) 
  
      {
          if (e.type = SDL_KEYDOWN)
          {
              switch (e.key.keysym.sym)
              {
              case SDLK_w:
                  SDL_RenderFillRect(renderer, &amp;player);
                  player.y = player.y + 1;
  
              }
  
          }
  
      }
  }
  int main(int arg, char*argv[]) {
      bool done = true;
  
      init();
      draw();
      gamelogic();
      while (done) 
      {
          SDL_PollEvent(&amp;e);
          if (e.type == SDL_QUIT)
          {
              done = false;
          }
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>when I put the key press in the while loop it just keeps painting instead of moving the rectangle and brushing it over</p>