我一直在尝试在XNA中制作一个蛇游戏,但我遇到了一个导致蛇全部搞砸了的错误。
正如你所看到的,每当我拿起一个“点”时,产生的每条新蛇都会向原始部分移动一点。
当游戏开始时,蛇只包含其中一个立方体。
这是产生蛇的新部分的代码。
private void PointCollision()
{
if (snake[0].getRect().Intersects(pointRectangle))
{
point.position = PointPosition();
pointRectangle = new Rectangle(
(int)point.position.X,
(int)point.position.Y,
point.texture.Width,
point.texture.Height);
score++;
if (xVel < 0)
snake.Add(
new Player(snakeTexture,
new Vector2(snake[snake.Count - 1].position.X + 20,
snake[snake.Count - 1].position.Y),
new Vector2(xVel, yVel)));
if (xVel > 0)
snake.Add(new Player(snakeTexture,
new Vector2(snake[snake.Count - 1].position.X - 20,
snake[snake.Count - 1].position.Y),
new Vector2(xVel, yVel)));
if (yVel < 0)
snake.Add(new Player(snakeTexture,
new Vector2(snake[snake.Count - 1].position.X,
snake[snake.Count - 1].position.Y + 20),
new Vector2(xVel, yVel)));
if (yVel > 0)
snake.Add(new Player(snakeTexture,
new Vector2(snake[snake.Count - 1].position.X,
snake[snake.Count - 1].position.Y - 20),
new Vector2(xVel, yVel)));
}
}
造成这种奇怪行为的原因是什么?