【问题标题】:C# Collision Check for PictureBox (Intersect)图片框的 C# 碰撞检查(相交)
【发布时间】:2023-04-03 04:35:02
【问题描述】:

长期以来,我一直在挑战这个问题。简单地说,我想避免两个图片框之间的碰撞。当看到pictureBox2 时,pictureBox1 必须停止。但是,我确实设法通过 Intersect 做到了这一点。就像下面的代码一样;

 private void method(PictureBox b)
    {
        if (pictureBox1.Bounds.IntersectsWith(b.Bounds)) {
            movement = false;}                                         
    }

问题是按键。我正在使用 KeyDown(W, A, S, D) 移动图片框1,当我按住键时,相交代码工作得很好。但是当我一个个按到键时,它只是滑入了pictureBox2。我怎样才能避免这种情况?我确实尝试用布尔值来阻止它,但它没有用。我想我需要 if 语句,但我无法做出逻辑。寻找您的解决方案。提前致谢。

KeyDown 事件;

 private void Form1_KeyDown(object sender, KeyEventArgs e)
    {

        if (movement == true)
        {
            int x = pictureBox1.Location.X;
            int y = pictureBox1.Location.Y;

            if (e.KeyCode == Keys.W) y -= chrspeed;
            else if (e.KeyCode == Keys.A) x -= chrspeed;
            else if (e.KeyCode == Keys.D) x += chrspeed;
            else if (e.KeyCode == Keys.S) y += chrspeed;

            pictureBox1.Location = new Point(x, y);
            method(pictureBox2);

        }


    }

chrspeed 为 20;

KeyUp 事件;

private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        movement = true;


    }

【问题讨论】:

  • 获取tour、阅读How to Askminimal reproducible examplemethod 实际上是一个方法的坏名。
  • 请显示您的“按键”方法和更多相关内容(您显示的代码没有多大帮助)。
  • 按键事件为空。我认为问题与 KeyPress 事件有关,这就是我提到的原因。 @PoulBak
  • 你很幸运,它是一个接一个的步进失败。将矩形靠近在一起,然后设置断点并开始调试。您可能无法获得预期的事件。
  • 方法只是为了澄清。如果你愿意,你可以称它为碰撞检查。它最初是 CLcheck。

标签: c# game-physics


【解决方案1】:

在您的代码中发生的事情和“工作”(它没有,你很幸运)是,第一次两个 picbox 相交时,picbox1 移动到 picbox2,method 返回false,下一次它没有不要输入if 语句。 您需要调用method()移动 picbox1 并然后检查true or false

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    int x = pictureBox1.Location.X;
    int y = pictureBox1.Location.Y;

    if (e.KeyCode == Keys.W) y -= chrspeed;
    else if (e.KeyCode == Keys.A) x -= chrspeed;
    else if (e.KeyCode == Keys.D) x += chrspeed;
    else if (e.KeyCode == Keys.S) y += chrspeed;

    method(new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height), pictureBox2 );

    if (movement == true)
    {
        pictureBox1.Location = new Point(x, y);
    }

}

private void method(Rectangle rect, PictureBox b)
{
    if (rect.IntersectsWith(b.Bounds)) {
        movement = false;}                                         
}

【讨论】:

  • 嗯,碰撞后它停止了移动。
  • 我试试这个,请稍等。
  • 我们正在寻找一个天才的家伙。太谢谢你了兄弟。
【解决方案2】:

我会将它放入 KeyDown 事件处理程序并删除 KeyUp 事件处理程序。您的代码的问题在于,每次您释放一个键时,movement 标志都会重置为 true。 这应该做你想做的事:

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
                int x = pictureBox1.Location.X;
                int y = pictureBox1.Location.Y;

                if (e.KeyCode == Keys.W) y -= chrspeed;
                else if (e.KeyCode == Keys.A) x -= chrspeed;
                else if (e.KeyCode == Keys.D) x += chrspeed;
                else if (e.KeyCode == Keys.S) y += chrspeed;

                Rectangle newRect = new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height);
                if (!newRect.Bounds.IntersectsWith(pictureBox2.Bounds)) {
                    pictureBox1.Location = new Point(x, y);
                }
            }
        }

不需要movement 标志。如果两个图片框不相交,它会设置新的位置。

请注意,当图片框发生碰撞时,您仍然可以按另一个键再次移开。

Edit:

您的评论完全正确,对此深表歉意。 我现在更改了代码,所以它使用新位置来计算它们是否相交。

【讨论】:

  • 我试过了,当它们碰撞时,它停止了移动。它已经碰撞了,所以它不会离开。
猜你喜欢
  • 2013-02-28
  • 1970-01-01
  • 2021-08-13
  • 2010-09-28
  • 2019-04-30
  • 2023-03-11
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多