Advertisement
wingman007

SnakeFood1b

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