C# 当picturebox与其他对象碰撞时,如何阻止它进一步移动?

C# 当picturebox与其他对象碰撞时,如何阻止它进一步移动?,c#,visual-studio-2013,C#,Visual Studio 2013,我制作了一个类似于C语言的游戏,其中角色是一个图片框,并使用箭头键移动它。我希望他在遇到固体物体时停下来。我使用不可见的方形面板来定义禁区,但当他接触面板时,我使用的代码使他朝相反的方向移动,我按下了另一个键 if (e.KeyCode == Keys.Right) { j_figure = 2; x += velocity; if ((playerBox.Bounds.IntersectsWith(pa

我制作了一个类似于C语言的游戏,其中角色是一个图片框,并使用箭头键移动它。我希望他在遇到固体物体时停下来。我使用不可见的方形面板来定义禁区,但当他接触面板时,我使用的代码使他朝相反的方向移动,我按下了另一个键

 if (e.KeyCode == Keys.Right)
        {
            j_figure = 2;
            x += velocity;

            if ((playerBox.Bounds.IntersectsWith(panel1.Bounds) || playerBox.Bounds.IntersectsWith(panel2.Bounds)))
            x -= velocity - 10;
            playerBox.Location = new Point(x, y);
        }
        else if (e.KeyCode == Keys.Left)
        {
            j_figure = 1;
            x -= velocity;

            if ((playerBox.Bounds.IntersectsWith(panel1.Bounds) || playerBox.Bounds.IntersectsWith(panel2.Bounds)))
                x += velocity + 10;
            playerBox.Location = new Point(x, y);
        }
        else if (e.KeyCode == Keys.Up)
        {
            j_figure = 3;
            y -= velocity;

            if ((playerBox.Bounds.IntersectsWith(panel1.Bounds) || playerBox.Bounds.IntersectsWith(panel2.Bounds)))
                y += velocity + 10;
            playerBox.Location = new Point(x, y);
        }
        else if (e.KeyCode == Keys.Down)
        {
            j_figure = 0;
            y += velocity;

            if ((playerBox.Bounds.IntersectsWith(panel1.Bounds) || playerBox.Bounds.IntersectsWith(panel2.Bounds)))
                y -= velocity - 10;
            playerBox.Location = new Point(x, y);
        }

为了使对象完全停止在禁止区域,必须根据区域边界及其自身大小设置其位置。对于左键和右键,这将是:

if (e.KeyCode == Keys.Right) {
    j_figure = 2;
    x += velocity;
    if (playerBox.Bounds.IntersectsWith(panel1.Bounds)) {
        x = panel1.Left - playerBox.Width - 1;
    } else if (playerBox.Bounds.IntersectsWith(panel2.Bounds)) {
        x = panel2.Left - playerBox.Width - 1;
    }
} else if (e.KeyCode == Keys.Left) {
    j_figure = 1;
    x -= velocity;
    if (playerBox.Bounds.IntersectsWith(panel1.Bounds)) {
        x = panel1.Right + 1;
    } else if (playerBox.Bounds.IntersectsWith(panel2.Bounds)) {
        x = panel2.Right + 1;
    }
} ...
playerBox.Location = new Point(x, y);
该逻辑必须相应地应用于垂直方向


使不可见区域可见以进行测试。

我终于做到了。通过添加 playerBox.Location=新点x,y; 每一个动作里面都有按键动作。更具体地说:

    if (e.KeyCode == Keys.Right)
        {
            j_figure = 2;

            x += velocity;
            playerBox.Location = new Point(x, y);

            if (playerBox.Bounds.IntersectsWith(panel1.Bounds))
            {
                x = panel1.Left - playerBox.Width;
            }
            else if (playerBox.Bounds.IntersectsWith(panel2.Bounds))
            {
                x = panel2.Left - playerBox.Width;
            }
         }
...

// code for other directions
...
// again but this time outside the KeyCode if statements
playerBox.Location = new Point(x, y);
}

听起来您需要理解所编写的代码并学习使用调试器。。如果效果不是你所期望的,也许你需要改变你的物体移动的方向,听起来像是XY的问题,但这并不完全有效。当角色发生这种情况时,例如:当他向左移动时,恰好停在对象上,然后我按下(让我们说)键向上移动,他会重新出现在对象的底部。相反的情况是,当他停在面板的右侧,我按下向下键,他会重新出现在面板的顶部。可能有一个像素重叠,所以在右侧加一个像素,在左侧减一个像素。