Move Snake:
We are going to move the body first and then the head. So the first loop is moving the body. It moves the back of the snake first, and then moves the part that is in front of that one and so on. The part of the snake that is in the rear gets the coordinates of the part that is in front of it. And it also gets the facing variable of the part in front. This movement is made in this loop. Make a Sub called MoveSnake, and then add this code:
VB Code:
Dim i As Long
'Move body
For i = (Length - 1) To 1 Step (-1)
Snake(i).X = Snake(i - 1).X
Snake(i).Y = Snake(i - 1).Y
Snake(i).Facing = Snake(i - 1).Facing
BitBlt Form1.hDC, Snake(i).X, Snake(i).Y, imgSnake.Width, imgSnake.Height, imgSnake.hDC, 0, 0, vbSrcCopy
Next i
It is first giving the x coordinate of the UDT in front of it selves, then assigning it to the image holding that snake part. Then it does the same with the Y coordinate before it gives the facing variable to it. Then we once again use the BitBlt function to copy all the body parts.
Move the head:
But we need to treat the head a bit different, because the head has no body part in front of it to give him his coordinates. So we need to find another way of moving him. We are going to use the heads facing variable to just move it in that direction the head is facing. This is very straight forward, so just add this code under the code for the body in the same Sub.
VB Code:
'Move head
If Snake(0).Facing = 1 Then 'Moves left
Snake(0).X = Snake(0).X - 10
ElseIf Snake(0).Facing = 2 Then 'Moves up
Snake(0).Y = Snake(0).Y - 10
ElseIf Snake(0).Facing = 3 Then 'Moves right
Snake(0).X = Snake(0).X + 10
ElseIf Snake(0).Facing = 4 Then 'Moves down
Snake(0).Y = Snake(0).Y + 10
End If
'Draw the head
BitBlt Form1.hDC, Snake(0).X, Snake(0).Y, imgSnake.Width, imgSnake.Height, imgSnake.hDC, 0, 0, vbSrcCopy
If the facing variable of the head is 1 (left) then it moves 10 pixels to the left. If it 2 (up) then move 10 pixels up and so on. Nothing magical to it at all. We are also here using the BitBlt function to draw the head.
The last thing we are going to do here is to draw the food again. Yes we have done that earlier, but we cleared the screen remember? So once again we are using the coordinates of the food to draw it again like this.
VB Code:
'Draw the food again on the same place, because we cleared the screen
BitBlt Form1.hDC, Food.X, Food.Y, imgFood.Width, imgFood.Height, imgFood.hDC, 0, 0, vbSrcCopy
Moving the head in another direction:
But we want to be able to move the head in another direction. To do this we are going to use the arrow keys. To use the keyboard in VB we need to set the KeyPreview property of the form to true. Then we need to write some code to change the facing variable of the head. Do that in the form keydown event.
VB Code:
'Turning the face of the snake
If KeyCode = 37 Then 'Left
Snake(0).Facing = 1
ElseIf KeyCode = 38 Then 'Up
Snake(0).Facing = 2
ElseIf KeyCode = 39 Then 'Right
Snake(0).Facing = 3
ElseIf KeyCode = 40 Then 'Down
Snake(0).Facing = 4
End If
This code changes the heads facing variable according to the arrow that is pressed. If the left arrow (keycode 37) is pressed, the facing variable is changed to 1. And so on. This is also very straight forward. And now when you have added these lines of code you can for the first time try the game, and move the snake in the direction you want it to. But there is one thing left before the game is playable at all. That is that the snake can at the moment not eat food and grow.