Processing 处理中的移动Snake逻辑

Processing 处理中的移动Snake逻辑,processing,Processing,我正在制作一个蛇游戏,但我不知道如何在处理过程中实现蛇的运动。我已经为蛇创建了一个类,其中包括一个可以检测按键的运动函数,但我一直在研究如何对蛇的运动进行编码。谁能给我一个简短的解释,如何实现蛇运动的基础上的代码如下 int score = 0; int unit = 20; PFont courierNew24, courierNew40; ArrayList unitList; String direction = "right"; String nextDirection = "";

我正在制作一个蛇游戏,但我不知道如何在处理过程中实现蛇的运动。我已经为蛇创建了一个类,其中包括一个可以检测按键的运动函数,但我一直在研究如何对蛇的运动进行编码。谁能给我一个简短的解释,如何实现蛇运动的基础上的代码如下

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 < unitList.size (); i++)
    {
      Unit snakePiece = (Unit) unitList.get(i);
      snakePiece.drawSnakePiece();
    }
  }

  void moveSnake()
  {
    if (direction != "left" && nextDirection == "right")
    {
        //Move Snake
    }
    if (direction != "right" && nextDirection == "left")
    {
    }
    if (direction != "up" && nextDirection == "down")
    {
    }
    if (direction != "down" && 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 && 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 > 1)
    {
      direction = nextDirection;
    }
    nextDirection = "left";
    break;
  case 'd':
  case 'D':
    directionCount += 1;
    if (directionCount > 1)
    {
      direction = nextDirection;
    }
    nextDirection = "right";
    break;
  case 'w':
  case 'W':
    directionCount += 1;
    if (directionCount > 1)
    {
      direction = nextDirection;
    }
    nextDirection = "up";
    break;
  case 's':
  case 'S':
    directionCount += 1;
    if (directionCount > 1)
    {
      direction = nextDirection;
    }
    nextDirection = "down";
    break;
  }
}
int评分=0;
整数单位=20;
PFont courierNew24、courierNew40;
ArrayList单位列表;
字符串方向=“右”;
字符串nextDirection=“”;
int directionCount=0;
蛇类
{
蛇(){
unitList=新的ArrayList();
增加(新单元(4,3));
增加(新单元(4,4));
增加(新单元(4,5));
增加(新单元(4,6));
增加(新单元(4,7));
增加(新单元(4,8));
}
void drawSnake()
{
对于(int i=0;i1)
{
方向=下一个方向;
}
nextDirection=“left”;
打破
案例“d”:
案例“D”:
方向计数+=1;
如果(方向计数>1)
{
方向=下一个方向;
}
nextDirection=“右”;
打破
案例“w”:
案例“W”:
方向计数+=1;
如果(方向计数>1)
{
方向=下一个方向;
}
nextDirection=“向上”;
打破
案例s:
案例S:
方向计数+=1;
如果(方向计数>1)
{
方向=下一个方向;
}
nextDirection=“向下”;
打破
}
}
简要说明:

  • 你把所有的蛇单位都放在一个列表中——这已经完成了。头和尾是列表的第一个和最后一个元素。所以它实际上是一个队列
  • 在每个刻度上,确定您应该移动的方向。例如,如果方向为左,则下一个头部坐标将位于相对于当前头部的(-1,0)
  • 在列表中的头部位置插入新装置,坐标在步骤2中确定
  • 从列表(和屏幕)中删除尾部单元
  • 这将安排运动。如果在头部位置发现一个苹果,初始化一个生长计数器。在每个刻度上,如果生长计数器>0,则减小它并跳过尾部单元移除。因此,只有头部会移动,直到它长大