0

我想使用 PictureBox 制作一个 AI,它可以随机漫游而不会弄乱它的动作,例如:PictureBox 必须执行向右移动的动作,如果计时器用完它,下一个动作就会向下移动,就像它会随机漫游一样。

我想我可能会想办法对它进行硬编码。但是可能需要很长时间,一旦计时器停止,它就不会再次重新启动。我知道为什么。

这是我的游戏的图片,因此您会对它有一些想法。

这也是代码

 private void Game_Load(object sender, EventArgs e)
    {
        E_Right.Start();
        if(Upcount == 0)
        {
            E_Right.Start();
        }
    }

    private void E_Right_Tick(object sender, EventArgs e)
    {
        if(Rightcount > 0)
        {
            EnemyTank.Left += 5;
            EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_RIGHT_v_2;
            Rightcount = Rightcount - 1;
        }
        if (Rightcount == 0)
        {
            E_Right.Stop();
            E_Down.Start();
        }
    }

    private void E_Up_Tick(object sender, EventArgs e)
    {
        if (Upcount > 0)
        {
            EnemyTank.Top -= 5;
            EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_TOP_v_1;
            Upcount = Upcount - 1;
        }
        if (Upcount == 0)
        {
            E_Up.Stop();
            E_Right.Start();
        }
    }

    private void E_Down_Tick(object sender, EventArgs e)
    {
        if (Downcount > 0)
        {
            EnemyTank.Top += 5;
            EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_DOWN_v_1;
            Downcount = Downcount - 1;
        }
        if (Downcount == 0)
        {
            E_Down.Stop();
            E_Left.Start();
        }
    }

    private void E_Left_Tick(object sender, EventArgs e)
    {
        if (Leftcount > 0)
        {
            EnemyTank.Left -= 5;
            EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_LEFT_v_1;
            Leftcount = Leftcount - 1;
        }
        if (Leftcount == 0)
        {
            E_Left.Stop();
            E_Up.Start();
        }
    }

好吧,假设 PictureBox 在 Location(0,0) 中。如果你看到一个 PictureBox 一个坦克的图像,没关系。这适用于用户将使用的 MainTank。

4

1 回答 1

0

你可以只用一个计时器来做到这一点:

int moveTo = 0;             // Move while this is not 0
int speed = 3;
Random rand = new Random();

// Keep track of whether the tank is moving up/down or left/right
enum MoveOrientation { Vertical, Horizontal };
MoveOrientation orientation = MoveOrientation.Vertical;

void ChooseNextPosition()
{
    // Negative numbers move left or down
    // Positive move right or up
    do {
        moveTo = rand.Next(-5, 5);
    } while (moveTo == 0);  // Avoid 0
}

private void Game_Load(object sender, EventArgs e) {
    ChooseNextPosition();
    timer1.Start();
}

private void timer1Tick(object sender, EventArgs e) {
    if (orientation == MoveOrientation.Horizontal)
    {
        EnemyTank.Left += moveTo * speed;
    }
    else
    {
        EnemyTank.Top += moveTo * speed;
    }
    moveTo -= moveTo < 0 ? -1 : 1;
    if (moveTo == 0)
    {
        // Switch orientation.
        // If currently moving horizontal, next move is vertical
        // And vice versa
        orientation = orientation == MoveOrientation.Horizontal ? MoveOrientation.Vertical : MoveOrientation.Horizontal;
        // Get new target
        ChooseNextPosition();
    }
}
于 2020-03-18T21:49:02.310 回答