Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Text
- Imports System.Timers
- Module Module1
- Private Spd As Integer = 500
- Private Boost As Single = 1.005
- Private KM As New KeyboardQueue
- Private RND As New Random()
- Sub Main()
- Console.Title = "CLI Snake"
- Console.ForegroundColor = ConsoleColor.White
- Console.CursorVisible = False
- While True
- If Logic() Then
- DrawGame()
- Threading.Thread.Sleep(Spd)
- Else
- Console.BackgroundColor = ConsoleColor.DarkRed
- DrawGame(True)
- End If
- End While
- Console.ReadLine()
- End Sub
- Private Structure Point
- Public X As Integer
- Public Y As Integer
- Public Sub New(ByVal X As Integer, ByVal Y As Integer)
- Me.X = X
- Me.Y = Y
- End Sub
- End Structure
- Private Tracker As New List(Of Point)
- Private Dir As Direction = Direction.Up
- Private PointX As Integer = 12
- Private PointY As Integer = 12
- Private Score As Integer = 0
- Private Enum Direction
- Up = 0
- Left = 1
- Right = 2
- Down = 3
- End Enum
- Private Function Logic() As Boolean
- If Tracker.Count = 0 Then
- Dir = Direction.Up
- PointX = 12
- PointY = 12
- Tracker.Add(New Point(12, 12))
- DropPoint()
- Else
- Dim K As KnMmonitor.KeyBoardKeys
- While True
- Try
- K = KM.DequeueKey
- Catch ex As Exception
- Exit While
- End Try
- If K = KnMmonitor.KeyBoardKeys.Left Then
- If Dir <> Direction.Right Then
- Dir = Direction.Left
- Exit While
- End If
- ElseIf K = KnMmonitor.KeyBoardKeys.Right Then
- If Dir <> Direction.Left Then
- Dir = Direction.Right
- Exit While
- End If
- ElseIf K = KnMmonitor.KeyBoardKeys.Up Then
- If Dir <> Direction.Down Then
- Dir = Direction.Up
- Exit While
- End If
- ElseIf K = KnMmonitor.KeyBoardKeys.Down Then
- If Dir <> Direction.Up Then
- Dir = Direction.Down
- Exit While
- End If
- End If
- End While
- Select Case Dir
- Case Direction.Up
- If Tracker(0).Y < 1 Then
- Return False
- Else
- If Tracker(0).X = PointX AndAlso Tracker(0).Y - 1 = PointY Then
- Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y - 1))
- DropPoint()
- Spd /= Boost
- Else
- Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y - 1))
- Tracker.RemoveAt(Tracker.Count - 1)
- End If
- End If
- Case Direction.Left
- If Tracker(0).X < 1 Then
- Return False
- Else
- If Tracker(0).X - 1 = PointX AndAlso Tracker(0).Y = PointY Then
- Tracker.Insert(0, New Point(Tracker(0).X - 1, Tracker(0).Y))
- DropPoint()
- Spd /= Boost
- Else
- Tracker.Insert(0, New Point(Tracker(0).X - 1, Tracker(0).Y))
- Tracker.RemoveAt(Tracker.Count - 1)
- End If
- End If
- Case Direction.Right
- If Tracker(0).X > 24 Then
- Return False
- Else
- If Tracker(0).X + 1 = PointX AndAlso Tracker(0).Y = PointY Then
- Tracker.Insert(0, New Point(Tracker(0).X + 1, Tracker(0).Y))
- DropPoint()
- Spd /= Boost
- Else
- Tracker.Insert(0, New Point(Tracker(0).X + 1, Tracker(0).Y))
- Tracker.RemoveAt(Tracker.Count - 1)
- End If
- End If
- Case Direction.Down
- If Tracker(0).Y > 24 Then
- Return False
- Else
- If Tracker(0).X = PointX AndAlso Tracker(0).Y + 1 = PointY Then
- Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y + 1))
- DropPoint()
- Spd /= Boost
- Else
- Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y + 1))
- Tracker.RemoveAt(Tracker.Count - 1)
- End If
- End If
- End Select
- For i = 1 To Tracker.Count - 1
- If Tracker(0).X = Tracker(i).X Then
- If Tracker(0).Y = Tracker(i).Y Then
- Return False
- End If
- End If
- Next
- End If
- Return True
- End Function
- Private Sub DropPoint()
- While Tracker.Contains(New Point(PointX, PointY))
- PointX = RND.Next(0, 25)
- PointY = RND.Next(0, 25)
- End While
- End Sub
- Private Sub DrawGame(Optional ByVal Lost As Boolean = False)
- Console.SetCursorPosition(0, 0)
- Dim output As New StringBuilder
- output.AppendLine("Score: " & Tracker.Count - 1 & vbTab & "Speed: " & 500 / Spd)
- output.AppendLine("=======================================================")
- For y = 0 To 25
- output.Append("|")
- For x = 0 To 25
- If Tracker.Contains(New Point(x, y)) Then
- output.Append(" #")
- Else
- If x = PointX AndAlso y = PointY Then
- output.Append(" $")
- Else
- output.Append(" ")
- End If
- End If
- Next
- output.AppendLine(" |")
- Next
- output.AppendLine("=======================================================")
- If Lost Then
- output.AppendLine("GAME OVER!!! , Press Enter To Play Again ...")
- Console.Clear()
- End If
- Console.Write(output.ToString)
- If Lost Then
- Console.ReadLine()
- Tracker.Clear()
- Console.BackgroundColor = ConsoleColor.Black
- End If
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment