Picturebox.bounds.intersectswith不起作用

问题描述 投票:0回答:1

我正在写一个看起来像《战城》的游戏。我仅使用Picturebox就使我的坦克能够发射导弹。但是,当我在敌人的坦克上进行测试时,它仍然无法通过对方。我该如何解决?

 public void MissileCollision()
    {
        foreach (var EnemyTank in EnemyTankArray)
        {
            foreach(var Missile in missilesArray)
            {
                if (EnemyTank.Bounds.IntersectsWith(Missile.Bounds))
                {
                    EnemyTank.Location = new Point(EnemyTank.Location.X + Position);
                }
            }
        }
    }

此代码说明,当我击中敌人坦克时,它会返回到顶部,其位置是在X坐标上随机产生的。

实际上是完整代码。

 Boolean moveLeft = false;
    Boolean moveRight = false;
    List<PictureBox> missilesArray = new List<PictureBox>();
    List<PictureBox> EnemyTankArray = new List<PictureBox>();
    Boolean shoots = true;
    Random rand = new Random();
    int Position = 0;
    public Game()
    {
        InitializeComponent();
    }
    public void enemyTank()
    {
        var EnemyTank = new PictureBox();
        this.Controls.Add(EnemyTank);
        EnemyTank.Width = 24;
        EnemyTank.Height = 31;
        Position = rand.Next(0, 286);
        EnemyTank.Location = new Point(EnemyTank.Location.X + Position);
        EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_DOWN_v_1;
        EnemyTank.BackgroundImageLayout = ImageLayout.Stretch;
        EnemyTankArray.Add(EnemyTank);
        EnemyTank.BringToFront();
    }
    public void createMissile()
    {
            var Missile = new PictureBox();
            this.Controls.Add(Missile);
            Missile.Width = 10;
            Missile.Height = 10;
            Missile.Top = MainTank.Top + MainTank.Height / 2 - Missile.Height / 2;
            Missile.Left = MainTank.Left + MainTank.Width / 2 - Missile.Width / 2;
            Missile.BackColor = Color.Black;
            missilesArray.Add(Missile);
            Missile.BringToFront();
    }
    public void MissileCollision()
    {
        foreach (var EnemyTank in EnemyTankArray)
        {
            foreach(var Missile in missilesArray)
            {
                if (EnemyTank.Bounds.IntersectsWith(Missile.Bounds))
                {
                    EnemyTank.Location = new Point(EnemyTank.Location.X + Position);
                }
            }
        }
    }
    private void MoveMainTank(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Space)
        {
            if (shoots == true)
            {
                createMissile();
            }
        }
       if(e.KeyData == Keys.Left)
        {
            moveLeft = true;
        }
       if(e.KeyData == Keys.Right)
        {
            moveRight = true;
        }
    }

    private void MoveSide(object sender, EventArgs e)
    {
        if(moveLeft == true)
        {
            shoots = false;
            MainTank.Left -= 10;
            MainTank.BackgroundImage = Properties.Resources.Tank_LEFT_v_1;
        }
        if (moveRight == true)
        {
            shoots = false;
            MainTank.Left += 10;
            MainTank.BackgroundImage = Properties.Resources.Tank_RIGHT_v_2;

        }
    }

    private void StopMainTank(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Left)
        {
            shoots = true;
            moveLeft = false;
            MainTank.BackgroundImage = Properties.Resources.Tank_TOP_v_1;
        }
        if (e.KeyData == Keys.Right)
        {
            shoots = true;
            moveRight = false;
            MainTank.BackgroundImage = Properties.Resources.Tank_TOP_v_1;
        }
    }

    private void Game_Load(object sender, EventArgs e)
    {
        enemyTank();
        MainTankMoveLeftRight.Start();
        shoot.Start();
        MoveEnemyTank.Start();
        MissileCollision();
    }

    private void ShootMissile(object sender, EventArgs e)
    {
        foreach(var Missile in missilesArray)
        {
            Missile.Top -= 10;
        }
    }

    private void EnemyTankForward(object sender, EventArgs e)
    {
        foreach(var EnemyTank in EnemyTankArray)
        {
            EnemyTank.Top += 10;
        }
    }
}

}

对于foreach,我实际上是通过延迟使其产生更多的EnemyTank,所以稍后再处理,我想重点说明的是,当我这样编码时它什么也没做,请帮助我解决这个问题。

c# picturebox intersection
1个回答
0
投票

唯一的答案是我添加了一个计时器,在使用表单加载使其工作后,MissileCollision将放置在计时器的刻度上

© www.soinside.com 2019 - 2024. All rights reserved.