locked
How can I keep the player out of the walls? RRS feed

  • Question

  • Ok, so I have this (working) code to make my character move around the map (thanks for that help, by the way)

        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            ' These variables are for moving the player. They take the current y/x position and shift one space up/down/left/right
            Dim playerUp = Player.Location.Y - 20
            Dim playerDown = Player.Location.Y + 20
            Dim playerRight = Player.Location.X + 20
            Dim playerLeft = Player.Location.X - 20
    
            Select Case e.KeyCode
                Case Keys.W
                    Player.Location = New Point(Player.Location.X, playerUp)
                Case Keys.S
                    Player.Location = New Point(Player.Location.X, playerDown)
                Case Keys.D
                    Player.Location = New Point(playerRight, Player.Location.Y)
                Case Keys.A
                    Player.Location = New Point(playerLeft, Player.Location.Y)
    
                    ' Now for the silly arrow keys
                Case Keys.Up
                    Player.Location = New Point(Player.Location.X, playerUp)
                Case Keys.Down
                    Player.Location = New Point(Player.Location.X, playerDown)
                Case Keys.Right
                    Player.Location = New Point(playerRight, Player.Location.Y)
                Case Keys.Left
                    Player.Location = New Point(playerLeft, Player.Location.Y)
            End Select
        End Sub

    But, the player can walk through the walls -which are colored labels- (As expected), how can I make the walls impassable.  In case you're wondering, I am very new to VB lol

    Thanks for reading :)


    /********** Zbee Multimedia Designer and Web Developer http://zbee.me **********/

    Friday, October 11, 2013 2:55 PM

Answers

  • Compute the new location without moving the player. If the new location is outside the bounds, set it back to the maximum value. Afterwards set the new location of the player. For each direction the limit is different so you have to think about what is the limit for the player in each direction.

    You just have to translate these words to VB.


    Armin

    • Marked as answer by Zbee- Friday, October 11, 2013 5:23 PM
    Friday, October 11, 2013 3:12 PM

All replies

  • Compute the new location without moving the player. If the new location is outside the bounds, set it back to the maximum value. Afterwards set the new location of the player. For each direction the limit is different so you have to think about what is the limit for the player in each direction.

    You just have to translate these words to VB.


    Armin

    • Marked as answer by Zbee- Friday, October 11, 2013 5:23 PM
    Friday, October 11, 2013 3:12 PM
  • You'll need to test to see if the bounds of the player after they move would intersect with the bounds of any label, and if so, do not move the player.

    Have you done any research on making a game in VB.Net?  This path you are headed down of using controls and events will be riddled with issues (this one is just the first), some of which have no reasonable workaround (e.g. rotating game objects).  You might want to read this article and then come up with a new design for your game.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, October 11, 2013 3:13 PM
  • You always answer my questions, Armin, thank you :)

    This is how I implemented it:

    Case Keys.W If playerUp >= -1 Then Player.Location = New Point(Player.Location.X, playerUp) End If

    Case Keys.S
       If playerDown <= 581 Then
           Player.Location = New Point(Player.Location.X, playerDown)
       End If



    /********** Zbee Multimedia Designer and Web Developer http://zbee.me **********/

    Friday, October 11, 2013 5:24 PM
  • Ah, yes, I'm quite aware, I've made games in other languages.

    However, my boss wants me to make a quick little game in VB, so I'm doing just that. And, seeing as how I'm new to VB, I'm following what he's saying to do, which is these event-driven things. I'll probably re-make the game in a few months -to make it acceptable to my friends- anyway.

    But, thank you for your response, nonetheless :)


    /********** Zbee Multimedia Designer and Web Developer http://zbee.me **********/

    Friday, October 11, 2013 5:27 PM
  • I know I've modified the code you've posted above myself within your other thread. There I only intended to make it compilable. In addition to the other suggestions you got there (iirc), I now also suggest to modify your approach a little:

          Dim NewLocation = player.Location
    
          Select Case e.KeyCode
             Case Keys.W, Keys.Up
                NewLocation.Y -= 20
                If NewLocation.Y < 0 Then NewLocation.Y = 0
                player.Location = NewLocation
             Case Keys.S, Keys.Down
                NewLocation.Y += 20
                If NewLocation.Y > 500 Then NewLocation.Y = 500
                player.Location = NewLocation
             Case Keys.D, Keys.Right
                NewLocation.X += 20
                If NewLocation.X > 500 Then NewLocation.X = 500
                player.Location = NewLocation
             Case Keys.A, Keys.Left
                NewLocation.X -= 20
                If NewLocation.X < 0 Then NewLocation.X = 0
                player.Location = NewLocation
          End Select
    

    You still have to adjust the limits 500 as you need.

    Lines like

    NewLocation.X += 20
    If NewLocation.X > 500 Then NewLocation.X = 500

    could also be replaced by the single line

    NewLocation.X = Math.Min(500, NewLocation.X + 20


    Armin

    Friday, October 11, 2013 5:38 PM
  • Daaaaaaaaaang! You're gonna end up writing like 90% of all of the code-blocks I use in this game, man! :)

    Anyways yeah, that works really well, thanks a LOT :D

    Here's how I am using it:

    Case Keys.W, Keys.Up
         NewLocation.Y -= 20 ' The one-line replacement you showed me didn't make 100% sense, so I'll stick with this
         If NewLocation.Y < 0 Then NewLocation.Y = 0
         Player.Location = NewLocation
    Case Keys.S, Keys.Down
         NewLocation.Y += 20
         If NewLocation.Y > 580 Then NewLocation.Y = 580
         Player.Location = NewLocation

    I'm not totally sure why I feel the need to reply to everything, but whatever.


    /********** Zbee Multimedia Designer and Web Developer http://zbee.me **********/

    Friday, October 11, 2013 6:09 PM
  • You're gonna end up writing like 90% of all of the code-blocks I use in this game, man! :)


    Isn't it the same block every time? ;-)))

    Armin

    Friday, October 11, 2013 6:42 PM