Results 1 to 5 of 5

Thread: [RESOLVED] Key Press Problem

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Resolved [RESOLVED] Key Press Problem

    I am using the following code to change the direction of my snake in a game:

    Code:
    Public Sub PressKey(ByVal Key As Input.Keys)
    
            Select Case Key
                Case Keys.Escape 'Exit Game'
                    MyBase.Exit()
                Case Keys.Up 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        If Snake(0).Direction <> BodyParts.Directions.South Then
                            Snake(0).Direction = BodyParts.Directions.North
                        End If
                    End If
                Case Keys.Right 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        If Snake(0).Direction <> BodyParts.Directions.West Then
                            Snake(0).Direction = BodyParts.Directions.East
                        End If
                    End If
                Case Keys.Down 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        If Snake(0).Direction <> BodyParts.Directions.North Then
                            Snake(0).Direction = BodyParts.Directions.South
                        End If
                    End If
                Case Keys.Left 'Change Direction To Up
                    If GameState = GameStates.Playing Then
                        If Snake(0).Direction <> BodyParts.Directions.East Then
                            Snake(0).Direction = BodyParts.Directions.West
                        End If
                    End If
            End Select
    End Sub
    I have set it up so that if the direction is set as east and the left key is pressed not to change the direction to west so the snake doesn't eat itself.

    This works however if the direction is east and I quickly press down then left the snake doesn't have time to move down and just moves to the left causing the snake to eat itself.

    Anyone know how i can solve this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Key Press Problem

    Add an extra variable to store the direction the snake last moved. You can then compare to that instead of the current direction to determine whether a new direction compatible or not.

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Key Press Problem

    Ok that didn't seem to work. Here's the code:

    Code:
    Public Sub PressKey(ByVal Key As Input.Keys)
    
            Select Case Key
                Case Keys.Escape 'Exit Game'
                    MyBase.Exit()
                Case Keys.Up 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        If TempDir <> BodyParts.Directions.None And TempDir <> BodyParts.Directions.South Then
                            TempDir = Snake(0).Direction
                            Snake(0).Direction = BodyParts.Directions.North
                        End If
                    End If
                Case Keys.Right 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        TempDir = Snake(0).Direction
                        If TempDir <> BodyParts.Directions.None And TempDir <> BodyParts.Directions.West Then
                            Snake(0).Direction = BodyParts.Directions.East
                        End If
                    End If
                Case Keys.Down 'Change Direction To Up'
                    If GameState = GameStates.Playing Then
                        TempDir = Snake(0).Direction
                        If TempDir <> BodyParts.Directions.None And TempDir <> BodyParts.Directions.North Then
                            Snake(0).Direction = BodyParts.Directions.South
                        End If
                    End If
                Case Keys.Left 'Change Direction To Up
                    If GameState = GameStates.Playing Then
                        TempDir = Snake(0).Direction
                        If TempDir <> BodyParts.Directions.None And TempDir <> BodyParts.Directions.East Then
                            Snake(0).Direction = BodyParts.Directions.West
                        End If
                    End If
            End Select
    
        End Sub
    Is this what you meant?

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Key Press Problem

    If firstsegment.X - nextsegment.X = 1 Then don't move
    Same thing with other directions.

  5. #5

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Key Press Problem

    Ok that didn't completely work but did help me solve the problem it is now working. Thanks.

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