C# 蛇身部位定位不当

C# 蛇身部位定位不当,c#,xna,C#,Xna,我有一个功能基本正常的蛇游戏。花花公子到处走动,吃水果,长身体 增加的身体部位立即出现在前一条蛇的后面,而不是以某种“链子”的形式出现 更新和绘制车身零件的代码: for (int i = snake.Count - 1; i >= 0; i--) { if (i == 0) { Snakepart head = snake[0]; // Collision with Screen Boundaries if (head.

我有一个功能基本正常的蛇游戏。花花公子到处走动,吃水果,长身体

增加的身体部位立即出现在前一条蛇的后面,而不是以某种“链子”的形式出现

更新和绘制车身零件的代码:

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
}
这使它们之间的间隔适当,但部分问题是它正在以更新速度重新定位,因此身体部位没有进行经典的“跟随领导者”运动。现在想弄清楚

图片:

如果我改变方向,它们都会很快向新方向移动。我认为,以某种方式放慢速度会解决这个问题

编辑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
    }
protected override void Draw(游戏时间游戏时间)
{
//清除后缓冲区
graphics.GraphicsDevice.Clear(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(食物纹理、食物角、颜色、白色);
for(int i=0;i
这似乎是在作业中使用错误的数据结构。使用该列表,您需要更新所有零件位置,这很容易出错


我建议使用一个队列,将新的部分作为新的头部排队,如果蛇没有生长,则从尾部将部分出列-这样您只需要重新计算新的头部位置。

新的peice没有任何偏移,我想它只是偏移了一点点,因为在检查交叉点之前,您已经更新了前面的工件位置。当添加一个新的peice时,你需要提供一个offset by offset,你是说在新的snakePosition.X上添加+26(精灵的宽度/高度)吗?当我这样做的时候,它会闪烁并弹回到头部。是的,基本上,但你需要找出蛇的哪一边将其添加到你可以发布你的绘制方法。问题在于绘制method@HackerMan添加了draw方法。好的,我将查看队列。“我还没有听说过这种类型。@zencha我建议你看看这个网站,它提供了关于c#的不同特性以及几种常见数据结构的简短主题教程。
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
    }