Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,108 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,721 people online right now. Registration is fast and FREE... Join Now!




Basic Movement and Collision Detection

 
Reply to this topicStart new topic

> Basic Movement and Collision Detection, A useful tutorial for Beginning Game Design

RodgerB
Group Icon



post 13 Oct, 2007 - 06:50 PM
Post #1


Welcome to this tutorial on basic movement and collision detection. In this tutorial, we will learn how to:
  • 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:
  1. Change the instance name to frmMain
  2. 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:
IPB Image

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:
IPB Image

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


Attached File  movement.zip ( 331.17k ) Number of downloads: 427

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. smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/1/08 09:25PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month