I am planning to make a simple 2D RPG game which uses tile based collision detection. But first I am making a make-shift program to see if it works.
The code
Public Class Form1
Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Right Then
PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
ElseIf e.KeyCode = Keys.Left Then
PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
ElseIf e.KeyCode = Keys.Up Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
ElseIf e.KeyCode = Keys.Down Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
End If
If PictureBox1.Location.X = 270 Then
PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
End If
End Sub
End Class
I have a wall located at 270,0 in the form. Thus I made it so that if picturebox1.location.x = 270, then it must move back. Making it look like he stopped moving. But when I only make wall fill up half the panel, I still cant go through from the other half. I know this is because I said that the picturebox1.location.X cant go past 270, but I do not know how to solve this.
Thanks in advance
EDIT: Also when I keep going in any direction, it doesn't stop but runs off the panel and keeps on going forever. How to stop this from happening?
Last edited by Brinith; Apr 7th, 2012 at 04:34 PM.
"When ideas and actions fuse, they form creations"
In the context of the game's map only being what fits on the form, eg its width and height are the same as the window, rather than being larger than the window and having to scroll, the answer to your two problems:
1) Since the map is fixed, having the player stopped at x=270 will stop them from going right even when the space is empty. To fix that, just check if the player's y is also within the upper and lower heights of the wall. That way it will only stop you if you are on the wall itself, not underneath. You would have to use If...End If statements for every wall boundary that exists.
VB Code:
'For example, the wall extends from 0 to 150 on the Y axis.
If PictureBox1.Location.X = 270 And PictureBox1.Location.Y >= 0 And PictureBox1.Location.Y <= 150 Then
'Move the player back one
2) The answer to that would be to stop the player from moving out of the boundary. This can be done right when they press the button with an And statement, although it could also just be placed within the If...End If statements as well.
VB Code:
'Assuming the form is 600x600
If e.KeyCode = Keys.Right And PictureBox1.Location.X < 570 Then
PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
ElseIf e.KeyCode = Keys.Left And PictureBox1.Location.X > -30 Then
PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
ElseIf e.KeyCode = Keys.Up And PictureBox1.Location.Y > -30 Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
ElseIf e.KeyCode = Keys.Down And PictureBox1.Location.Y < 570 Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
End If
I hope those help somewhat, and if I made a mistake interpreting your post in any way, let me know!
EDIT:
Now I also noticed that since your If...End If statement checking if the player's X reaches 270 is outside of the statements for each movement direction, if you go around to the other side of the wall and try to move onto it, it will move you back left instead of pushing you back right. You'll need to either put the code for wall checking within each movement block, or just store which key was pressed and push you back appropriately. Here is what I came up with, again with the wall of height 150 and the form of 600x600.
VB Code:
Public Class Form1
Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
'Set a variable used to store what direction was moved
Dim intPlayerDirection As Integer '0=Down,1=Left,2=Right,3=Up
'Assuming the form is 600x600
'Move the player in the direction they pressed
If e.KeyCode = Keys.Right And PictureBox1.Location.X < 570 Then
PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
intPlayerDirection = 2 'Player moved right
ElseIf e.KeyCode = Keys.Left And PictureBox1.Location.X > -30 Then
PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
intPlayerDirection = 1 'Player moved left
ElseIf e.KeyCode = Keys.Up And PictureBox1.Location.Y > -30 Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
intPlayerDirection = 3 'Player moved up
ElseIf e.KeyCode = Keys.Down And PictureBox1.Location.Y < 570 Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
intPlayerDirection = 0 'Player moved down
End If
'Now check for the wall
If PictureBox1.Location.X = 270 And PictureBox1.Location.Y >= 0 And PictureBox1.Location.Y <= 150 Then
'Push the player back according to what direction they went
If intPlayerDirection = 0 Then PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
If intPlayerDirection = 1 Then PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
If intPlayerDirection = 2 Then PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
If intPlayerDirection = 3 Then PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
End If
End Sub
End Class
And lastly, if you're going to make a tile-based game, I would advise that you build a sort of passability tilemap to work with. What I mean is, something like a 2-dimensional array containing passability information for each tile. That way, you can record the player's X and Y as 1,1 or 3,5 instead of something like 60, 270 as you do now. For example, when the player wants to move right, you just check the passability of the tile next to them, like this
VB Code:
'Assuming 2-dimensional array arrMapPass with max dimensions 19,14 (20x15 map)
'And a value of 0 is passable, 1 is not
'Also intPlayerX and intPlayerY for x/y values
If e.KeyCode = Keys.Right And intPlayerX < 19 Then
If arrMapPass(intPlayerX+1,intPlayerY) = 0 Then
'Move player right since the tile is passable
intPlayerX += 1
End If
End If
Of course you can do that however works best for you.
Use greater than or less than when comparing points for collision detection. Don't use check for equality because you may be moving your object in units that are not a factor of 270.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Apologies, It seems I misunderstood the problem you were having. Disregard my first answer. Would it help you if I worked up a small project that demonstrates animation and effective collision detection ?
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Its fine. I've been working on mundane database related stuff for months now. I need a little break to do something fun. I love writing things like this
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Just to let you know, there is a post by Tensuke above (post #2) that was not visible before due to needing manual approval... it looks like there is some useful info there.
Sorry this took so long. Had to step out....Here is something I put together for you to look at. I made a control called Wall of which you can place as many as you like on a form and when the app runs a bouncing ball will interact with it by bouncing off them. The collision detection may be a bit more complicated than what you need and even then its not perfect in the sense that the ball bounces back contrary to what physics would dictate at some points but still, I Hope you can learn something from it. GL
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
I see I'll download the zip when i get back home.
Also thanks Tensuke, that made me understand it much better.
EDIT: I got the collision detection for the wall and the sides of the panel except the bottom one. The code for each side was exactly the same except the direction it pushed the player back to. NOTE: THE PANEL IS 570 by 540
EDIT: Got it the bottom part to work after resizing the panel to 600 by 600. Thanks for all your help
Last edited by Brinith; Apr 9th, 2012 at 02:50 PM.
"When ideas and actions fuse, they form creations"