How can I prevent picture boxes from intersecting, while they are in constant movement?

81 views Asked by At

A bit of an introduction: I am creating a game for a Grade 11 Visual Basic Computer Science class. My game is simply a survival game, where your character can shoot a bullet, left or right, to defend itself from 8 enemy picture boxes.

The movement for the player is a GetASyncKeyState function, for all the arrow keys, and the enemy moves on a constant timer as shown here:

  Private Sub tmrenemy2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrenemy2.Tick
    picenemy2.Visible = True

    'these are the diagonal movements
    If picenemy2.Location.X < picguy.Location.X And picenemy2.Location.Y < picguy.Location.Y Then
        picenemy2.Location = New Point(picenemy2.Location.X + 2, picenemy2.Location.Y + 2)

    ElseIf picenemy2.Location.X < picguy.Location.X And picenemy2.Location.Y > picguy.Location.Y Then
        picenemy2.Location = New Point(picenemy2.Location.X + 2, picenemy2.Location.Y - 2)

    ElseIf picenemy2.Location.X > picguy.Location.X And picenemy2.Location.Y < picguy.Location.Y Then
        picenemy2.Location = New Point(picenemy2.Location.X - 2, picenemy2.Location.Y + 2)

    ElseIf picenemy2.Location.X > picguy.Location.X And picenemy2.Location.Y > picguy.Location.Y Then
        picenemy2.Location = New Point(picenemy2.Location.X - 2, picenemy2.Location.Y - 2)

    Else 'these are the straight movements
        If picenemy2.Location.X < picguy.Location.X Then
            picenemy2.Location = New Point(picenemy2.Location.X + 2, picenemy2.Location.Y)

        ElseIf picenemy2.Location.X > picguy.Location.X Then
            picenemy2.Location = New Point(picenemy2.Location.X - 2, picenemy2.Location.Y)

        ElseIf picenemy2.Location.Y < picguy.Location.Y Then
            picenemy2.Location = New Point(picenemy2.Location.X, picenemy2.Location.Y + 2)

        ElseIf picenemy2.Location.Y > picguy.Location.Y Then
            picenemy2.Location = New Point(picenemy2.Location.X, picenemy2.Location.Y - 2)
        End If
    End If

End Sub

picenemy is the enemy picturebox, and picguy is the player's character.

Basically what happens as of now of the game running: The enemies all chase the character, but in a few arrow movements, the enemies clump together.

And by clump together, I mean the picture boxes go ontop of eachother, and overlap, and seem like it is only one enemy in pursuit of the player.

I was wondering how I could edit the movement patterns for the enemy so they stay out of eachother's way, or if they get close to eachother they back off, and move around?

Or am I getting into to deep of AI technology for a program I am supposed to understand?

0

There are 0 answers