在处理中移动蛇形逻辑

Moving Snake Logic in Processing

我正在制作 Snake Game,但我对如何在 Processing 中实现 Snake 的移动感到困惑。我为蛇创建了一个 class,其中包括一个可以检测按键的移动功能,但我一直在研究如何对蛇的移动进行实际编码。谁能根据下面的代码简单解释一下如何实现贪吃蛇运动?

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;
  }
}

简要说明:

  1. 您将所有蛇单位都放在一个列表中 - 这已经完成了。头和尾是列表的第一个和最后一个元素。所以它实际上是一个队列。
  2. 在每个刻度上,确定您应该移动的方向。例如,如果方向是左,那么下一个头部坐标将是相对于当前头部的(-1,0)。
  3. 在第 2 步中确定的坐标的头部位置插入新单元。
  4. 从列表(和屏幕)中删除尾部单元。

那就安排运动吧。如果在头部位置发现一个苹果,则初始化一个生长计数器。在每个 tick 上,如果 growth_counter > 0,则减少它并跳过尾部单元移除。因此,只有头部会移动,直到它长大。