蛇体部位定位不正确

时间:2014-04-07 17:45:52

标签: c# xna

我有一个功能最强大的蛇游戏。老兄四处走动,吃水果,长出身体。

增加的身体部位紧挨着前一个蛇形片出现,而不是出现在各种各样的“链条”中。

更新和绘制身体部位的代码:

for (int i = snake.Count - 1; i >= 0; i--)
{
    if (i == 0)
    {

        Snakepart head = snake[0];

        // Collision with Screen Boundaries
        if (head.snakePartRectangle.X <= 0 || 
            head.snakePartRectangle.X >= screenWidth ||
            head.snakePartRectangle.Y <= 0 ||
            head.snakePartRectangle.Y >= screenHeight)
        {
            headExists = false;
        }

        //Collision with Body
        //for (int j = 1; j < snake.Count; j++) {
        //  if (head.snakePartRectangle.Intersects(snake[j].snakePartRectangle))
        //      this.Exit ();
        //}

        // Collision with Food
        if (head.snakePartRectangle.Intersects(foodRectangle))
        {
            Snakepart part = new Snakepart();
            //part.snakePartRectangle = snake [snake.Count - 1].snakePartRectangle;
            part.snakePosition.X = snake[snake.Count - 1].snakePosition.X;
            part.snakePosition.Y = snake[snake.Count - 1].snakePosition.Y;
            part.snakeTexture = this.Content.Load < Texture2D > ("pacman");
            snake.Add(part);
            foodEaten = true;
        }
    }
    else if (i > 0)
    {
        snake[i].snakePosition = snake[i - 1].snakePosition;
    }
}



base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself. 
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    // Clear the backbuffer
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();

    spriteBatch.Draw(foodTexture, foodRectangle, Color.White);

    for (int i = 0; i < snake.Count; i++)
    {
        spriteBatch.Draw(snake[i].snakeTexture, snake[i].snakePosition, Color.Blue);

    }

    spriteBatch.End();

    //TODO: Add your drawing code here
    base.Draw(gameTime);

    #endregion
}

图片:

它的目的是成为一群蓝色的pacmans,不像图像中那样重叠。

是什么导致身体部位如此紧密地咬合前一部分?


编辑:我将此添加到更新正文部分的逻辑中。

else if (i > 0) {
                if (moveRight) {
                    snake [i].snakePosition.X = snake [i - 1].snakePosition.X - 26;
                    snake [i].snakePosition.Y = snake [i - 1].snakePosition.Y;
                }

                if (moveLeft) {
                    snake [i].snakePosition.X = snake [i - 1].snakePosition.X + 26;
                    snake [i].snakePosition.Y = snake [i - 1].snakePosition.Y;
                }
                if (moveUp) {
                    snake [i].snakePosition.X = snake [i - 1].snakePosition.X;
                    snake [i].snakePosition.Y = snake [i - 1].snakePosition.Y + 26;
                }
                if (moveDown) {
                    snake [i].snakePosition.X = snake [i - 1].snakePosition.X;
                    snake [i].snakePosition.Y = snake [i - 1].snakePosition.Y - 26;
                }
            }

它们有适当的间距,但问题的一部分是它以更新速率重新定位,因此身体部位没有做经典的“跟随领导者”运动。试着想出来。

照片:

如果我改变方向,他们都会非常快速地移动到新的方向。我认为,以某种方式减缓这种情况会解决这个问题。

编辑2:请求绘制方法

protected override void Draw (GameTime gameTime)
    {
        // Clear the backbuffer
        graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

        spriteBatch.Begin ();

        spriteBatch.Draw (foodTexture, foodRectangle, Color.White);

        for (int i = 0; i < snake.Count; i++)
        {
            spriteBatch.Draw (snake [i].snakeTexture, snake [i].snakePosition, Color.Blue);

        }

        spriteBatch.End ();

        //TODO: Add your drawing code here
        base.Draw (gameTime);

        #endregion
    }

1 个答案:

答案 0 :(得分:1)

这似乎是为作业使用错误的数据结构的情况。使用列表,您需要更新所有零件位置,并且容易出错。

我建议使用一个队列,只是将新部件作为一个新头部进行排队,如果蛇不生长,则将部件从尾部出队 - 这样你只需要重新计算新的头部位置。