Advertisement
wingman007

SnakeFoodObstacles2a

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