我的2D阵列中的命中检测是取出随机的人而不是它击中的人

时间:2017-06-10 01:03:05

标签: c# arrays multidimensional-array comments collision-detection

嘿,所以我在这里尝试将初始生命值改为0,一旦被击中。但是有了这个,它正在改变2D阵列中的一个随机敌人并使它们消失而不是被击中的那些。我已经评论过我的大部分代码希望它有帮助:) 找不到有相同问题的帖子抱歉,如果有...任何其他建议也会很棒!感谢

       public partial class Form1 : Form
    {
        //score & ammo
        int score = 0;
        int ammo = 5;

        //setting cannon
        int tank = 0;

        //mouse cords
        int mouseX = 0;
        int mouseY = 0;

        //Small boom 
        int Sboomx = -20;
        int Sboomy = -20;

        //random positions for enemys to spawn
        int MinX = 1020;
        int MaxX = 7020;
        int MinY = 350;
        int MaxY = 400;

        //speed of them
        int SpeedMinX = -7;
        int SpeedMaxX = -2;

        //array
        int[,] bads = new int[300, 5];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //setting positions and loading in people
            Random randNum = new Random();

            for (int i = 0; i < 300; i++)
            {
                //where they go
                bads[i,0] = randNum.Next(MinX, MaxX);
                bads[i, 1] = randNum.Next(MinY, MaxY);
                //health
                bads[i, 2] = 1;

                //how they move
                //dx
                bads[i, 3] = randNum.Next(SpeedMinX, SpeedMaxX);
                //dy
                bads[i, 4] = 1;
            }         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //background
            e.Graphics.DrawImage(Properties.Resources.Background1, 0, 0, 1000, 500);

            //Aim
            e.Graphics.DrawImage(Properties.Resources.crosshair, mouseX - 10, mouseY - 10, 20, 20);

            //explotion
            e.Graphics.DrawImage(Properties.Resources.small, Sboomx - 285, Sboomy - 298, 600, 600);

            //Tank cannon        
            if (tank == 1)
            {
                e.Graphics.DrawImage(Properties.Resources.Tank__5__copy, 140, 335, 60, 60);
            }
            else if (tank == 2)
            {
                e.Graphics.DrawImage(Properties.Resources.Tank__10__copy, 140, 335, 60, 60);
            }
            else if (tank == 3)
            {
                e.Graphics.DrawImage(Properties.Resources.Tank__15__copy, 140, 335, 60, 60);
            }
            else if (tank == 4)
            {
                e.Graphics.DrawImage(Properties.Resources.Tank_0__copy, 140, 335, 60, 60);
            }

            //draw Bad guys
            for (int i = 0; i < 300; i++)
            {             
                    if (bads[i, 2] == 1)
                    {
                        e.Graphics.FillRectangle(Brushes.Black, bads[i, 0] , bads[i, 1], 10, 20);
                    }
            }
            //undrawthem
            for (int i = 0; i < 300; i++)
            {
                if (bads[i, 2] == 0)
                {
                    e.Graphics.FillRectangle(Brushes.Black, bads[i, 0], bads[i, 1], 0, 0);
                    Sboomx = -20;
                    Sboomy = -20;
                }
            }
        }

        //Timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            //Collision 
            for (int i = 0; i < 300; i++)
            {
                if (Sboomx == bads[i, 0] || Sboomy == bads[i,1])
                {
                    bads[i, 2] = 0;
                    ammo++;
                    score = score + 5; ;
                }
            }

                //Score
            label2.Text = " " + score;

            //Ammo
            label1.Text = " " + ammo;

            //Moving enemys
            for (int i = 0; i < 300; i++)
            {
                bads[i, 0] = bads[i, 0] + bads[i, 3];
                bads[i, 1] = bads[i, 1] + bads[i, 4];
                if (bads[i, 0] > 2040 || bads[i, 0] < 140)
                {
                    bads[i, 3] = bads[i, 3] * -1;
                }
                if (bads[i, 1] > 335 || bads[i, 1] < 350)
                {
                    bads[i, 4] = bads[i, 4] * -1;
                }
            }
            this.Refresh();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            //Ammo
        }

        private void label2_Click(object sender, EventArgs e)
        {
            //Customers served
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            //Control tank cannon
            mouseY = e.Y;
            mouseX = e.X;

            if (mouseY > 350 && mouseY < 375)
            {
                tank = 1;
            }
            else if (mouseY > 376 && mouseY < 390)
            {
                tank = 2;
            }
            else if (mouseY > 391)
            {
                tank = 3;
            }
            else if (mouseY <= 339)
            {
                tank = 4;
            }
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //creates explotion and takes away ammo
            Sboomx = e.X;
            Sboomy = e.Y;
            ammo--;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            //moves explosion
            Sboomx = -20;
            Sboomy = -20;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你正在尝试做什么,那么,问题出在这一行:

if (Sboomx == bads[i, 0] || Sboomy == bads[i,1])

坏人是一个矩形,炸弹是一个点,你在这里做的是试图将该矩形的一个点与炸弹x坐标 y坐标相匹配,也是错误的,你需要检查炸弹坐标是否里面那个矩形。

此外,最好将“//碰撞”循环移动到Form1_MouseDown事件,以避免鼠标点击和计时器滴答之间的延迟,尤其是因为您重置了{{1}中的炸弹坐标事件。

另一件事,“// undrawthem”循环在这里没有逻辑,你正在绘制一个尺寸为0 * 0的矩形(你认为它会调整现有矩形的大小吗?),当你调用Form1_MouseUp时,{调用{1}}使整个表面无效,然后调用Refresh()来更新无效区域,以便在Invalidate()事件被提升时,无论现有事件是什么,都会清除并重新绘制整个表单图形之前。

您可以考虑这一点,而不是使用Update(),而是使用Form1_PaintRefresh(),因此您不会刷新整个表面:

Invalidate(invalidate_area)

但是如果你这样做了,你将需要照顾死去的坏人,移动坏人(更新旧的占用区域和新的占用区域)等等。