School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,049 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,543 people online right now. Registration is fast and FREE... Join Now!



Basic Movement and Collision Detection

Page 1 of 1

Basic Movement and Collision Detection A useful tutorial for Beginning Game Design Rate Topic: ***** 1 Votes

#1 RodgerB  Icon User is offline

  • D.I.C Lover
  • Icon
  • View blog
  • Group: Expert w/DIC++
  • Posts: 2,244
  • Joined: 21-September 07


Dream Kudos: 2200

Expert In: Dot Net Technologies

Posted 13 October 2007 - 06:50 PM

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:
  • 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:
Posted Image

Now that we have our controls in order, lets start programming them. Add the following code into your frmMain class:
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.

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:

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:
Posted 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.freevbcod...ode.Asp?ID=4451
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().

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: 1112
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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month