Advertisement
wingman007

SnakeFoodObstacles2b

Oct 14th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.47 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.  
  7. namespace Snake2b
  8. {
  9.     class Program
  10.     {
  11.         struct Point
  12.         {
  13.             public int x, y;
  14.             public Point(int x, int y)
  15.             {
  16.                 this.x = x;
  17.                 this.y = y;
  18.             }
  19.         }
  20.  
  21.         public enum Direction { Left, Right, Up, Down}
  22.  
  23.         static void Main(string[] args)
  24.         {
  25. /*
  26.             Point a = new Point(10, 10);
  27.             Point b = a;
  28.  
  29.             b.x = 20;
  30.             b.y = 15;
  31.  
  32.             Console.WriteLine("a.x = {0} a.y ={1}", a.x, a.y);
  33.             Console.WriteLine("b.x = {0} b.y ={1}", b.x, b.y);
  34.  
  35.             return;
  36. */
  37. //            Point[] snake = new Point[100];
  38.  
  39.             Queue<Point> snake = new Queue<Point>();
  40.             for (int i = 0; i < 5; i++) {
  41.                 snake.Enqueue(new Point(i, 0));
  42.             }
  43.  
  44.             List<Point> obstacles = new List<Point>() {
  45.               new Point(10, 10),
  46.               new Point(70, 15),
  47.               new Point(45, 20)
  48.             };
  49.  
  50.             ConsoleKeyInfo cki;
  51.            
  52.             int positionX = 0;
  53.             int positionY = 0;
  54.  
  55. //            int position1X = 0;
  56. //            int position1Y = 1;
  57.  
  58.             Direction direction = Direction.Right;
  59.             char symbol = '*';
  60.  
  61.             Point segment, head, newHead = new Point(0,0), food;
  62.  
  63.             Random randomNumber = new Random();
  64.  
  65.             do {
  66.                 food = new Point(
  67.                     randomNumber.Next(0, Console.WindowWidth - 1),
  68.                     randomNumber.Next(0, Console.WindowHeight - 1));            
  69.             } while(snake.Contains(food) || obstacles.Contains(food));
  70.  
  71.             bool gameRunning = true;
  72.  
  73.             Console.Title = "Arrow keys to steer. Esc to exit.";
  74.             do
  75.             {
  76.                 // 1. loop. Endless loop of the life
  77.                 while (!Console.KeyAvailable)
  78.                 {
  79.                     head = snake.Last();
  80.                     switch (direction) {
  81.                         case Direction.Right:
  82.                             // if (positionX < Console.WindowWidth - 1) positionX++;
  83.                             // if (positionX == Console.WindowWidth - 1) positionX = 0;
  84.                             // positionX++;
  85.                             if (head.x == Console.WindowWidth - 1)
  86.                             {
  87.                                 newHead = new Point(0, head.y);
  88.                             }
  89.                             else {
  90.                                 newHead = new Point(head.x + 1, head.y);
  91.                             }
  92.                             symbol = '>';
  93.                             break;
  94.                         case Direction.Left:
  95.                             // if (positionX > 0) positionX--;
  96.                             // if (positionX == 0) positionX = Console.WindowWidth - 1;
  97.                             // positionX--;
  98.                             if (head.x == 0)
  99.                             {
  100.                                 newHead = new Point(Console.WindowWidth - 1, head.y);
  101.                             }
  102.                             else
  103.                             {
  104.                                 newHead = new Point(head.x - 1, head.y);
  105.                             }
  106.                             symbol = '<';
  107.                             break;
  108.                         case Direction.Up:
  109.                             // if (positionY > 0) positionY--;
  110.                             // if (positionY == 0) positionY = Console.WindowHeight - 1;
  111.                             // positionY--;
  112.                             if (head.y == 0)
  113.                             {
  114.                                 newHead = new Point(head.x, Console.WindowHeight - 1);
  115.                             }
  116.                             else
  117.                             {
  118.                                 newHead = new Point(head.x, head.y - 1);
  119.                             }
  120.                             symbol = '^';
  121.                             break;
  122.                         case Direction.Down:
  123.                             // if (positionY < Console.WindowHeight) positionY++;
  124.                             // if (positionY == Console.WindowHeight - 1) positionY = 0;
  125.                             // positionY++;
  126.                             if (head.y == Console.WindowHeight - 1)
  127.                             {
  128.                                 newHead = new Point(head.x, 0);
  129.                             }
  130.                             else
  131.                             {
  132.                                 newHead = new Point(head.x, head.y + 1);
  133.                             }
  134.                             symbol = 'v';
  135.                             break;
  136.                     }
  137.  
  138.                     snake.Enqueue(newHead);
  139.  
  140.                     // collisian detection food
  141.                     if (newHead.x == food.x && newHead.y == food.y)
  142.                     {
  143.                         do {
  144.                               food = new Point(
  145.                                    randomNumber.Next(0, Console.WindowWidth - 1),
  146.                                    randomNumber.Next(0, Console.WindowHeight - 1));
  147.                         } while(snake.Contains(food) || obstacles.Contains(food));
  148.                     }
  149.                     else
  150.                     {
  151.                         snake.Dequeue();
  152.                     }
  153.  
  154.                     // collision detection with obstacle
  155.                     if (obstacles.Contains(newHead))
  156.                     {
  157.                         gameRunning = false;
  158.                         break;
  159.                     }
  160.  
  161.                     Console.Clear();
  162.  
  163.                     // food
  164.                     Console.SetCursorPosition(food.x, food.y);
  165.                     Console.ForegroundColor = ConsoleColor.Yellow;
  166.                     Console.Write("@");
  167.  
  168.                     // obstacles
  169.                     foreach(Point obstacle in obstacles){
  170.                         Console.SetCursorPosition(obstacle.x, obstacle.y);
  171.                         Console.ForegroundColor = ConsoleColor.Blue;
  172.                         Console.Write("#");                        
  173.                     }
  174.  
  175.                     // Console.SetCursorPosition(positionX, positionY);
  176.                     Console.ForegroundColor = ConsoleColor.Green;
  177.                     // Console.BackgroundColor = ConsoleColor.Yellow;
  178.                     // Console.Write(symbol); // "*"
  179.                     // Console.WriteLine("The ifinite loop");
  180.                     for (int i = 0; i < snake.Count; i++)
  181.                     {
  182.                         segment = snake.ElementAt(i);
  183.                         Console.SetCursorPosition(segment.x, segment.y);
  184.                         if (i == snake.Count - 1)
  185.                         {
  186.                             Console.Write(symbol);
  187.                         }
  188.                         else
  189.                         {
  190.                             Console.Write("*");
  191.                         }
  192.                     }
  193.  
  194.                     System.Threading.Thread.Sleep(150);
  195.                 }
  196.  
  197.                 if (!gameRunning)
  198.                 {
  199.                     Console.SetCursorPosition(10, 10);
  200.                     Console.ForegroundColor = ConsoleColor.Yellow;
  201.                     Console.Write("Game Over! Hit Esc to exit!");
  202.                 }
  203.  
  204.                 // 2. event happend. Meteorit is passing by.
  205.                 cki = Console.ReadKey(true);
  206.                 switch (cki.Key) {
  207.                     case ConsoleKey.RightArrow :
  208.                         direction = Direction.Right;
  209.                         // if (positionX < Console.WindowWidth - 1) positionX++;
  210.                         break;
  211.                     case ConsoleKey.LeftArrow:
  212.                         direction = Direction.Left;
  213.                         // if (positionX > 0) positionX--;
  214.                         break;
  215.                     case ConsoleKey.UpArrow:
  216.                         direction = Direction.Up;
  217.                         // if (positionY > 0) positionY--;
  218.                         break;
  219.                     case ConsoleKey.DownArrow:
  220.                         direction = Direction.Down;
  221.                         // if (positionY < Console.WindowHeight) positionY++;
  222.                         break;
  223.                 }
  224.             } while (cki.Key != ConsoleKey.Escape);
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement