提问者:小点点

在处理中移动Snake逻辑


我正在制作一个贪吃蛇游戏,但我对如何在处理中实现贪吃蛇的运动感到困惑。我已经为贪吃蛇创建了一个类,其中包含一个可以检测按键的运动函数,但我被困在如何实际编码贪吃蛇的运动上。有人能根据下面的代码给我简单解释一下如何实现贪吃蛇的运动吗?

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个答案

匿名用户

简要说明:

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

这将安排移动。如果你在头部位置找到一个苹果,初始化一个增长计数器。在每个滴答声中,如果growth_counter