title: Moving Snake Logic in Processing
body: <p>I am in the process of making a Snake Game, but I am confused on how to implement the Snake's movement in Processing. I have created a class for the snake, which includes a function for movement that can detect key presses, but I'm stuck on how to actually code the movement of the snake. Can anyone give me a brief explanation on how to implement the Snake movement based on the code below?</p>
  
  <pre><code>int score = 0; 
  int unit = 20; 
  PFont courierNew24, courierNew40;
  ArrayList unitList;
  String direction = "right";
  String nextDirection = "";
  int directionCount = 0;
  
  class Snake
  {
    Snake() {
      unitList = new ArrayList();
      unitList.add(new Unit(4, 3));
      unitList.add(new Unit(4, 4));
      unitList.add(new Unit(4, 5));
      unitList.add(new Unit(4, 6));
      unitList.add(new Unit(4, 7));
      unitList.add(new Unit(4, 8));
    }
  
    void drawSnake()
    {
      for (int i = 0; i &lt; unitList.size (); i++)
      {
        Unit snakePiece = (Unit) unitList.get(i);
        snakePiece.drawSnakePiece();
      }
    }
  
    void moveSnake()
    {
      if (direction != "left" &amp;&amp; nextDirection == "right")
      {
          //Move Snake
      }
      if (direction != "right" &amp;&amp; nextDirection == "left")
      {
      }
      if (direction != "up" &amp;&amp; nextDirection == "down")
      {
      }
      if (direction != "down" &amp;&amp; nextDirection == "up")
      {
      }
    }
  }
  
  class Unit
  {
    int row, column;
  
    Unit (int unitRow, int unitColumn)
    {
      row = unitRow;
      column = unitColumn;
    }
  
    void drawSnakePiece()
    {
      fill(0, 255, 0);
      rect(column*unit, row*unit, unit, unit);
    }
  
    void drawApple()
    {
      fill(255, 0, 0);
      ellipse(column*unit+(unit/2), row*unit+(unit/2), unit, unit);
    }
  
    void collision(int unitRow, int unitColumn)
    {
      if (row == unitRow &amp;&amp; column == unitColumn)
      {
      }
    }
  }
  
  //Functions
  void scoreBoard()
  {
    fill(255);
    textFont(courierNew24, 24);
    text("Score: " + score, 20, 670);
  }
  
  void gameOver()
  {
    fill(255);
    textFont(courierNew40, 40);
    textAlign(CENTER, CENTER);
    text("Game Over, Score of " + score, 500, 350);
  }
  
  void setup()
  {
    size(1000, 700); 
    background(0); 
    courierNew24 = loadFont("CourierNewPSMT-24.vlw");
    courierNew40 = loadFont("CourierNewPSMT-40.vlw");
    scoreBoard();
  }
  
  void draw()
  {
    smooth();
    Snake snake = new Snake();
    snake.drawSnake();
    snake.moveSnake();
  
    Unit apple = new Unit(10, 10);
    apple.drawApple();
  }
  
  void keyPressed()
  {
    switch(key)
    {
    case 'a':
    case 'A':
      directionCount += 1;
      if (directionCount &gt; 1)
      {
        direction = nextDirection;
      }
      nextDirection = "left";
      break;
    case 'd':
    case 'D':
      directionCount += 1;
      if (directionCount &gt; 1)
      {
        direction = nextDirection;
      }
      nextDirection = "right";
      break;
    case 'w':
    case 'W':
      directionCount += 1;
      if (directionCount &gt; 1)
      {
        direction = nextDirection;
      }
      nextDirection = "up";
      break;
    case 's':
    case 'S':
      directionCount += 1;
      if (directionCount &gt; 1)
      {
        direction = nextDirection;
      }
      nextDirection = "down";
      break;
    }
  }
  </code></pre>