C#-防止picturebox离开边界
我有一个名为player的图片盒和4个在游戏开始时作为边界的图片盒。如果按下一个键,我会执行以下代码: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
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);
}
如何移动球员,但防止他离开边界
有一种观点认为您的代码不正确,至少我这么认为
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实际上是一个犹太人用恐怖音乐开始收集硬币,如果你没能及时收集到,希特勒会尖叫一声跳到你的身上。(我不是种族主义者,我是犹太人,我只是喜欢黑色幽默)是的,谢谢!!