TizzyT

ConsoleSnake -TizzyT

Nov 13th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.92 KB | None | 0 0
  1. Imports System.Text
  2. Imports System.Timers
  3.  
  4. Module Module1
  5.     Private Spd As Integer = 500
  6.     Private Boost As Single = 1.005
  7.     Private KM As New KeyboardQueue
  8.     Private RND As New Random()
  9.  
  10.     Sub Main()
  11.         Console.Title = "CLI Snake"
  12.         Console.ForegroundColor = ConsoleColor.White
  13.         Console.CursorVisible = False
  14.  
  15.         While True
  16.             If Logic() Then
  17.                 DrawGame()
  18.                 Threading.Thread.Sleep(Spd)
  19.             Else
  20.                 Console.BackgroundColor = ConsoleColor.DarkRed
  21.                 DrawGame(True)
  22.             End If
  23.         End While
  24.         Console.ReadLine()
  25.     End Sub
  26.  
  27.     Private Structure Point
  28.         Public X As Integer
  29.         Public Y As Integer
  30.         Public Sub New(ByVal X As Integer, ByVal Y As Integer)
  31.             Me.X = X
  32.             Me.Y = Y
  33.         End Sub
  34.     End Structure
  35.  
  36.     Private Tracker As New List(Of Point)
  37.     Private Dir As Direction = Direction.Up
  38.     Private PointX As Integer = 12
  39.     Private PointY As Integer = 12
  40.     Private Score As Integer = 0
  41.  
  42.     Private Enum Direction
  43.         Up = 0
  44.         Left = 1
  45.         Right = 2
  46.         Down = 3
  47.     End Enum
  48.  
  49.     Private Function Logic() As Boolean
  50.         If Tracker.Count = 0 Then
  51.             Dir = Direction.Up
  52.             PointX = 12
  53.             PointY = 12
  54.             Tracker.Add(New Point(12, 12))
  55.             DropPoint()
  56.         Else
  57.  
  58.             Dim K As KnMmonitor.KeyBoardKeys
  59.  
  60.             While True
  61.                 Try
  62.                     K = KM.DequeueKey
  63.                 Catch ex As Exception
  64.                     Exit While
  65.                 End Try
  66.                 If K = KnMmonitor.KeyBoardKeys.Left Then
  67.                     If Dir <> Direction.Right Then
  68.                         Dir = Direction.Left
  69.                         Exit While
  70.                     End If
  71.                 ElseIf K = KnMmonitor.KeyBoardKeys.Right Then
  72.                     If Dir <> Direction.Left Then
  73.                         Dir = Direction.Right
  74.                         Exit While
  75.                     End If
  76.                 ElseIf K = KnMmonitor.KeyBoardKeys.Up Then
  77.                     If Dir <> Direction.Down Then
  78.                         Dir = Direction.Up
  79.                         Exit While
  80.                     End If
  81.                 ElseIf K = KnMmonitor.KeyBoardKeys.Down Then
  82.                     If Dir <> Direction.Up Then
  83.                         Dir = Direction.Down
  84.                         Exit While
  85.                     End If
  86.                 End If
  87.             End While
  88.  
  89.             Select Case Dir
  90.                 Case Direction.Up
  91.                     If Tracker(0).Y < 1 Then
  92.                         Return False
  93.                     Else
  94.                         If Tracker(0).X = PointX AndAlso Tracker(0).Y - 1 = PointY Then
  95.                             Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y - 1))
  96.                             DropPoint()
  97.                             Spd /= Boost
  98.                         Else
  99.                             Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y - 1))
  100.                             Tracker.RemoveAt(Tracker.Count - 1)
  101.                         End If
  102.                     End If
  103.                 Case Direction.Left
  104.                     If Tracker(0).X < 1 Then
  105.                         Return False
  106.                     Else
  107.                         If Tracker(0).X - 1 = PointX AndAlso Tracker(0).Y = PointY Then
  108.                             Tracker.Insert(0, New Point(Tracker(0).X - 1, Tracker(0).Y))
  109.                             DropPoint()
  110.                             Spd /= Boost
  111.                         Else
  112.                             Tracker.Insert(0, New Point(Tracker(0).X - 1, Tracker(0).Y))
  113.                             Tracker.RemoveAt(Tracker.Count - 1)
  114.                         End If
  115.                     End If
  116.                 Case Direction.Right
  117.                     If Tracker(0).X > 24 Then
  118.                         Return False
  119.                     Else
  120.                         If Tracker(0).X + 1 = PointX AndAlso Tracker(0).Y = PointY Then
  121.                             Tracker.Insert(0, New Point(Tracker(0).X + 1, Tracker(0).Y))
  122.                             DropPoint()
  123.                             Spd /= Boost
  124.                         Else
  125.                             Tracker.Insert(0, New Point(Tracker(0).X + 1, Tracker(0).Y))
  126.                             Tracker.RemoveAt(Tracker.Count - 1)
  127.                         End If
  128.                     End If
  129.                 Case Direction.Down
  130.                     If Tracker(0).Y > 24 Then
  131.                         Return False
  132.                     Else
  133.                         If Tracker(0).X = PointX AndAlso Tracker(0).Y + 1 = PointY Then
  134.                             Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y + 1))
  135.                             DropPoint()
  136.                             Spd /= Boost
  137.                         Else
  138.                             Tracker.Insert(0, New Point(Tracker(0).X, Tracker(0).Y + 1))
  139.                             Tracker.RemoveAt(Tracker.Count - 1)
  140.                         End If
  141.                     End If
  142.             End Select
  143.  
  144.             For i = 1 To Tracker.Count - 1
  145.                 If Tracker(0).X = Tracker(i).X Then
  146.                     If Tracker(0).Y = Tracker(i).Y Then
  147.                         Return False
  148.                     End If
  149.                 End If
  150.             Next
  151.         End If
  152.         Return True
  153.     End Function
  154.  
  155.     Private Sub DropPoint()
  156.         While Tracker.Contains(New Point(PointX, PointY))
  157.             PointX = RND.Next(0, 25)
  158.             PointY = RND.Next(0, 25)
  159.         End While
  160.     End Sub
  161.  
  162.     Private Sub DrawGame(Optional ByVal Lost As Boolean = False)
  163.         Console.SetCursorPosition(0, 0)
  164.         Dim output As New StringBuilder
  165.         output.AppendLine("Score: " & Tracker.Count - 1 & vbTab & "Speed: " & 500 / Spd)
  166.         output.AppendLine("=======================================================")
  167.         For y = 0 To 25
  168.             output.Append("|")
  169.             For x = 0 To 25
  170.                 If Tracker.Contains(New Point(x, y)) Then
  171.                     output.Append(" #")
  172.                 Else
  173.                     If x = PointX AndAlso y = PointY Then
  174.                         output.Append(" $")
  175.                     Else
  176.                         output.Append("  ")
  177.                     End If
  178.                 End If
  179.             Next
  180.             output.AppendLine(" |")
  181.         Next
  182.         output.AppendLine("=======================================================")
  183.         If Lost Then
  184.             output.AppendLine("GAME OVER!!! , Press Enter To Play Again ...")
  185.             Console.Clear()
  186.         End If
  187.         Console.Write(output.ToString)
  188.         If Lost Then
  189.             Console.ReadLine()
  190.             Tracker.Clear()
  191.             Console.BackgroundColor = ConsoleColor.Black
  192.         End If
  193.     End Sub
  194. End Module
Advertisement
Add Comment
Please, Sign In to add comment