Advertisement
wingman007

SnakeAllSnippets

Oct 14th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.67 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 Snake
  9. {
  10.     class Program
  11.     {
  12.         public enum Direction { Left, Right, Up, Down}
  13.  
  14.         struct Point
  15.         {
  16.             public int x, y;
  17.             public Point(int x, int y)
  18.             {
  19.                 this.x = x;
  20.                 this.y = y;
  21.             }
  22.         }
  23.        
  24.         static void Main(string[] args)
  25.         {
  26.             ConsoleKeyInfo cki;
  27.             // int positionX = 0;
  28.             // int positionY = 0;
  29.             Direction direction = Direction.Right;
  30.  
  31.             Queue<Point> snake = new Queue<Point>();
  32.             for (var i = 0; i < 5; i++) {
  33.                 snake.Enqueue(new Point(0,i));
  34.             }
  35.             // or
  36.             // int[][] snakeArray = new int[100][]; // nut will be difficult
  37.  
  38.             Random randomNumber = new Random();
  39.  
  40.             Point segment, head, newHead = new Point(0, 0),
  41.                 food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  42. //                newObstacle;
  43.  
  44.             List<Point> obstacles = new List<Point>() {
  45.                 new Point(10, 20),
  46.                 new Point(34, 12),
  47.                 new Point(67, 15)
  48.             };
  49.  
  50.             bool collisionWithObstacle = false;
  51.  
  52.             double delay = 150.0;
  53.  
  54. //            foreach (Point obstacle in obstacles)
  55. //            {
  56. //                Console.SetCursorPosition(obstacle.x, obstacle.y);
  57. //                Console.ForegroundColor = ConsoleColor.Blue;
  58. //                Console.Write("#");
  59. //            }
  60.  
  61.             char symbol = '>';
  62. //            Console.WriteLine(Console.WindowHeight); // 25
  63. //            Console.WriteLine(Console.WindowWidth); // 80
  64. //            Console.SetCursorPosition(Console.WindowWidth - 1, Console.WindowHeight - 1);
  65. //            Console.SetCursorPosition(0,0);
  66. //            Console.Write("$");
  67. //            return;
  68.  
  69.             // System.Console.WriteLine(Environment.TickCount);
  70.             // return;
  71.  
  72.             int foodCounter = 0;
  73.  
  74.             int lastFeedingTime = Environment.TickCount;
  75.  
  76.             int score = 0;
  77.  
  78.             Console.Title = "Use the Arrow keys to steer! Esc to exit the fame!";
  79.  
  80.             do
  81.             {
  82.                 while (!Console.KeyAvailable)
  83.                 {
  84.                     foodCounter++;
  85.                     head = snake.Last();
  86.                     switch (direction) {
  87.                         case Direction.Right :
  88.                             symbol = '>';
  89.                             // if (positionX == Console.WindowWidth - 1) positionX = 0;
  90.                             if (head.x == Console.WindowWidth - 1) newHead = new Point(0, head.y);
  91.                             else newHead = new Point(head.x + 1, head.y);
  92.                             break;
  93.                         case Direction.Left :
  94.                             symbol = '<';
  95.                             // if (positionX == 0) positionX = Console.WindowWidth - 1;
  96.                             if (head.x == 0) newHead = new Point(Console.WindowWidth - 1, head.y);
  97.                             else newHead = new Point(head.x - 1, head.y);
  98.                             break;
  99.                         case Direction.Up :
  100.                             symbol = '^';
  101.                             // if (positionY == 0) positionY = Console.WindowHeight;
  102.                             // else positionY--;
  103.                             if (head.y == 0) newHead = new Point(head.x, Console.WindowHeight -1);
  104.                             else newHead = new Point(head.x, head.y - 1);
  105.                             break;
  106.                         case Direction.Down :
  107.                             symbol = 'V';
  108.                             // if (positionY == Console.WindowHeight) positionY = 0;
  109.                             // else positionY++;
  110.                             if (head.y == Console.WindowHeight - 1) newHead = new Point(head.x,0);
  111.                             else newHead = new Point(head.x, head.y + 1);
  112.                             break;
  113.                     }
  114.                    
  115.                     snake.Enqueue(newHead);
  116.  
  117.                     // collision with food
  118.                     if (food.x == newHead.x && food.y == newHead.y) {
  119.                         // generate new food
  120.                         // Make sure the food is not on the head of the snake
  121. //                        while (food.x == newHead.x && food.y == newHead.y)
  122. //                        {
  123. //                            food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  124. //                        }
  125.  
  126.                         // 2. the food is not on the body or obstacles
  127.                         do {
  128.                             food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  129.                         } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake
  130.  
  131.                         // 3. the food is not on the head and obstacles
  132. //                        do
  133. //                        {
  134. //                            food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  135. //                        } while ((food.x == newHead.x && food.y == newHead.y) || obstacles.Contains(food));
  136.  
  137. /*
  138.                         // add obstacle if neccessasry
  139.                         do
  140.                         {
  141.                             newObstacle = new Point(
  142.                                 randomNumber.Next(0,Console.WindowHeight - 1),
  143.                                 randomNumber.Next(0, Console.WindowWidth - 1));
  144.                         }
  145.                         while (snake.Contains(newObstacle) || obstacles.Contains(newObstacle) ||
  146.                             (food.x != newObstacle.x && food.y != newObstacle.y));
  147.                         obstacles.Add(newObstacle);
  148. */
  149. //                        Console.SetCursorPosition(newObstacle.x, newObstacle.y);
  150. //                        Console.ForegroundColor = ConsoleColor.Cyan;
  151. //                        Console.Write("=");
  152.                        
  153.                         // foodCounter = 0;
  154.                         foodCounter = 0;
  155.                         lastFeedingTime = Environment.TickCount;
  156.  
  157.                     }
  158.                     else {
  159.                         snake.Dequeue();
  160.                     }
  161.                    
  162.                     // detect colisions with obstacles
  163.                     foreach (Point obstacle in obstacles) {
  164.                         if (obstacle.x == newHead.x && obstacle.y == newHead.y) {
  165.                             collisionWithObstacle = true;
  166.                         }
  167.                     }
  168. /*
  169.                     // the time for food elapsed
  170.                     // 1.
  171.                     if (foodCounter == 80) {
  172.                         foodCounter = 0;
  173.                         do
  174.                         {
  175.                             food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  176.                         } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake    
  177.                     }
  178. */
  179.                     // 2.
  180.                     if (Environment.TickCount - lastFeedingTime  >= 8000)
  181.                     {
  182.                         lastFeedingTime = Environment.TickCount;
  183.                         do
  184.                         {
  185.                             food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
  186.                         } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake    
  187.                     }
  188.  
  189.                     Console.Clear();
  190.  
  191.                     // display food
  192.                     Console.SetCursorPosition(food.x, food.y);
  193.                     Console.ForegroundColor = ConsoleColor.Yellow;
  194.                     Console.Write("@");
  195.  
  196.                     // display obstalces
  197.                     foreach (Point obstacle in obstacles)
  198.                     {
  199.                         Console.SetCursorPosition(obstacle.x, obstacle.y);
  200.                         Console.ForegroundColor = ConsoleColor.Blue;
  201.                         Console.Write("#");
  202.                     }
  203.  
  204.  
  205.                     // Console.SetCursorPosition(positionX, positionY);
  206.                     // Console.ForegroundColor = ConsoleColor.Blue;
  207.                     Console.ForegroundColor = ConsoleColor.Green;
  208.                     // Console.Write(symbol);
  209.                     // for (var i = 0; i < snake.LongCount(); i++) {
  210.                     for (var i = 0; i < snake.Count; i++) {
  211.                         segment = snake.ElementAt(i);
  212.                         Console.SetCursorPosition(segment.x,segment.y);
  213.                        //  if (i == snake.LongCount() - 1) Console.Write(symbol);
  214.                         if (i == snake.Count - 1) Console.Write(symbol);
  215.                         else Console.Write("*"); ;
  216.                     }
  217.  
  218.                     if (collisionWithObstacle) break;
  219. //                    Console.SetCursorPosition(0,0);
  220. //                    Console.Write(Environment.TickCount);
  221.  
  222.                     // Thread.Sleep(150);
  223.                     if (delay > 50) delay -= 0.1;
  224.                     Thread.Sleep((int)delay);
  225.                 }
  226.  
  227.                 if (collisionWithObstacle) {
  228.                     Console.Clear();
  229.                     Console.ForegroundColor = ConsoleColor.Yellow;
  230.                     Console.SetCursorPosition(10, 10);
  231.                     Console.Write("GAME OVER! Score: {0}\n Hit Escape to Exit the game. Press \"R\" to retry!", (snake.Count - 6) * 100);
  232.                 }
  233.  
  234.                 cki = Console.ReadKey(true);
  235.                 switch (cki.Key) {
  236.                     case ConsoleKey.LeftArrow :
  237.                         if (direction != Direction.Right) direction = Direction.Left; // don't allow th snake to crawl over her tail
  238. //                        direction = Direction.Left;
  239.                         break;
  240.                     case ConsoleKey.RightArrow :
  241.                         if (direction != Direction.Left) direction = Direction.Right;
  242. //                        direction = Direction.Right;
  243.                         break;
  244.                     case ConsoleKey.UpArrow :
  245.                         if (direction != Direction.Down) direction = Direction.Up;
  246. //                        direction = Direction.Up;
  247.                         break;
  248.                     case ConsoleKey.DownArrow :
  249.                         if (direction != Direction.Up) direction = Direction.Down;
  250. //                        direction = Direction.Down;
  251.                         break;
  252.                 }
  253.  
  254.                 // the player wants to try again
  255.                 if (collisionWithObstacle && cki.Key == ConsoleKey.R)
  256.                 {
  257.                     // handle an event in the game. The infinite loop has been left
  258.                     collisionWithObstacle = false;
  259.                     // new snake with 5 segments
  260.                     snake.Clear();
  261.                     for (var i = 0; i < 5; i++)
  262.                     {
  263.                         snake.Enqueue(new Point(0, i));
  264.                     }
  265.  
  266.                     delay = 150.0;
  267.                 }
  268.  
  269.             } while (cki.Key != ConsoleKey.Escape);
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement