将图片框保留在表单的边框内

时间:2018-02-05 18:44:51

标签: c# visual-studio topdown

嗨,所以我在视觉工作室使用c#制作这个自上而下的射击游戏,由于某种原因,我无法阻止图片框移出边界,现在发生的事情是,如果我放置图片框在右下角然后它工作,但如果我将图片框放在中心这里是我的代码不起作用:

private void Level_1_of__desolor_KeyDown(object sender, KeyEventArgs e)
    {
        int xpos = CharacterMain.Location.X;
        int ypos = CharacterMain.Location.Y;
        if (e.KeyCode == Keys.A)
        {
            this.CharacterMain.Load(@"../Graphics\Energy L.png");
        }
        else if (e.KeyCode == Keys.D)
        {
            this.CharacterMain.Load(@"../Graphics\Energy R.png");
            CharacterMain.Left += speed;
        }
        else if (e.KeyCode == Keys.W)
        {
            this.CharacterMain.Load(@"../Graphics\Energy F.png");
            CharacterMain.Top -= 10;
        }
        else if (e.KeyCode == Keys.S)
        {
            this.CharacterMain.Load(@"../Graphics\Energy B.png");
            CharacterMain.Top += 10;
        }
        if (xpos > 12 && xpos < 1096 && ypos == 523)
        {
            if (ypos == 523)
            {
                CharacterMain.Top -= 10;
            }
        }
        if (CharacterMain.Location.Y < 523 && CharacterMain.Location.Y > 12)
        {
            if (CharacterMain.Location.X == 12)
            {
                CharacterMain.Left += 10;
            }
        }
        if (CharacterMain.Location.Y < 523 && CharacterMain.Location.Y > 12)
        {
            if (CharacterMain.Location.X == 12)
            {
                CharacterMain.Left += 10;
            }
        }
        if (CharacterMain.Location.X > 12 && CharacterMain.Location.X < 1102)
        {
            if (CharacterMain.Location.Y == 12)
            {
                CharacterMain.Top += 10;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为我的逻辑出来很愚蠢,基本上发生的事情是我正在做的事情就是这里的固定代码。

//Border Control
        #region
        if (xpos < 44)
        {
                CharacterMain.Left += 10;
        }
        if (ypos > 523)
        {
            CharacterMain.Top -= 10;
        }
        if (ypos < 89)
        {
            CharacterMain.Top += 10;
        }
        if (xpos > 1068)
        {
            CharacterMain.Left -= 10;
        }
        #endregion