title: Snake game moving issue [JAVA]
body: <p>I'm trying to write a Snake game in Java, as a start I figured out how to get the snake (I have only implemented its head for the moment) move right.
  My problem is that I can't get it to move down (the snake should continue going down even if the key is released) when the "down" key is pressed.
  Any ideas?</p>
  
  <p>Main code:</p>
  
  <pre><code>import javax.swing.JFrame;    
  
  public class Main {
  
      public static void main(String[] args) throws InterruptedException {
          JFrame frame = new JFrame();
          frame.setVisible(true);
          frame.setSize(500, 500);
          frame.setResizable(false);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  
          Window window = new Window();
          frame.add(window);
  
          while(true) {
  
              window.move();
  
              Thread.sleep(10);
          }           
  
      }
  
  }
  </code></pre>
  
  <p>Window code:</p>
  
  <pre><code>import java.awt.Graphics;    
  import javax.swing.JPanel;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  
  
  public class Window extends JPanel {
  
      public Snake snake = new Snake(this);
  
      public Window() {
  
          this.setLayout(null);
          setFocusable(true);
  
      }
  
      public void paint(Graphics g2d) {
          super.paint(g2d);
          snake.paint(g2d);
      }
  
      public void move() {
  
          snake.moveRight();
  
          this.addKeyListener(new KeyAdapter() {
              @Override
              public void keyPressed(KeyEvent event) {
                  if(event.getKeyCode() == KeyEvent.VK_DOWN) {
                      snake.setDx(0);
  
                  }
              }
  
              public void keyReleased(KeyEvent event) {
                  if(event.getKeyCode() == KeyEvent.VK_DOWN) {
                      snake.moveDown();
                  }
              }
  
              public void keyTyped(KeyEvent event) {
  
              }
  
          });
  
          this.repaint();
      }
  
  }
  </code></pre>
  
  <p>Snake code:</p>
  
  <pre><code>import java.awt.Graphics;
  
  public class Snake {
  
      Window window;
  
      private int positionX = 0;
      private int positionY = 0;
  
      private int dx = 1;
      private int dy = 1;
  
      public Snake(Window window) {
          this.window = window;
      }
  
      public void paint(Graphics g2d) {
          g2d.fillRect(positionX, positionY, 20, 20);
      }
  
      public int getPositionX() {
          return positionX;
      }
  
      public void setPositionX(int positionX) {
          this.positionX = positionX;
      }
  
      public int getPositionY() {
          return positionY;
      }
  
      public void setPositionY(int positionY) {
          this.positionY = positionY;
      }
  
      public int getDx() {
          return dx;
      }
  
      public void setDx(int dx) {
          this.dx = dx;
      }
  
      public int getDy() {
          return dy;
      }
  
      public void setDy(int dy) {
          this.dy = dy;
      }
  
      public void moveRight() {
          positionX += dx;
      }
  
      public void moveDown() {
          positionY += dy;
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance.</p>