-
Aug 22nd, 2009, 08:29 AM
#1
Thread Starter
Addicted Member
[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?
-
Aug 22nd, 2009, 08:37 AM
#2
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.
-
Aug 22nd, 2009, 04:17 PM
#3
Thread Starter
Addicted Member
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?
-
Aug 22nd, 2009, 05:17 PM
#4
Re: Key Press Problem
If firstsegment.X - nextsegment.X = 1 Then don't move
Same thing with other directions.
-
Aug 23rd, 2009, 04:42 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|