簡體   English   中英

列表中具有多個矩形的XNA碰撞

[英]XNA Collisions with multiple rectangles in list

我正在重制一個真正古老的游戲。.有線條(蠕蟲),您可以用箭頭鍵控制它們。.每個玩家都有一條線條。.問題是,當我嘗試與當前碰撞時我的矩形位置與列表中的先前矩形之一不起作用..如果您能幫助我,那將是非常棒的..非常感謝..

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);
    }
}

在我看來,您永遠不會碰到沖突,因為每次游戲更新(每秒30次)時,您都在創建一個新的矩形,這不好。 因此,您必須獲取移動的矩形,在移動時增加其x和y,然后檢查相交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM