【问题标题】:Moving Picturebox randomly as it roams在漫游时随机移动 Picturebox
【发布时间】:2020-06-29 17:32:33
【问题描述】:

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

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

Here's a Picture of my Game so you would have some ideas about it.

这也是代码

 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。

【问题讨论】:

  • 代码和问题很不清楚。你真的会使用各种计时器吗?是针对不同的控件还是针对相同的控件?
  • Rightcount 等人在哪里设置/重置?
  • @TaW 我很好地使用了计时器,以便在计数后至少可以执行 AI 的移动时使用它。现在我只是不知道它会如何随机生成。

标签: c# timer picturebox


【解决方案1】:

只需一个计时器即可完成:

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();
    }
}

【讨论】:

  • 来自 void timer1Tick 的嗯嗯:应该是 EnemyTank.Left 而不是 EnemyTank.Right?
  • 它可以工作,但是......它的移动方式可能令人困惑,它只是向左移动然后停止。我怎样才能让定时器不停止?
  • 当我修改代码时,它似乎现在可以完美运行了,是的,我会随机说,但 AI 的移动方式只有有限的移动时间。例如,PictureBox 的第一步是向右移动。至少给他时间向右走一会儿,然后改变方向。他唯一做的就是立即改变方向,但它确实移动了。演示它是这样的: RIGHT LEFT UP RIGHT DOWN LEFT... 随机... 看起来它可以执行动作但时间很短.. 我怎样才能让他的动作完全一样?
  • @MineDyse 对不起,是的,有一个错字。那应该是EnemyTank.Top。会修。 if (moveToX == 0 &amp;&amp; moveToY = 0)也有错别字。
  • 谢谢,嗯.. 是否有可能让EnemyTank 在几秒钟内移动它的某个方向,就像直线移动 5 米的长度然后去另一个方向 5又直了几米?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多