Advertisement
wingman007

SnakeFood1a

Oct 13th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.52 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 Snake1a
  8. {
  9.  
  10.     struct Point
  11.     {
  12.         public int x, y;
  13.         public Point(int x, int y)
  14.         {
  15.             this.x = x;
  16.             this.y = y;
  17.         }
  18.     }
  19.  
  20.     class Program
  21.     {
  22.         public enum Direction { Up, Down, Left, Right };
  23.  
  24.         static void Main(string[] args)
  25.         {
  26. //            Point a = new Point(10,10);
  27.            
  28. //            Point b = a;
  29. //            b.x = 15;
  30. //            b.y = 20;
  31. //            Console.WriteLine("a point has x = {0} y = {1}", a.x, a.y);
  32. //            Console.WriteLine("b point has x = {0} y = {1}", b.x, b.y);
  33. //            return;
  34.            
  35.             ConsoleKeyInfo cki;
  36.             int positionX = 0;
  37.             int positionY = 0;
  38.  
  39.             Queue<Point> snake = new Queue<Point>();
  40.  
  41.             for (var i = 0; i < 5; i++) {
  42.                 snake.Enqueue(new Point(i, 0));
  43.             }
  44.  
  45.             // or
  46.             // Point[] snake = new Point[100];
  47.  
  48. //            int position1X = 0;
  49. //            int position1Y = 1;
  50.  
  51.             Direction direction = Direction.Right;
  52.             char symbol = '>';
  53.  
  54.             Point segment, head, newHead = new Point(0,0), food;
  55.  
  56.             Random randomNumber = new Random();
  57.  
  58.             food = new Point(
  59.                 randomNumber.Next(0, Console.WindowWidth - 1),
  60.                 randomNumber.Next(0, Console.WindowHeight - 1));
  61.  
  62.  
  63.  
  64.             do
  65.             {
  66.                 //1. the endles llop of the life
  67.                 while (!Console.KeyAvailable)
  68.                 {
  69.                     head = snake.Last();
  70.                     // my world!!! is here
  71.                     switch (direction)
  72.                     {
  73.                         case Direction.Down:
  74.                             // if (positionY < Console.WindowHeight) positionY++;
  75.                             // if (positionY == Console.WindowHeight - 1) positionY = 0;
  76.                             // positionY++;
  77.                             if (head.y == Console.WindowHeight - 1) newHead = new Point(head.x, 0);
  78.                             else newHead = new Point(head.x, head.y + 1);
  79.                             symbol = 'V';
  80.                             break;
  81.                         case Direction.Up:
  82.                             // if (positionY > 0) positionY--;
  83.                             // if (positionY == 0) positionY = Console.WindowHeight - 1;
  84.                             // positionY--;
  85.                             if (head.y == 0) newHead = new Point(head.x, Console.WindowHeight - 1);
  86.                             else newHead = new Point(head.x, head.y - 1);
  87.                             symbol = '^';
  88.                             break;
  89.                         case Direction.Left:
  90.                             // if (positionX > 0) positionX--;
  91.                             // if (positionX == 0) positionX = Console.WindowWidth - 1;
  92.                             // positionX--;
  93.                             if (head.x == 0) newHead = new Point(Console.WindowWidth - 1, head.y);
  94.                             else newHead = new Point(head.x - 1, head.y);
  95.                             symbol = '<';
  96.                             break;
  97.                         case Direction.Right:
  98.                             // if (positionX < Console.WindowWidth - 1) positionX++;
  99.                             // if (positionX == Console.WindowWidth - 1) positionX = 0;
  100.                             // positionX++;
  101.                             if (head.x == Console.WindowWidth - 1) newHead = new Point(0, head.y);
  102.                             else newHead = new Point(head.x + 1, head.y);
  103.                             symbol = '>';
  104.                             break;
  105.                     }
  106.                     Console.Clear();
  107.                     snake.Enqueue(newHead);
  108.  
  109.                     if (food.x == newHead.x && food.y == newHead.y) {
  110.                         do {
  111.                             food = new Point(
  112.                                  randomNumber.Next(0, Console.WindowWidth - 1),
  113.                                  randomNumber.Next(0, Console.WindowHeight - 1));
  114.                         } while(snake.Contains(food));
  115.                     }
  116.                     else
  117.                     {
  118.                         snake.Dequeue();
  119.                     }
  120.  
  121.                     Console.ForegroundColor = ConsoleColor.Yellow;
  122.                     Console.SetCursorPosition(food.x, food.y);
  123.                     Console.Write("@");
  124.  
  125. //                    Console.SetCursorPosition(positionX, positionY);
  126.                     Console.ForegroundColor = ConsoleColor.Green;
  127.                     // Console.BackgroundColor = ConsoleColor.Blue;
  128. //                    Console.Write(symbol);
  129.                     for (var i = 0; i < snake.Count; i++) {
  130.                         segment = snake.ElementAt(i);
  131.                         Console.SetCursorPosition(segment.x, segment.y);
  132.                         if (i == snake.Count - 1)
  133.                         {
  134.                             Console.Write(symbol);
  135.                         }
  136.                         else
  137.                         {
  138.                             Console.Write("*");
  139.                         }  
  140.                     }
  141.  
  142.                         // Console.WriteLine("Endless loop!");
  143.                         System.Threading.Thread.Sleep(150);
  144.                 }
  145.                 // If I am out of my loop there was an Event
  146.                 // Take care of the event
  147.                 cki = Console.ReadKey(true);
  148.                 switch (cki.Key) {
  149.                     case ConsoleKey.RightArrow :
  150.                         // if (positionX < Console.WindowWidth -1) positionX++;
  151.                         direction = Direction.Right;
  152.                         break;
  153.                     case ConsoleKey.LeftArrow:
  154.                         // if (positionX > 0) positionX--;
  155.                         direction = Direction.Left;
  156.                         break;
  157.                     case ConsoleKey.UpArrow:
  158.                         // if (positionY > 0) positionY--;
  159.                         direction = Direction.Up;
  160.                         break;
  161.                     case ConsoleKey.DownArrow:
  162.                         // if (positionY < Console.WindowHeight) positionY++;
  163.                         direction = Direction.Down;
  164.                         break;
  165.                 }
  166.  
  167.  
  168.             } while (cki.Key != ConsoleKey.Escape);
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement