0

I want to make an AI using PictureBox that can roam randomly without messing its movements like for example: PictureBox must execute the movement to go Right, if the Timer runs out it the next movement is going Down, like just how random it would roam.

I'd thought I might figured it out to Hard code it. However might took long, also once the Timer Stops, it won't Restart again. idk why.

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

Here's the code also

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

Well just assume that the PictureBox is in Location(0,0). If you see a PictureBox an image of a Tank nevermind that. That goes for the MainTank of the user will be using.

Mine Dyse
  • 1
  • 4
  • Code and question are very unclear. Do you really use various timers? For different or for the same controls? – TaW Mar 18 '20 at 15:33
  • Where do `Rightcount` et al get set/reset? – 001 Mar 18 '20 at 15:33
  • @TaW I use Timers well to make it use of whenever the movement of an AI could atleast be executed after the count. Now I just don't know how will it make it randomly. – Mine Dyse Mar 18 '20 at 17:27

1 Answers1

0

You could do it with just one timer:

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();
    }
}
001
  • 13,291
  • 5
  • 35
  • 66
  • Uhm from the void timer1Tick: Is that supposed to be EnemyTank.Left instead of EnemyTank.Right? – Mine Dyse Mar 19 '20 at 07:16
  • It works but... The way its movement is likely confusing, it just moves to the left and then stops. How will I be able to make the Timer not to stop? – Mine Dyse Mar 19 '20 at 07:27
  • As I revised the code, it seems that it works perfectly now however, yes I'd say randomly but the way the AI moves it has only limited time of movement. Like for example the PictureBox first move is going to the right. Atleast give him time to go to the right for a little while then change the direction. The only thing he does is Changing direction immediately but it does move. demonstrate it goes like this: RIGHT LEFT UP RIGHT DOWN LEFT... Randomly... It seems like it can execute the movements but a short limit of time.. How can I make him move as exactly as it is? – Mine Dyse Mar 19 '20 at 08:03
  • @MineDyse Sorry, yes there is a typo. That should be `EnemyTank.Top`. Will fix. There was also a typo in `if (moveToX == 0 && moveToY = 0)`. – 001 Mar 19 '20 at 11:45
  • Thanks, and uhm.. Would it possible to make the `EnemyTank` to move its certain direction in a seconds of time like moving in a length of 5 meters straight then go for another direction of a 5 meters straight again? – Mine Dyse Mar 19 '20 at 12:38
  • Can you clarify? Examples: move 5m up for 2s. Or: move 3m down for 4s? – 001 Mar 19 '20 at 12:42
  • Yes exactly like that. According to your previous code u gave me earlier is that all those movements were working simultaneously but it does move but the AI moves confusingly to the point it executes the movements like Up,Down,Right,Left randomly. Anyways all movements should have lengths of 5 meters or less. – Mine Dyse Mar 19 '20 at 15:29
  • I just tried your code and it's quite good now. Now I can't figure out how will I put sprite images to the Tank. Cuz it's only making it turn Right and Down. Here's the code actually `EnemyTank.Left += moveTo * speed; EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_RIGHT_v_2;` – Mine Dyse Mar 20 '20 at 05:31
  • What does `bool vertical` referring to? – Mine Dyse Mar 20 '20 at 06:39
  • That shouldn't be there. Just a remnant from a previous version. Removed. – 001 Mar 20 '20 at 11:52