[英]C# - Preventing a picturebox from leaving a boundary
我有一個名為 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);
}
我如何移動玩家但阻止他離開邊界?
有一種觀點認為您的代碼不正確,至少我是這么認為的。
你總是將玩家移動到他的新位置。 你確實檢查他是否觸及邊界。 如果他觸及邊界,您將他向上移動 1 個像素,向左移動 1 個像素,然后將他移動到所選方向的 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
}
但也許你也想
switch
而不是if
這使它更具可讀性。所以我的建議是這樣的:
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
}
這對你有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.