检查Picturebox是否触摸屏幕C#上的任何对象

时间:2014-11-18 09:57:00

标签: c# winforms

我的游戏问题是用C#编码的。我试过在论坛上搜索这个问题,但没有一个适用于这种情况:/

我正在尝试检查Picturebox是否在屏幕上点击了其他任何Picturebox。

我试过这个,但是当我添加更多对象时,这可能会太慢。

if (!square.Bounds.IntersectsWith(grass.Bounds) && !square.Bounds.IntersectsWith(grMiddle.Bounds) && !square.Bounds.IntersectsWith(grRight.Bounds) && !square.Bounds.IntersectsWith(middle.Bounds) && !square.Bounds.IntersectsWith(sideRight.Bounds) && !square.Bounds.IntersectsWith(topPanel.Bounds))

这种方法不起作用,因为玩家移动增加了100倍并且我立即死亡,不知怎的......

foreach (PictureBox pic in this.Controls.OfType<PictureBox>())
        {
            if (pic != square)  // exclude square PictureBox from the test
            {
                if (!square.Bounds.IntersectsWith(pic.Bounds))
                {
                    if (!square.Bounds.IntersectsWith(rightGoal.Bounds))
                    {
                        if (Right) { square.Left += 5; }
                        if (Left) { square.Left -= 5; }
                        if (Up) { square.Top -= 5; }
                        if (Down) { square.Top += 5; }
                    }
                    else
                    {
                        pally.points++;
                        rightPoints.Text = pally.points.ToString();
                        square.Location = new Point(690, 533);
                    }
                }
                else
                {
                    square.Location = new Point(690, 533);
                }
            }
        }

我没有想法,一些帮助会很好:)

1 个答案:

答案 0 :(得分:2)

                    if (Right) { square.Left += 5; }
                    if (Left) { square.Left -= 5; }
                    if (Up) { square.Top -= 5; }
                    if (Down) { square.Top += 5; }

这些测试键盘输入是什么?您正在使用此代码移动您的方块。而且你正在为表单上的每个图片框做这件事。这就是你的广场走得太远的原因。将您的职位更新代码与交叉代码分开可以解决您的问题。