- Move a control with a keypress.
- Use formula to get boundaries.
- Test if a control will collide with another.
First you will need a new form. Create one, and do the following:
- Change the instance name to frmMain
- Set the Size property to a multiple of the value you would like to increment with
I bet you are a little confused why we would want to set the form size as a multiple of the value we'd like to increment. This is because the way we are testing for form collisions. If we had a form size of 300,300, we would have a tiny gap in between the boundary and the player, and we don't want that now do we?
Create a new PictureBox in the middle of the form and call it picPlayer. Select an image for the control so we can actually see something happening. For the purpose of this example, I have selected a toad.
By now we should have something like this:

Now that we have our controls in order, lets start programming them. Add the following code into your frmMain class:
CODE
Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Dim Loc As Point
Select Case e.KeyCode
Case Keys.Left
If Not picPlayer.Location.X - 5 < 0 Then
Loc = New Point(picPlayer.Location.X - 5, picPlayer.Location.Y)
picPlayer.Location = Loc
End If
Case Keys.Right
If Not picPlayer.Location.X + 5 > Me.Width - picPlayer.Width - 5 Then
Loc = New Point(picPlayer.Location.X + 5, picPlayer.Location.Y)
picPlayer.Location = Loc
End If
Case Keys.Up
If Not picPlayer.Location.Y - 5 < 0 Then
Loc = New Point(picPlayer.Location.X, picPlayer.Location.Y - 5)
picPlayer.Location = Loc
End If
Case Keys.Down
If Not picPlayer.Location.Y - 5 > Me.Height - picPlayer.Height * 1.5 Then
Loc = New Point(picPlayer.Location.X, picPlayer.Location.Y + 5)
picPlayer.Location = Loc
End If
End Select
End Sub
Lets break the code up. We have created the variable Loc so we can change the location of picPlayer. Our select case statement will switch between the possible KeyCode scenarios, and put them into the right context, like an If statement, just with multiple conditions.
CODE
If Not picPlayer.Location.X - 5 < 0 Then
Lets turn this into an English question. If picPlayer's Location minus 5 is not less than form's minimum width value, run the following code:
CODE
Loc = New Point(picPlayer.Location.X - 5, picPlayer.Location.Y)
picPlayer.Location = Loc
We will first initialize the variable with the location we want to send picPlayer to. We want it relative to its current position so we use its current Location and decrement or increment it by a value. We then obviously change picPlayer's Location according to Loc.
Ok, so now that we know how to move a control with key presses, and check for bounds, lets get into Collision Detection with other objects.
Add a new PictureBox to your form and call it picMoney. Select an image for your money PictureBox. Now your form should look something like this:

Coding collision detection isn't as hard as you think. You may think that you need to check for picMoney's various location properties to create a rectangle formula, but VB.NET has a wonderful function called IntersectsWith(). Lets put this function to practice. Because we want to make it a little more interesting, we will make the Money's location change every time it has been picked up. To do this we need a random number function.
Credits to Intelligent Solutions Inc. - http://www.freevbcode.com/ShowCode.Asp?ID=4451
CODE
Public Function RandomNumber(ByVal MaxNumber As Integer, _
Optional ByVal MinNumber As Integer = 0) As Integer
'initialize random number generator
Dim r As New Random(System.DateTime.Now.Millisecond)
'if passed incorrect arguments, swap them
'can also throw exception or return 0
If MinNumber > MaxNumber Then
Dim t As Integer = MinNumber
MinNumber = MaxNumber
MaxNumber = t
End If
Return r.Next(MinNumber, MaxNumber)
End Function
Once you have placed the RandomNumber() function in your code, add the following code at the bottom of frmMain_KeyDown().
CODE
If picPlayer.Bounds.IntersectsWith(picMoney.Bounds) Then
picMoney.Hide()
picMoney.Location = New Point(RandomNumber(Me.Width), RandomNumber(Me.Height))
MsgBox("You have collected some money.")
picMoney.Show()
End If
Click to view attachment
And there we have it! You should now have a fully maneuverable player that collects money. Thanks for reading, and I hope you found some of this interesting.