0

我正在尝试制作一个游戏,但我在这里遇到了一个小问题......我正在用箭头键移动一个图片框,以避免其他图片框......问题是,我的图片框移出了表格当我按左键太多次...我成功解决了右侧的这个问题(通过用另一个挡住图片框),但左侧版本仍然不起作用,我不知道为什么...

这是代码:

if (pictureBox7.Bounds.IntersectsWith(pictureBox1.Bounds))
            switch (e.KeyCode)
            {
                case Keys.Escape: Application.Exit(); break;
                case Keys.P: timerkunai1.Enabled = false;
                    timerkunai2.Enabled = false; timerkunai3.Enabled = false;
                    timerkunai4.Enabled = false; timerninja.Enabled = false;
                    timerlife.Enabled = false;
                    button3.Show(); break;
                case Keys.Right: i = 6; dx = 25; press = true; break;            
            }
        if (pictureBox8.Bounds.IntersectsWith(pictureBox1.Bounds))
            switch (e.KeyCode)
            {
                case Keys.Escape: Application.Exit(); break;
                case Keys.P: timerkunai1.Enabled = false;
                    timerkunai2.Enabled = false; timerkunai3.Enabled = false;
                    timerkunai4.Enabled = false; timerninja.Enabled = false;
                    timerlife.Enabled = false;
                    button3.Show(); break;
                case Keys.Left: i = 0; dx = -25; press = true; break;
            }
        else
            switch (e.KeyCode)
        {
            case Keys.Escape: Application.Exit(); break;
            case Keys.P: timerkunai1.Enabled = false;
                timerkunai2.Enabled = false; timerkunai3.Enabled = false;
                timerkunai4.Enabled = false; timerninja.Enabled = false;
                timerlife.Enabled = false;
                button3.Show(); break;
            case Keys.Left: i = 0; dx = -25; press = true; break;
            case Keys.Right: i = 6; dx = 25; press = true; break;
        }
4

2 回答 2

0

您的 dx 变量是否是 PictureBox 新位置的偏移量?然后将 Location.x 限制为 0:

 if (pictureBox1.Location.x + dx > 0)
     pictureBox1.Location += dx;

如果要限制左右大小,表单的宽度,请使用以下代码:

if ((pictureBox1.Location.x + dx > 0) && (pictureBox1.Location.x + dx < this.Size.Width - pictureBox1.Size.Width))
     pictureBox1.Location += dx;
于 2013-04-26T15:43:53.833 回答
0

您需要代码来检查图片框的边界是否在表单之外。如果图片框移动会导致它超出界限,则阻止该运动。像这样的伪代码: if (pictureBoxZ + dx < 0 || pictureBoxZ + dx > pictureBoxZ.Parent.Width) { //Deny Motion }

于 2013-04-26T15:34:39.757 回答