C# 在Picturebox漫游时随机移动它

C# 在Picturebox漫游时随机移动它,c#,timer,picturebox,C#,Timer,Picturebox,我想用PictureBox制作一个AI,它可以随意漫游而不会弄乱它的动作,例如: PictureBox必须执行移动才能向右移动,如果计时器用完,则下一个移动将向下移动,就像它将漫游的随机程度一样 我想我可能会把它硬编码出来。然而,这可能需要很长时间,而且一旦计时器停止,它将不会再次重新启动。我不知道为什么 这是代码 private void Game_Load(object sender, EventArgs e) { E_Right.Start();

我想用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位于位置(0,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();
    }
}
int moveTo=0;//在这不是0时移动
内速度=3;
Random rand=新的Random();
//跟踪油箱是向上/向下移动还是向左/向右移动
枚举移动方向{垂直,水平};
MoveOrientation=MoveOrientation.Vertical;
void chooseExtPosition()
{
//负数向左或向下移动
//正向向右或向上移动
做{
moveTo=rand.Next(-5,5);
}while(moveTo==0);//避免0
}
私有无效游戏加载(对象发送方,事件参数e){
选择extposition();
timer1.Start();
}
私有void timer1Tick(对象发送方,事件参数e){
如果(方向==移动方向.水平)
{
EnemyTank.Left+=移动到*速度;
}
其他的
{
EnemyTank.Top+=移动到*速度;
}
moveTo-=moveTo<0?-1:1;
if(moveTo==0)
{
//切换方向。
//如果当前水平移动,则下一步是垂直移动
//反之亦然
方向=方向==移动方向。水平?移动方向。垂直:移动方向。水平;
//获得新目标
选择extposition();
}
}

代码和问题非常不清楚。你真的使用各种计时器吗?对于不同或相同的控制?在哪里设置/重置
Rightcount
等?@TaW我很好地使用计时器,以便在计数后至少可以执行AI的移动时使用它。现在我只是不知道它是如何随机产生的。嗯,来自《虚空时间》的提示:那应该是EnemyTank。左边而不是EnemyTank。对吗?它有效,但是。。。它的移动方式可能令人困惑,它只是向左移动然后停止。我怎样才能让计时器不停下来呢?当我修改代码时,它现在似乎工作得很好,但是,是的,我会随机说,但是AI的移动方式,它的移动时间有限。例如,PictureBox的第一步是向右移动。至少给他一点时间向右走一会儿,然后改变方向。他唯一能做的就是立即改变方向,但方向确实在移动。演示它是这样的:右左上右下左。。。随机。。。它似乎可以执行这些动作,但时间有限。。“我怎样才能让他像现在这样移动呢?”@MineDyse对不起,是的,有个打字错误。应该是
EnemyTank.Top
。我会修好的。
if(moveToX==0&&moveToY=0)
中也有输入错误。谢谢,嗯。。是否有可能使
EnemyTank
在几秒钟内移动其特定方向,就像在5米长的直线上移动,然后再朝另一个5米长的直线方向移动?