用Ticker Winforms Space Invaders游戏bug

时间:2015-02-24 14:20:52

标签: c# winforms

我在写一个Space Invaders游戏,其中我有2个Tickers(以10和75ms的间隔调用)。为此,10ms Ticker至关重要。 股票代码间接调用您在下面看到的方法。 此方法负责移动入侵者/外星人。

他们首先向右移动直到他们到达右边界(ClientRectangle - 100),然后他们应该向下移动30个像素并向左切换当前的方向。如果它们到达左边界(ClientRectangle + 100),它们也应该向下移动30个像素并再次切换它们的当前路径。 左右移动没有问题,向下移动也有效,但有时它们向下移动2或3次(2或3 * 30像素)。

我无法弄清楚问题所在。我猜这个方法经常调用这个方法,以至于尚未计算出新的位置。

private void MoveInvaders()
{
    bool downLeft = false;
    bool downRight = false;

    if (movedDown)
    {
        //rect = ClientRectangle of the Form
        //I´m using LINQ for some collision detection
        //The surface is the representation of the Invader as a Rectangle

        downRight = (from inv in invaders
                     where (inv.Surface.X + inv.Surface.Width) < rect.right - 100
                     select inv).ToList().Count > 0;

        if (!downRight)
        {
            downLeft = (from inv in invaders
                        where inv.Surface.X > rect.left + 100
                        select inv).ToList().Count > 0;
        }

        if (downRight || downLeft)
        {
            movedDown = false;
            downLeft = false;
            downRight = false;
        }

    }
    else
    {

        downRight = (from inv in invaders
                     where (inv.Surface.X + inv.Surface.Width) >= rect.right - 100
                     select inv).ToList().Count > 0;

        if (!downRight)
        {
            downLeft = (from inv in invaders
                        where inv.Surface.X <= rect.left + 100
                        select inv).ToList().Count > 0;
        }
    }

    foreach (Invader invader in invaders)
    {
        if (!movedDown)
        {
            if (downRight)
            {
                invader.Move(Way.Down);  //Their old Point.Y += 30
                currentWay = Way.Left;
            }
            else if (downLeft)
            {
                invader.Move(Way.Down);
                currentWay = Way.Right;
            }
        }

        invader.Move(currentWay);  //old Point.X += 1
    }

    if (!movedDown && (downLeft || downRight))
    {
        movedDown = true;
    }
}

0 个答案:

没有答案