这是计时器1中的代码,按钮在pictureBox1上随机移动,但有时它会移出边界。
我试图首先检查 pictureBox1 的顶部边界/边框,但它不起作用,有时会离开图片框区域。
这是计时器中的原始代码:
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 顶部的检查之后:
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);
}
}
}
但它不起作用。如何检查左上角和右下角?所以按钮不会移出界限?
我首先关心的是,你使用了太多的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;
}
}
}
旁注:我最终编写并运行了这个,对我有用。