OfficialSmudge

Console Snake

Sep 28th, 2015
3,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.Runtime.InteropServices;
  10.  
  11. namespace ConsoleSnake
  12. {
  13.     class Vector2
  14.     {
  15.         public int X;
  16.         public int Y;
  17.         public Vector2(int x, int y)
  18.         {
  19.             X = x;
  20.             Y = y;
  21.         }
  22.     }
  23.  
  24.     class Program
  25.     {
  26.         static List<Vector2> snakePos = new List<Vector2>();
  27.         static int direction = 1;
  28.         static Random r = new Random();
  29.         static Vector2 foodPos;
  30.         static double lastT = 0;
  31.         static int sN = 0;
  32.         static Vector2 screenBounds = new Vector2(80, 24);
  33.         static ConsoleColor[] rainbowColor = new ConsoleColor[] {ConsoleColor.Red, ConsoleColor.Yellow, ConsoleColor.Green, ConsoleColor.Blue};
  34.         static bool canControl = true;
  35.  
  36.         static void reset()
  37.         {
  38.             snakePos.Clear();
  39.             foodPos = new Vector2(r.Next(1, screenBounds.X), r.Next(1, screenBounds.Y));
  40.             for (int i = 0; i < 6; i++)
  41.                 snakePos.Add(new Vector2(i + 1, 6));
  42.             draw(true);
  43.         }
  44.  
  45.         static void getInput()
  46.         {
  47.             while (!canControl)
  48.             {
  49.             }
  50.                 ConsoleKey ck = Console.ReadKey().Key;
  51.                 if (ck == ConsoleKey.UpArrow && direction != 1)
  52.                     direction = 0;
  53.                 else
  54.                     if (ck == ConsoleKey.DownArrow && direction != 0)
  55.                         direction = 1;
  56.                     else
  57.                         if (ck == ConsoleKey.LeftArrow && direction != 3)
  58.                             direction = 2;
  59.                         else
  60.                             if (ck == ConsoleKey.RightArrow && direction != 2)
  61.                                 direction = 3;
  62.                             else
  63.                                 if (ck == ConsoleKey.Spacebar)
  64.                                     draw(true);
  65.                 canControl = false;
  66.             getInput();
  67.         }
  68.  
  69.         static void Main(string[] args)
  70.         {
  71.             reset();
  72.  
  73.             new Thread(getInput).Start();
  74.             draw(true);
  75.  
  76.             while (true)
  77.             {
  78.                 for (int i = snakePos.Count - 1; i > 0; i--)
  79.                     snakePos[i] = snakePos[i - 1];
  80.  
  81.                 if (direction == 0)
  82.                     snakePos[0] = new Vector2(snakePos[0].X, snakePos[0].Y - 1);
  83.                 if (direction == 1)
  84.                     snakePos[0] = new Vector2(snakePos[0].X, snakePos[0].Y + 1);
  85.                 if (direction == 2)
  86.                     snakePos[0] = new Vector2(snakePos[0].X - 1, snakePos[0].Y);
  87.                 if (direction == 3)
  88.                     snakePos[0] = new Vector2(snakePos[0].X + 1, snakePos[0].Y);
  89.  
  90.                 foreach (Vector2 sp in snakePos)
  91.                     if (sp.X == foodPos.X && sp.Y == foodPos.Y)
  92.                     {
  93.                         foodPos = new Vector2(r.Next(1, screenBounds.X), r.Next(1, screenBounds.Y));
  94.                         snakePos.Add(snakePos[snakePos.Count - 1]);
  95.                         break;
  96.                     }
  97.  
  98.                 if (snakePos[0].Y < 0)
  99.                     snakePos[0].Y = screenBounds.Y - 1;
  100.  
  101.                 if (snakePos[0].X < 0)
  102.                     snakePos[0].X = screenBounds.X - 1;
  103.  
  104.                 if (snakePos[0].Y > screenBounds.Y - 1)
  105.                     snakePos[0].Y = 0;
  106.  
  107.                 if (snakePos[0].X > screenBounds.X - 1)
  108.                     snakePos[0].X = 0;
  109.  
  110.                 for (int i = snakePos.Count - 2; i > 0; i--)
  111.                     if (snakePos[i].X == snakePos[0].X && snakePos[i].Y == snakePos[0].Y)
  112.                     {
  113.                         reset();
  114.                         break;
  115.                     }
  116.                 canControl = true;
  117.                 draw(true);
  118.                 Thread.Sleep(50);
  119.             }
  120.         }
  121.  
  122.         static void draw(bool clear)
  123.         {
  124.             if (!clear)
  125.             {
  126.                 foreach (Vector2 pos in snakePos)
  127.                 {
  128.                     Console.SetCursorPosition(pos.X, pos.Y);
  129.                     Console.Write('O');
  130.                 }
  131.                 Console.ForegroundColor = ConsoleColor.Yellow;
  132.                 Console.SetCursorPosition(foodPos.X, foodPos.Y);
  133.                 Console.Write('*');
  134.                 Console.ForegroundColor = ConsoleColor.Gray;
  135.                
  136.                 Console.SetCursorPosition(snakePos[snakePos.Count - 1].X, snakePos[snakePos.Count - 1].Y);
  137.                 Console.Write(' ');
  138.             }
  139.             else
  140.             {
  141.                 Console.Clear();
  142.                 char[,] drawChars = new char[screenBounds.X, screenBounds.Y];
  143.  
  144.                 drawChars[0, 0] = 'a';
  145.                 for (int x = 0; x < drawChars.GetLength(0); x++)
  146.                     for (int y = 0; y < drawChars.GetLength(1); y++)
  147.                         drawChars[x, y] = ' ';
  148.  
  149.                 for (int i = snakePos.Count - 2; i > 0; i--)
  150.                     drawChars[snakePos[i].X, snakePos[i].Y] = 'O';
  151.                 drawChars[foodPos.X, foodPos.Y] = '*';
  152.  
  153.                 for (int y = 0; y < drawChars.GetLength(1); y++)
  154.                     for (int x = 0; x < drawChars.GetLength(0); x++)
  155.                     {
  156.                         if (drawChars[x, y] == '*')
  157.                             Console.ForegroundColor = ConsoleColor.Yellow;
  158.  
  159.                         for (int i = 0; i < snakePos.Count - 2; i++)
  160.                             if (x == snakePos[i].X && y == snakePos[i].Y && false)
  161.                             {
  162.                                 int ii = i;
  163.                                 while (ii > rainbowColor.Length - 1)
  164.                                     ii -= rainbowColor.Length;
  165.                                 Console.ForegroundColor = rainbowColor[ii];
  166.                             }
  167.  
  168.                         Console.Write(drawChars[x, y]);
  169.                         Console.ForegroundColor = ConsoleColor.Gray;
  170.                     }
  171.             }
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment