Snake
VB.Net Label Array
VB.Net Label Array
Here is the quick cute Snake game to code and play. The snake is an array of Labels that grows as it eats food.
Since the Labels are created at Run time I use Snake(i) to loop through the Labels.
The arrow keys change the direction of the slithering snake and the N key starts a new game.
The project starts with a plain form frmSnake at the default size of 300 x 300.
Double click the form. At the top are 3 global variables.
Public Class frmSnake
Dim Length As Integer = 4
Dim Snake() As Label
Dim blnNewGame As Boolean
Private Sub frmSnake_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Snake"
'Create the food
lblFood = New Label
lblFood.Size = New Size(10, 10)
lblFood.BackColor = Color.Red
Me.Controls.Add(lblFood)
'Create the snake
For i As Integer = 0 To Length
ReDim Preserve Snake(i)
Snake(i) = New Label
Snake(i).Size = New Size(10, 10)
Snake(i).BackColor = Color.Green
Snake(i).Left = 120 - 10 * i
Snake(i).Top = 120
Me.Controls.Add(Snake(i))
Next i
Randomize()
Snake(0).BackColor = Color.Chocolate 'The head
Snake(0).Tag = "Right" 'Initial move direction
PlaceFood()
If blnNewGame Then Exit Sub
Dim T As New Timer
T.Interval = 150
T.Enabled = True
AddHandler T.Tick, AddressOf Timer1_Tick
End Sub
This calls 2 procedures.
Sub PlaceFood()
Dim x As Integer
x = Me.Width / 10 - 6
x = (x * Rnd(x) + 2)
lblFood.Left = x * 10
x = Me.Height / 10 - 8
x = (x * Rnd(x) + 1)
lblFood.Top = x * 10
End Sub
and:
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
FindFood()
MoveSnake()
End Sub
This calls 2 procedures.
Sub FindFood()
If Snake(0).Location = lblFood.Location Then
'Head finds food and the tail grows
Length += 1
ReDim Preserve Snake(Length)
Snake(Length) = New Label
Snake(Length).Size = Snake(1).Size
Snake(Length).BackColor = Snake(1).BackColor
'Place the new body part at the end of the snake
Snake(Length).Left = Snake(Length - 1).Left
Snake(Length).Top = Snake(Length - 1).Top
Controls.Add(Snake(Length))
PlaceFood()
Me.Text = "Snake has eaten: " & (Length - 4).ToString
End If
End Sub
and:
Sub MoveSnake()
'Move body
For i As Integer = Length To 1 Step -1
Snake(i).Left = Snake(i - 1).Left
Snake(i).Top = Snake(i - 1).Top
Next
'Move head. If it hits wall then move opposite direction
If Snake(0).Tag = "Left" Then
Snake(0).Left = Snake(0).Left - 10
If Snake(0).Left < 20 Then
Snake(0).Tag = "Right"
End If
ElseIf Snake(0).Tag = "Up" Then
Snake(0).Top = Snake(0).Top - 10
If Snake(0).Top < 20 Then
Snake(0).Tag = "Down"
End If
ElseIf Snake(0).Tag = "Right" Then
Snake(0).Left = Snake(0).Left + 10
If Snake(0).Left > Me.Width - 40 Then
Snake(0).Tag = "Left"
End If
ElseIf Snake(0).Tag = "Down" Then
Snake(0).Top = Snake(0).Top + 10
If Snake(0).Top > Me.Height - 70 Then
Snake(0).Tag = "Up"
End If
End If
End Sub
Now all that's required is to be able to steer the snake. Click in the Form_Load procedure then select the KeyDown event. The arrow keys change the Snakes' direction and the N key starts a new snake.
Private Sub frmSnake_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
'Change direction of the head
Case Keys.Left : Snake(0).Tag = "Left"
Case Keys.Up : Snake(0).Tag = "Up"
Case Keys.Right : Snake(0).Tag = "Right"
Case Keys.Down : Snake(0).Tag = "Down"
Case Keys.N 'Start a New game
'Erase the current snake
For i As Integer = 0 To Length
Snake(i).BackColor = DefaultBackColor
Snake(i).Top = 0
Snake(i).Left = 0
Next
'Erase the food
lblFood.BackColor = DefaultBackColor
lblFood.Top = 0
lblFood.Left = 0
blnNewGame = True
'Create new snake
Length = 4
frmSnake_Load(sender, e)
End Select
End Sub
End Class
I hope you enjoy this slightly addictive game.
Cheers,
Rudi