Question Game Development / Restrict Movement

RDaugherty

New member
Joined
Dec 14, 2016
Messages
2
Programming Experience
1-3
Let me start off by saying I'm new to VB!

I learned AutoIt3 as my first language and would say I am proficient in it but wanted to expand my horizons and landed on VB.

Started ~2 months ago I can't believe the differences from A to B.

Now onto the question!


I've started writing just a small Rogue-Like game in VB and had a concept question.

I current have:

EX1Knlb.png


And I want to restrict the player from being able to move pass the walls / into the shop ect.

I have figured out a way to do it but I'm not 100% sure if it is the most efficient way so I thought I'd ask!

Code

VB.NET:
    Dim NoGoXLocations As New ArrayList
    Dim NoGoYLocations As New ArrayList


    Dim NoMove As Boolean
    Dim KeyPressed As String

Private Sub HomeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' NoGoX Against Left Wall
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})
        NoGoXLocations.AddRange(New String() {"70"})


        ' NogoX Against ShopWall
        NoGoXLocations.AddRange(New String() {"120"})
        NoGoXLocations.AddRange(New String() {"120"})
        NoGoXLocations.AddRange(New String() {"120"})
        NoGoXLocations.AddRange(New String() {"120"})


        ' NoGoY Against Left Wall
        NoGoYLocations.AddRange(New String() {"360"})
        NoGoYLocations.AddRange(New String() {"340"})
        NoGoYLocations.AddRange(New String() {"320"})
        NoGoYLocations.AddRange(New String() {"300"})
        NoGoYLocations.AddRange(New String() {"280"})
        NoGoYLocations.AddRange(New String() {"260"})
        NoGoYLocations.AddRange(New String() {"240"})
        NoGoYLocations.AddRange(New String() {"220"})
        NoGoYLocations.AddRange(New String() {"200"})
        NoGoYLocations.AddRange(New String() {"180"})
        NoGoYLocations.AddRange(New String() {"160"})
        NoGoYLocations.AddRange(New String() {"140"})


        ' NoGoY Against ShopWall
        NoGoYLocations.AddRange(New String() {"120"})
        NoGoYLocations.AddRange(New String() {"100"})
        NoGoYLocations.AddRange(New String() {"80"})
        NoGoYLocations.AddRange(New String() {"60"})
End Sub

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown


        KeyPressed = ""
        NoMove = False


        Select Case e.KeyCode
            Case Keys.Left
                KeyPressed = "Left"
            Case Keys.Right
                KeyPressed = "Right"
            Case Keys.Up
                KeyPressed = "Up"
            Case Keys.Down
                KeyPressed = "Down"
            Case Keys.End ' This is just for testing to get the X/Y I don't want them to move to.
                Dim XLocation As String = CharPictureBox.Location.X - 20
                Dim YLocation As String = CharPictureBox.Location.Y - 20
                MessageBox.Show(XLocation + vbNewLine + YLocation)
        End Select


        If KeyPressed = "Left" Then
            For i As Integer = 0 To NoGoXLocations.Count - 1
                If NoGoXLocations.Item(i) > CharPictureBox.Location.X - 20 Then
                    If NoGoYLocations.Item(i) = CharPictureBox.Location.Y - 20 Then
                        NoMove = True
                    End If
                End If
            Next
        End If


        If Not NoMove = True Then
            If KeyPressed = "Left" Then
                CharPictureBox.Location = New Point(CharPictureBox.Location.X - 20, CharPictureBox.Location.Y)
            ElseIf KeyPressed = "Right" Then
                CharPictureBox.Location = New Point(CharPictureBox.Location.X + 20, CharPictureBox.Location.Y)
            ElseIf KeyPressed = "Up" Then
                CharPictureBox.Location = New Point(CharPictureBox.Location.X, CharPictureBox.Location.Y - 20)
            ElseIf KeyPressed = "Down" Then
                CharPictureBox.Location = New Point(CharPictureBox.Location.X, CharPictureBox.Location.Y + 20)
            End If


        End If


        NoMove = False
    End Sub
 
Back
Top