Public Class Form1
Dim m As Integer = 4
Dim p(m) As Point
Dim collision As Boolean = False
Dim NowUp As Boolean = False
Dim NowDown As Boolean = False
Dim NowLeft As Boolean = False
Dim NowRight As Boolean = True
Dim foodeaten As Boolean = True
Dim rng As New Random
Dim fx, fy As Integer
Dim f As Integer = 1
Dim foodpoint As Point
Dim d As New Bitmap(512, 256)
Dim g2 As Graphics = Graphics.FromImage(d)
Private Enum Direction
Up
Down
Right
Left
End Enum
Dim currdirection As Direction = Direction.Right
#Region "SnakeField"
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim b As New Bitmap(512, 256)
Dim g As Graphics = Graphics.FromImage(b)
Select Case currdirection
Case Direction.Left
If NowRight = False Then
If p(m).X > PictureBox1.Bounds.Left Then
p(m).X -= 16
NowLeft = True
NowUp = False
NowDown = False
NowRight = False
Else
collision = True
End If
Else
p(m).X += 16
NowRight = True
End If
Case Direction.Right
If NowLeft = False Then
If p(m).X < PictureBox1.Width - 16 Then
p(m).X += 16
NowLeft = False
NowUp = False
NowDown = False
NowRight = True
Else
collision = True
End If
Else
p(m).X -= 16
NowLeft = True
End If
Case Direction.Up
If NowDown = False Then
If p(m).Y > 0 Then
p(m).Y -= 16
NowLeft = False
NowUp = True
NowDown = False
NowRight = False
Else
collision = True
End If
Else
p(m).Y += 16
NowDown = True
End If
Case Direction.Down
If NowUp = False Then
If p(m).Y < PictureBox1.Bounds.Height - 16 Then
p(m).Y += 16
NowLeft = False
NowUp = False
NowDown = True
NowRight = False
Else
collision = True
End If
Else
p(m).Y -= 16
NowUp = True
End If
End Select
If collision = False Then
For i As Integer = 0 To m - 1
p(i) = p(i + 1)
g.FillRectangle(Brushes.Black, New Rectangle(p(i), New Size(16, 16)))
Next
g.FillRectangle(Brushes.Black, New Rectangle(p(m), New Size(16, 16)))
g.Dispose()
PictureBox1.Image = b
Me.Refresh()
Me.Refresh()
End If
End Sub
#End Region
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
currdirection = Direction.Left
End If
If e.KeyCode = Keys.Right Then
currdirection = Direction.Right
End If
If e.KeyCode = Keys.Up Then
currdirection = Direction.Up
End If
If e.KeyCode = Keys.Down Then
currdirection = Direction.Down
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If foodeaten Then
Call PlaceNewFood()
foodeaten = False
End If
If foodpoint = p(m) Then
foodeaten = True
End If
End Sub
Public Sub PlaceNewFood(Optional ByVal EmptyCell As Boolean = False)
fx = rng.Next(0, PictureBox1.Width)
fx = fx / 16
fx = fx * 16
fy = rng.Next(0, PictureBox1.Height)
fy = fy / 16
fy = fy * 16
foodpoint = New Point(fx, fy)
For i As Integer = 0 To m - 1
p(i) = p(i + 1)
g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
Next
g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
g2.Dispose()
PictureBox1.BackgroundImage = d
Me.Refresh()
Me.Refresh()
End Sub
End Class