【问题标题】:XNA Collisions with multiple rectangles in listXNA 与列表中多个矩形的碰撞
【发布时间】:2013-02-05 16:57:43
【问题描述】:

我正在重制一款非常老的游戏。有线条(蠕虫),你可以用箭头键控制它们。每个玩家都有一个线条。问题是,当我尝试制作与我的矩形的当前位置与列表中以前的矩形之一发生冲突,它不起作用..如果你能帮助我,那就太棒了..非常感谢..

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D dotTexture;
    Rectangle dotRectangle;
    Vector2 dotCenter;
    Vector2 dotPosition;
    float dotRotation;
    Vector2 dotVelocity;
    float dotSpeed = 1.5f;

    bool smrt1 = false;

    Texture2D previousDotTexture;
    List<Vector2> previousDotsList = new List<Vector2>();
    List<Rectangle> previousRecsList = new List<Rectangle>();

    Random random = new Random();

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 800;
        graphics.PreferredBackBufferWidth = 800;
    }

    protected override void Initialize()
    {            
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        dotTexture = Content.Load<Texture2D>("dot2");
        dotPosition = new Vector2(random.Next(200, 600),random.Next(200, 600));

        previousDotTexture = Content.Load<Texture2D>("dot1");

    }

    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        if (Keyboard.GetState().IsKeyDown(Keys.Left))
            dotRotation += 0.1f;

        if (Keyboard.GetState().IsKeyDown(Keys.Right))
            dotRotation -= 0.1f;

        if (Keyboard.GetState().IsKeyDown(Keys.Space) && smrt1==true)
        {
            Thread.Sleep(500);
            dotPosition = new Vector2(random.Next(200, 600), random.Next(200, 600));
            previousDotsList.Clear();
            dotSpeed = 1.5f;
            smrt1 = false;
        }

        dotRectangle = new Rectangle((int)dotPosition.X, (int)dotPosition.Y, dotTexture.Width, dotTexture.Height);
        dotPosition = dotVelocity + dotPosition;
        dotCenter = new Vector2(dotRectangle.Width / 2, dotRectangle.Height / 2);

        dotVelocity.X = (float)Math.Sin(dotRotation) * dotSpeed;
        dotVelocity.Y = (float)Math.Cos(dotRotation) * dotSpeed;


        Vector2 previousDotsPos = dotPosition;
        Rectangle previousDotsRecs = new Rectangle((int)dotCenter.X, (int)dotCenter.Y, 2, 2);
        previousDotsList.Add(previousDotsPos);
        previousRecsList.Add(previousDotsRecs);

        CheckCollision();

        base.Update(gameTime);
    }

    private void CheckCollision()
    {
        foreach (Rectangle previousDotsRecs in previousRecsList)
        {
            if (dotRectangle.Intersects(previousDotsRecs))
            {
                smrt1 = true;
                dotSpeed = 0f;
            }
        }
    }


    private void DrawDots()
    {
        foreach (Vector2 previousDotsPos in previousDotsList)
            spriteBatch.Draw(previousDotTexture, previousDotsPos, Color.White);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();

        DrawDots();
        spriteBatch.Draw(dotTexture, dotPosition, null, Color.White, dotRotation, dotCenter, 1f, SpriteEffects.None, 0);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

【问题讨论】:

    标签: c# list foreach xna collision


    【解决方案1】:

    在我看来,你永远不会发生碰撞,因为每次游戏更新(每秒 30 次)你都会创建一个新的矩形,这并不好。所以你必须得到移动的矩形,在移动时增加它们的 x 和 y,然后检查交叉点。

    【讨论】:

    • 谢谢你.. :) 我会尝试改变它.. 还有.. 有没有办法在不需要制作这么多精灵的情况下制作我的运动“轨迹”?我认为这太浪费了。。一定有曲线或某事的某种方式。我不知道。..:(谢谢..
    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 2012-05-09
    • 2019-07-14
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多