如何通过网格移动对象

时间:2013-05-03 05:23:29

标签: c# arrays xna

我无法想出如何在我的蛇游戏中使用数组的解决方案。我在400x400尺寸的窗口上有一个20x20像素的头。

areaGrid = new Vector2[columns, rows];

for (int x = 0; x < columns; x++)
{
    for (int y = 0; y < rows; y++)
    {
        areaGrid[x, y] = new Vector2(x * 20, y * 20);
        Console.WriteLine("areaGrid[{0},{1}] = {2}", x, y, areaGrid[x, y]);
    }
}

很自然地,蛇的头部和尾巴可以占据400个“块”。我在[5,5]处的阵列中绘制了头部,它是网格上的坐标100,100。我希望头部一次移动20个像素,这是阵列中的一个新点。因此,例如,向右移动会将头部放置在阵列中的[5,6]处,并且将120,100放置在网格上。我只是不知道该怎么做。如何在更新方法中通过数组实现移动?

1 个答案:

答案 0 :(得分:0)

使用带有块坐标的列表:

List<Vector2> Snake = new List() { {5,5}, {5,6}, {5,7}, {6,7}, {7,7} }

并呈现它:

var transform = Matrix.CreateScale(20) * Matrix.CreateTranslation(offset);
SpriteBatch.Begin(null,null,..., tranform);
foreach (var pos in Snake)
  SpriteBatch.Draw(white_1x1_texture, pos, null, SnakeColor);
SpriteBatch.End();

SpriteBatch.Begin();
foreach (var pos in Snake) {
  var box = new Rectangle(offset.X + pos.X * 20, offset.Y + pos.Y*20, 20,20);
  SpriteBatch.Draw(white_1x1_texture, box, null, SnakeColor);
} 
SpriteBatch.End();