Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp11
  9. {
  10.     enum Direction
  11.     {
  12.         North,
  13.         East,
  14.         South,
  15.         West
  16.     }
  17.  
  18.     class Variables
  19.     {
  20.         public static int BoardWidth { get; set; }
  21.         public static int BoardHeight { get; set; }
  22.         public static int TrailSpeed { get; set; }
  23.  
  24.         public static Direction direction { get; set; }  
  25.         public static int GameScore { get; set; }
  26.         public static bool IsDead { get; set; }
  27.         public static string[,] GameBoard { get; set; }
  28.  
  29.         public Variables()
  30.         {
  31.             // Settings
  32.             BoardWidth = 25;
  33.             BoardHeight = 25;
  34.             TrailSpeed = 100;
  35.  
  36.             direction = Direction.South;
  37.             GameScore = 0;
  38.             IsDead = false;
  39.             GameBoard = new string[Variables.BoardHeight, Variables.BoardWidth];
  40.         }
  41.     }
  42.  
  43.     class Body
  44.     {
  45.         public int X { get; set; }
  46.         public int Y { get; set; }
  47.     }
  48.  
  49.     class Program
  50.     {
  51.         public static List<Body> Trail = new List<Body>();
  52.  
  53.         public static void GetDirection()
  54.         {
  55.             if (Console.KeyAvailable == true)
  56.             {
  57.                 var command = Console.ReadKey().Key;
  58.  
  59.                 // Assign direction with arrow key pressed
  60.                 switch (command)
  61.                 {
  62.                     case ConsoleKey.UpArrow:
  63.                         Variables.direction = Direction.North;
  64.                         break;
  65.  
  66.                     case ConsoleKey.RightArrow:
  67.                         Variables.direction = Direction.East;
  68.                         break;
  69.  
  70.                     case ConsoleKey.DownArrow:
  71.                         Variables.direction = Direction.South;
  72.                         break;
  73.  
  74.                     case ConsoleKey.LeftArrow:
  75.                         Variables.direction = Direction.West;
  76.                         break;
  77.  
  78.                     case ConsoleKey.Spacebar:
  79.                         Body body = new Body { X = Trail[Trail.Count - 1].X, Y = Trail[Trail.Count - 1].Y };
  80.                         Trail.Add(body);
  81.                         break;
  82.                 }
  83.             }
  84.         }
  85.  
  86.         public static void UpdateGame()
  87.         {
  88.            
  89.  
  90.  
  91.  
  92.             for (int i = Trail.Count - 1; i >= 0; i--)
  93.             {
  94.                 // If the head
  95.                 if (i == 0)
  96.                 {
  97.                     switch (Variables.direction)
  98.                     {
  99.  
  100.                         case Direction.North:
  101.                             Trail[i].Y--;
  102.                             break;
  103.  
  104.                         case Direction.East:
  105.                             Trail[i].X++;
  106.                             break;
  107.  
  108.                         case Direction.South:
  109.                             Trail[i].Y++;
  110.                             break;
  111.  
  112.                         case Direction.West:
  113.                             Trail[i].X--;
  114.                             break;
  115.                     }
  116.                 }
  117.  
  118.                 // If the body
  119.                 else
  120.                 {
  121.                     Trail[i].X = Trail[i - 1].X;
  122.                     Trail[i].Y = Trail[i - 1].Y;
  123.                 }
  124.  
  125.                 // Reset Board
  126.                 for (int k = 0; k < Variables.BoardHeight; k++)
  127.                 {
  128.                     for (int j = 0; j < Variables.BoardWidth; j++)
  129.                     {
  130.                         Variables.GameBoard[k, j] = " ";
  131.                     }
  132.                 }
  133.  
  134.                 // Make Trail
  135.                 for (int j = Trail.Count - 1; j >= 0; j--)
  136.                 {
  137.                     Variables.GameBoard[Trail[j].Y, Trail[j].X] = "#";
  138.                 }
  139.  
  140.                 // Output Trail          
  141.                 string output = "";
  142.  
  143.                 for (int k = 0; k < Variables.BoardWidth; k++)
  144.                 {
  145.                     for (int j = 0; j < Variables.BoardHeight; j++)
  146.                     {
  147.                         output += (Variables.GameBoard[k, j]);
  148.                     }
  149.                     output += (Environment.NewLine);
  150.                 }
  151.                 Console.Clear();
  152.                 Console.WriteLine(output);
  153.             }
  154.         }
  155.  
  156.         public static void Main(string[] args)
  157.         {
  158.             new Variables();
  159.  
  160.             Console.CursorVisible = false;
  161.  
  162.             Body head = new Body { X = 2, Y = 2 };
  163.             Trail.Add(head);
  164.  
  165.             Console.SetWindowSize(Variables.BoardWidth, Variables.BoardHeight);
  166.  
  167.                 Thread t1 = new Thread(() =>
  168.                 {
  169.                     while (true)
  170.                     {
  171.                         GetDirection();
  172.                     }
  173.                      
  174.                 });
  175.  
  176.                 Thread t2 = new Thread(() =>
  177.                 {
  178.                     while (true)
  179.                     {
  180.                         Thread.Sleep(100);
  181.                         UpdateGame();
  182.                     }
  183.  
  184.                 });
  185.  
  186.             t1.Start();
  187.             t2.Start();
  188.         }  
  189.     }  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement