C#-防止图片框离开边界

时间:2019-02-01 12:36:34

标签: c# winforms graphics

我有一个名为player的图片框和4个图片框,它们在游戏开始时作为边界。如果按下某个键,我将执行以下代码:

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

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

            if (player.Bounds.IntersectsWith(openspot1.Bounds) || player.Bounds.IntersectsWith(openspot2.Bounds) || player.Bounds.IntersectsWith(openspot3.Bounds) || player.Bounds.IntersectsWith(openspot4.Bounds))
            {
                player.Location = new Point(player.Location.X - 1, player.Location.Y - 1);
            }

            player.Location = new Point(x, y);
        }

如何移动播放器,但防止其离开边界?

a

1 个答案:

答案 0 :(得分:1)

有一种观点认为您的代码不正确,至少我是这样认为的。

  1. 您总是将播放器移动到新位置。您要检查他是否触及边界。如果他触摸边界,则将他向上移动一个像素,向左移动一个像素,仅将他移动,然后将其移动7像素到选定的方向。 因此,在if内检查他是否触及边界时,您必须中断操作,而不执行设置新位置的其余代码。一个简单的return;就可以了。

  2. 如果按下键,您将执行边界检查,如果播放器没有碰到边界,则将移动播放器。这是错误的顺序。您必须检查玩家在移动后是否会触及边界,并且仅当他不会触碰边界时才移动。否则,您将把他移到边界,再也不会把他带出。

这是经过一些更正的代码:

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

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

    var tempPlayerPosition= player.Bounds; // get the players position and remember it temporary
    tempPlayerPosition.X = x; // change the players temporary pisition to the new one that it will have after the move
    tempPlayerPosition.Y = y;

    //check if the play would touch the boundyries if we use the new temporary pisition
    if (tempPlayerPosition.IntersectsWith(openspot1.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot2.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot3.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot4.Bounds))
    {
        return; //if he would touch the boundary, then do nothing
    }

    player.Location = new Point(x, y); //if he would not touch the boundary, move him to his new location
}

但是也许您也想

  • 如果按下的键不是W,S,A或D,则可以防止执行您的代码(即使他没有更改任何值)。
  • 使用switch代替if,这使其更具可读性。
  • 不必每次都写7时使用局部变量,因此,如果将来会更改,则只需更改一个值即可。

所以我的推荐如下:

private void HorrorForm_KeyDown(object sender, KeyEventArgs e)
{    
    var oneStep = 7; // define the amount of pixel the player will be moved
    var tempPlayerPosition = player.Bounds;// get the players position and remember it temporary

    switch (e.KeyCode) // check which key was presses
    {
        case Keys.D:
            tempPlayerPosition.X += oneStep; // move right
            break;
        case Keys.A:
            tempPlayerPosition.X -= oneStep; // move left
            break;
        case Keys.S:
            tempPlayerPosition.Y += oneStep; // move down
            break;
        case Keys.W:
            tempPlayerPosition.Y -= oneStep; // move up
            break;
         default: // you may wan't to do nothing if there any other key presses...
            return;
    }

    //check if the play would touch the boundyries if we use the new temporary pisition
    if (tempPlayerPosition.IntersectsWith(openspot1.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot2.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot3.Bounds) || 
        tempPlayerPosition.IntersectsWith(openspot4.Bounds))
    {
        return; //if he would touch the boundary, then do nothing
    }

    player.Location = new Point(tempPlayerPosition.X, tempPlayerPosition.Y);   //if he would not touch the boundary, move him to his new location
}

这对您有帮助吗?