c# - 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);
}
我如何移动玩家但防止他离开边界?
解决方案
您的代码有一些不正确的观点,至少我是这么认为的。
你总是将玩家移动到他的新位置。你确实检查他是否触及界限。如果他触及边界,您将他向上移动一个像素,向左移动一个像素,仅将他移动到所选方向的 7 个像素。因此,
if
在您检查他是否触及边界的地方,您必须中断并且不执行设置新位置的其余代码。一个简单return;
的就可以完成这项工作。如果按下某个键,则执行边界检查,如果玩家没有触及边界,您将移动玩家。这是错误的顺序。您必须检查玩家是否会在移动后触及边界,并且只有当他不会触及边界时才会移动他。否则,您会将他移入边界,并且永远不会再将他弄出来。
因此,这是您的代码,并进行了一些更正:
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 的键,则防止执行您的代码(即使他没有更改任何值)。
- use
switch
而不是if
this 使它更具可读性。 - 而不是每次都写入数字 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
}
这对你有帮助吗?
推荐阅读
- reactjs - 反应本机地图方向:无法读取未定义的属性“地图”
- ios - iOS SwiftUI:防止 resizable() 动画
- html - 使用 CSS 创建自定义复选框
- javascript - 除非已经在 Javascript 中编码,否则对 XML 实体进行编码
- python - Python operator.methodcaller() 没有正确处理参数?
- php - 在 PHP 浏览器中不断看到错误(与表单有关)
- java - IllegalStateException:无法打开嵌套条目“BOOT-INF/lib/xxx.jar”。使用 yguard
- linux - Linux fork,execve - 没有等待僵尸
- python - 未保存具有外键字段的 ModelForm
- html - 如何正确地水平放置引导复选框