这是timer1中的代码,按钮在pictureBox1上移动,但有时会移出边界。
我试图首先检查pictureBox1的上边界/边框,但是它不能正常工作它有时会离开pictureBox区域。
这是计时器中的原始代码:
private void timer1_Tick(object sender, EventArgs e)
{
if (currentPosition.X != randomPoint.X)
{
if (currentPosition.X > randomPoint.X)
currentPosition.X -= 1;
else
currentPosition.X += 1;
button1.Location = currentPosition;
}
else if (currentPosition.Y != randomPoint.Y)
{
if (currentPosition.Y > randomPoint.Y)
currentPosition.Y -= 1;
else
currentPosition.Y += 1;
button1.Location = currentPosition;
}
else
{
randomPoint.X = r.Next(0, pictureBox1.Width - button1.Width - 1);
randomPoint.Y = r.Next(0, pictureBox1.Height - button1.Height - 1);
}
}
这是在我尝试添加对pictureBox1 top的检查之后:
private void timer1_Tick(object sender, EventArgs e)
{
int maxScreenHeight = pictureBox1.Height - button1.Size.Height;
int maxScreenWidth = pictureBox1.Width - button1.Size.Width;
if (button1.Top < 0)
button1.Top = 0;
else if (button1.Top > maxScreenHeight)
button1.Top = maxScreenHeight;
else
{
if (currentPosition.X != randomPoint.X)
{
if (currentPosition.X > randomPoint.X)
currentPosition.X -= 1;
else
currentPosition.X += 1;
button1.Location = currentPosition;
}
else if (currentPosition.Y != randomPoint.Y)
{
if (currentPosition.Y > randomPoint.Y)
currentPosition.Y -= 1;
else
currentPosition.Y += 1;
button1.Location = currentPosition;
}
else
{
randomPoint.X = r.Next(0, pictureBox1.Width - button1.Width - 1);
randomPoint.Y = r.Next(0, pictureBox1.Height - button1.Height - 1);
}
}
}
但它不起作用。如何进行左上角和右下角的检查?那么按钮是否会移出界限?
答案 0 :(得分:2)
我首先关注的是,你使用了太多if-elseif-else分支,而不需要。 IMO在逻辑上有一层,许多代码部分可能大部分时间都没有输入。
此外,可能过于迂腐,但是你总是在计算随机数调用的上限时浪费资源。再次只是我的观点,更好的是只预测一次这些值并保持它们的通知,而不是一直要求他们的计算,无论处理器有多少,都要使它更容易。
编辑:是的,正如valter所提到的,你要么将pictureBox作为按钮的父级,要么只设置随机数的下限:那将是LOWER_LIMITs
然后,我认为这应该是您想要提出的代码:
namespace stackoverflow.com/questions/22135510
{
public partial class Form1 : Form
{
public int X_UPPER_LIMIT = 0;
public int Y_UPPER_LIMIT = 0;
public int X_LOWER_LIMIT = 0;
public int Y_LOWER_LIMIT = 0;
public Point randomPoint = new Point();
public Point newPosition = new Point();
public Random r = new Random();
public Form1()
{
InitializeComponent();
//care here, remove the pitureBox1.Location effects, if pictureBox is parent to the button
X_UPPER_LIMIT = pictureBox1.Location.X + pictureBox1.Width - button1.Width - 1
Y_UPPER_LIMIT = pictureBox1.Location.Y + pictureBox1.Height - button1.Height - 1;
X_LOWER_LIMIT = pictureBox1.Location.X + 1; //set this to 0, if pictureBox is parent of button
Y_LOWER_LIMIT = pictureBox1.Location.Y + 1; //set this to 0, if pictureBox is parent of button
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
//first, lets call the randoms at the start of tick
randomPoint.X = r.Next(X_LOWER_LIMIT, X_UPPER_LIMIT);
randomPoint.Y = r.Next(Y_LOWER_LIMIT, Y_UPPER_LIMIT);
newPosition.X = button1.Location.X;
newPosition.Y = button1.Location.Y;
//now, lets update positions, if they are not equal
if (button1.Location.X != randomPoint.X)
{
if (button1.Location.X > randomPoint.X)
newPosition.X--;
else
newPosition.X++;
}
if (button1.Location.Y != randomPoint.Y)
{
if (button1.Location.Y > randomPoint.Y)
newPosition.Y--;
else
newPosition.Y++;
}
button1.Location = newPosition;
}
}
}
旁注:我最终写了并运行它,对我有用。