IF blocks to LOOP

本文关键字:LOOP to blocks IF | 更新日期: 2023-09-27 18:28:39

我真的需要缩短代码,并将大量的IF运算符块转换为LOOP(for,foreach)。

当它在IF块中时,代码工作。当它在FOR循环中时,代码有时不起作用。

帮忙会很好的。

IF块:

            PictureBox tmp = new PictureBox();
        tmp.Bounds = pbxDators.Bounds;
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 3;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 2;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 1;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 0;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -1;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -2;
            return true;
        }
        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -3;
            return true;
        }
        return false;

我试图写的FOR循环来取代那些巨大的IF:

            for (int i = -3; i <= 3; i++)
        {
            PictureBox tmp = new PictureBox();
            tmp.Bounds = pbxDators.Bounds;
            int a = 10;
            if (i == 3)
            {
                tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
                if (dt.Bounds.IntersectsWith(tmp.Bounds))
                {
                    atskanotAudio(2);
                    bumbasStiprums = 2;
                    return true;
                }
            }
            else
            {
                tmp.SetBounds(tmp.Location.X, tmp.Location.Y + a, 1, 15);
                a = a + 10;
                if (dt.Bounds.IntersectsWith(tmp.Bounds))
                {
                    atskanotAudio(2);
                    bumbasStiprums = 2;
                    return true;
                }
            }
        }
        return false;

所有这些代码都在一个公共函数中,该函数将picturebox对象作为参数并返回布尔值。。

为什么我的FOR循环在大多数情况下都不起作用?如果球击中中间,有时还好,但大多数情况下球会飞过物体边界。没有生成错误。

IF blocks to LOOP

这似乎是您需要的基于第一个代码的代码:

PictureBox tmp = new PictureBox();
tmp.Bounds = pbxDators.Bounds;
tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
for (var i = 3; i >= -3; i--)
{
    if (dt.Bounds.IntersectsWith(tmp.Bounds))
    {
        atskanotAudio(1);
        bumbasStiprums = i;
        return true;
    }
    tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
}
return false;