Game Loop:
We need a game loop that is running as long as the game is running. The simplest way to do this is to use a timer. Put a timer on the form. Set the Name property to tmrLoop, and the interval to 150, and enabled to true. This is now our game loop that is called every 0.15 second. We are going to use this "loop" to move the head of the snake, move the body, and to check if the snake found the food. Add these lines of code to the tmrLoop timer event.
VB Code:
'Check to see if the snake finds food
Call FindFood
'Moves the snake
Call MoveSnake
These lines call two subs that we are going to make soon. The first one checks to see if the snake found food. And the other one is moving the whole snake. Let’s do the fun part first.
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 line 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
imgSnake(i).Left = Snake(i).X
Snake(i).Y = Snake(i - 1).Y
imgSnake(i).Top = Snake(i).Y
Snake(i).Facing = Snake(i - 1).Facing
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.
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
imgSnake(0).Left = Snake(0).X
ElseIf Snake(0).Facing = 2 Then 'Moves up
Snake(0).Y = Snake(0).Y - 10
imgSnake(0).Top = Snake(0).Y
ElseIf Snake(0).Facing = 3 Then 'Moves right
Snake(0).X = Snake(0).X + 10
imgSnake(0).Left = Snake(0).X
ElseIf Snake(0).Facing = 4 Then 'Moves down
Snake(0).Y = Snake(0).Y + 10
imgSnake(0).Top = Snake(0).Y
End If
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.
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.