the following is my full program code for create a snake game ,i couldn't find the error in this can u check this one
Public Class Form2
#Region "snake stuff"
Dim snake(1000) As PictureBox
Dim length As Integer = -1
Dim left_right_mover As Integer = 0
Dim up_down_mover As Integer = 0
Dim r As Random
Private Sub create_head()
length += 1
snake(length) = New PictureBox
With snake(length)
.Height = 10
.Width = 10
.BackColor = Color.White
.Top = (PictureBox1.Top + PictureBox1.Bottom) / 2
.Left = (PictureBox1.Left + PictureBox1.Right) / 2
End With
Me.Controls.Add(snake(length))
snake(length).BringToFront()
lengthensnake()
lengthensnake()
End Sub
Private Sub lengthensnake()
length += 1
snake(length) = New PictureBox
With snake(length)
.Height = 10
.Width = 10
.BackColor = Color.White
.Top = snake(length - 1).Top
.Left = snake(length - 1).Left + 10
End With
Me.Controls.Add(snake(length))
snake(length).BringToFront()
End Sub
Private Sub Form2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Select Case e.KeyChar
Case "a"
left_right_mover = -10
up_down_mover = 0
Case "d"
left_right_mover = 10
up_down_mover = 0
Case "e"
up_down_mover = -10
left_right_mover = 0
Case "s"
up_down_mover = 10
left_right_mover = 0
End Select
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
For i = length To 1 Step -1
snake(i).Top = snake(i - 1).Top
snake(i).Left = snake(i - 1).Left
Next
snake(0).Top += up_down_mover
snake(0).Left += left_right_mover
collide_with_mouse()
End Sub
#End Region
#Region "mouse stuff"
Dim mouse As New PictureBox
Private Sub create_mouse()
mouse = New PictureBox
With mouse
.Width = 10
.Height = 10
.BackColor = Color.Red
.Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10) //null exception unhandeled //
.Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
End With
Me.Controls.Add(mouse)
mouse.BringToFront()
End Sub
#End Region
#Region "collission"
Private Sub collide_with_mouse()
Dim mouse As New PictureBox
If snake(0).Bounds.IntersectsWith(mouse.Bounds) Then
lengthensnake()
mouse.Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10)
mouse.Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
End If
End Sub
#End Region
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
create_head()
Timer1.Start()
create_mouse()
End Sub
End Class