Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-防止picturebox离开边界_C#_Winforms_Graphics - Fatal编程技术网

C#-防止picturebox离开边界

C#-防止picturebox离开边界,c#,winforms,graphics,C#,Winforms,Graphics,我有一个名为player的图片盒和4个在游戏开始时作为边界的图片盒。如果按下一个键,我会执行以下代码: private void HorrorForm_KeyDown(对象发送方,KeyEventArgs e) { intx=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.KeyC

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

private void HorrorForm_KeyDown(对象发送方,KeyEventArgs e)
{
intx=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=新点(player.Location.X-1,player.Location.Y-1);
}
player.Location=新点(x,y);
}
如何移动球员,但防止他离开边界


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

  • 你总是把球员移到他的新位置。你要检查他是否触及边界。如果他接触到边界,你将他向上移动一个像素,向左移动一个像素,然后将他移动7个像素到选定的方向。 因此,在
    if
    中,您检查他是否触及边界,您必须中断,并且不执行设置新位置的其余代码。一个简单的
    返回将完成此工作

  • 如果按下某个键,您将执行边界检查。如果播放器未触及边界,您将移动播放器。这是错误的顺序。你必须检查玩家在移动后是否会触摸边界,并且只有当他不会触摸边界时才能移动他。否则你会把他带到边界,再也不能把他带出去

  • 下面是您的代码和一些更正:

    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,则防止执行代码(即使他没有更改任何值)
    • 使用
      开关
      而不是
      如果
      这会使它更具可读性
    • 不要每次都写数字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
    }
    

    这对你有帮助吗?

    问题是,它之所以被命名为恐怖游戏,是因为它的马里奥在windows窗体中吃便士吗?如果你想做游戏,你应该看看monogame或unity。在visualstudio中,Monogame非常容易入门,而且更像C#。统一也很伟大,但与纯粹的C#有一点不同,更像是使用统一。@jreese实际上是一个犹太人用恐怖音乐开始收集硬币,如果你没能及时收集到,希特勒会尖叫一声跳到你的身上。(我不是种族主义者,我是犹太人,我只是喜欢黑色幽默)是的,谢谢!!