Vb2010 - Snake Game Issue
I currently making a "Snake" game in VB2010. I am using picture boxes to create the snake. The snake moves fine and it picks up the apples fine.
But when I turn the snake Left or Up it only displays the first 5 segments of the snake. When the snake travels Right or Down it displays the entire snake. I am completely stumped on what is happening.
Here is my code to Move the Snake.
'Moving the Body Of the Snake
For i As Integer = (Length - 1) To 1 Step (-1)
Snake(i).X = Snake(i - 1).X
Snake(i).Y = Snake(i - 1).Y
SnakeBody(i).Location = New Point(Snake(i).X, Snake(i).Y)
Snake(i).Facing = Snake(i - 1).Facing
Next i
'Moves the Head of the Snake
'Moves Left
If Snake(0).Facing = 1 Then
开发者_开发问答 Snake(0).X = Snake(0).X - 20
SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)
' Moves Up
ElseIf Snake(0).Facing = 2 Then
Snake(0).Y = Snake(0).Y - 20
SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)
' Moves Right
ElseIf Snake(0).Facing = 3 Then
Snake(0).X = Snake(0).X + 20
SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)
' Moves Down
ElseIf Snake(0).Facing = 4 Then
Snake(0).Y = Snake(0).Y + 20
SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)
End If
Length += 1
Also here is the code that Add's one segment to the snake when it comes in contact with the apple.
'Checks to see if the head of the snake finds the food
If Snake(0).X = Food.Left And Snake(0).Y = Food.Top Then
LoadSnakeBody()
'Placing the new body on its place
If Snake(Length - 1).Facing = 1 Then 'Looking left
Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width
Snake(Length).Y = Snake(Length - 1).Y
SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
SnakeBody(Length).Visible = True
ElseIf Snake(Length - 1).Facing = 2 Then 'Looking up
Snake(Length).X = Snake(Length - 1).X
Snake(Length).Y = Snake(Length - 1).Y + SnakeBody(0).Height
SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
SnakeBody(Length).Visible = True
ElseIf Snake(Length - 1).Facing = 3 Then 'Looking right
Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width
Snake(Length).Y = Snake(Length - 1).Y
SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
SnakeBody(Length).Visible = True
ElseIf Snake(Length - 1).Facing = 4 Then 'Looking down
Snake(Length).X = Snake(Length - 1).X
Snake(Length).Y = Snake(Length - 1).Y - SnakeBody(0).Height
SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
SnakeBody(Length).Visible = True
End If
PlaceFood()
End If
If anyone could tell me what is wrong I would appreciate it sooo much!
Thanks
Hazarding a guess (more code might help; also, IANA VB expert) - is this some kind of boundary problem where Snake(0).X
and Snake(0).Y
can't go below 0 or something?
Other suggestion: check the signs of your left-facing body-segment addition code. I think this might be a problem:
Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width
If the segment is facing left, and we're trying to add a piece to the end, we need to move to the right, i.e. PLUS SnakeBody(0).Width
.
Snake(Length).X = Snake(Length - 1).X + SnakeBody(0).Width
Hopefully this helps. I did a snake game when I was in school, so I feel your pain with getting all the directions and body segment additions working out properly.
精彩评论