一个boolean的判断语句能否用某种if之类的语句替换?

来源:百度知道 编辑:UC知道 时间:2024/06/03 18:57:00
bool y1Boolean, x1Boolean;
PointF[] points = new PointF[] { new PointF(9, 40), new PointF(100, 50) };//这是一个含点坐标的array,用于后面画图

private void timer1_Tick(object sender, EventArgs e)
{
Bitmap myImage = new Bitmap(300, 300);
// 下面这个for循环是给想让array中对应坐标的点移动起来,但是我想要的效果是,当这个点移动到pictureBox边缘的时候就会反弹,就像一个球在一个框框里面不停弹来弹去

for (int i = 0; i < points.Length; i++)
{
if (points[i].Y <= 0)
{
y1Boolean = true;
}
if (points[i].Y >= 300)
{
y1Boolean = false;
}
if (points[i].X <= 0)
{
x1Boolean = true;
}
if (points[i].X >= 300)
{
x1Boolean = false;
}
if (x1Boolean)

for 循环 不要了。

改成这样。

添加一个方法:把for 里的代码拷到 这个方法里,再修改,

checkPoint(PointF point,Bitmap myImage ){
y1 Boolean=false;
x1 Boolean =false;;

if (point.Y <= 0)
{
y1Boolean = true;
}
if (point.Y >= 300)
{
y1Boolean = false;
}
if (point.X <= 0)
{
x1Boolean = true;
}
if (point.X >= 300)
{
x1Boolean = false;
}
if (x1Boolean)
{
point.X += 2.2f;
}
if (!x1Boolean)
{
point.X -= 2.2f;
}
if (y1Boolean)
{
point.Y -= (119 - 55) / (142 - 190) * 2.2f;
}
if (!y1Boolean)
{
point.Y += (119 - 55) / (142 - 190) * 2.2f;

Graphics g = Graphics.FromImage(myImage);
g.FillRectangle(Brushes.Blue, points[i].X, points[i].Y, 5, 5);
pictureBox1.Image = myImage;

}

timer1_Tick 就可以写成这样啦:

private v