Results 1 to 11 of 11

Thread: [RESOLVED] Trouble with simple game

  1. #1

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Resolved [RESOLVED] Trouble with simple game

    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.

    Pictures-
    http://i.imgur.com/fRdjb.jpg
    http://i.imgur.com/ShLEm.jpg

    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"

  2. #2
    New Member
    Join Date
    Apr 2012
    Posts
    2

    Re: Trouble with simple game

    This got kind of long, sorry!

    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:
    1. 'For example, the wall extends from 0 to 150 on the Y axis.
    2. If PictureBox1.Location.X = 270 And PictureBox1.Location.Y >= 0 And PictureBox1.Location.Y <= 150 Then
    3.   '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:
    1. 'Assuming the form is 600x600
    2. If e.KeyCode = Keys.Right And PictureBox1.Location.X < 570 Then
    3.   PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
    4. ElseIf e.KeyCode = Keys.Left And PictureBox1.Location.X > -30 Then
    5.   PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
    6. ElseIf e.KeyCode = Keys.Up And PictureBox1.Location.Y > -30 Then
    7.   PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
    8. ElseIf e.KeyCode = Keys.Down And PictureBox1.Location.Y < 570 Then
    9.   PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
    10. 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:
    1. Public Class Form1
    2. Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    3. 'Set a variable used to store what direction was moved
    4. Dim intPlayerDirection As Integer '0=Down,1=Left,2=Right,3=Up
    5.  
    6. 'Assuming the form is 600x600
    7. 'Move the player in the direction they pressed
    8. If e.KeyCode = Keys.Right And PictureBox1.Location.X < 570 Then
    9.   PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
    10.   intPlayerDirection = 2 'Player moved right
    11. ElseIf e.KeyCode = Keys.Left And PictureBox1.Location.X > -30 Then
    12.   PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
    13.   intPlayerDirection = 1 'Player moved left
    14. ElseIf e.KeyCode = Keys.Up And PictureBox1.Location.Y > -30 Then
    15.   PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
    16.   intPlayerDirection = 3 'Player moved up
    17. ElseIf e.KeyCode = Keys.Down And PictureBox1.Location.Y < 570 Then
    18.   PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
    19.   intPlayerDirection = 0 'Player moved down
    20. End If
    21. 'Now check for the wall
    22. If PictureBox1.Location.X = 270 And PictureBox1.Location.Y >= 0 And PictureBox1.Location.Y <= 150 Then
    23.   'Push the player back according to what direction they went
    24.   If intPlayerDirection = 0 Then PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 30)
    25.   If intPlayerDirection = 1 Then PictureBox1.Location = New Point(PictureBox1.Location.X + 30, PictureBox1.Location.Y)
    26.   If intPlayerDirection = 2 Then PictureBox1.Location = New Point(PictureBox1.Location.X - 30, PictureBox1.Location.Y)
    27.   If intPlayerDirection = 3 Then PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 30)
    28. End If
    29. End Sub
    30. 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:
    1. 'Assuming 2-dimensional array arrMapPass with max dimensions 19,14 (20x15 map)
    2. 'And a value of 0 is passable, 1 is not
    3. 'Also intPlayerX and intPlayerY for x/y values
    4. If e.KeyCode = Keys.Right And intPlayerX < 19 Then
    5.   If arrMapPass(intPlayerX+1,intPlayerY) = 0 Then
    6.     'Move player right since the tile is passable
    7.     intPlayerX += 1
    8.   End If
    9. End If
    Of course you can do that however works best for you.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Trouble with simple game

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  4. #4

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Trouble with simple game

    Could you please give me a example of how to do it because I still don't seem to understand
    "When ideas and actions fuse, they form creations"

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Trouble with simple game

    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 ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  6. #6

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Trouble with simple game

    Only if you have time because I really wouldn't want to trouble you when you are busy
    "When ideas and actions fuse, they form creations"

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Trouble with simple game

    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  8. #8

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Trouble with simple game

    I see thank you. I appreciate it very much
    "When ideas and actions fuse, they form creations"

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trouble with simple game

    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.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Trouble with simple game

    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
    Attached Files Attached Files
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  11. #11

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Trouble with simple game

    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"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width