首页 > 解决方案 > C# - 防止图片框离开边界

问题描述

我有一个名为 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);
        }

我如何移动玩家但防止他离开边界?

一个

标签: c#winformsgraphics

解决方案


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

  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 的键,则防止执行您的代码(即使他没有更改任何值)。
  • useswitch而不是ifthis 使它更具可读性。
  • 而不是每次都写入数字 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
}

这对你有帮助吗?


推荐阅读