Advertisement
n4wn4w

SNAKE

May 19th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.02 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Linq;
  6.  
  7. namespace TeamworkProject
  8. {
  9.     struct Position
  10.     {
  11.         public int row;
  12.         public int col;
  13.  
  14.         public Position(int x, int y)
  15.         {
  16.             this.row = x;
  17.             this.col = y;
  18.         }
  19.     }
  20.  
  21.     class Snake
  22.     {
  23.         static void Main()
  24.         {
  25.             Console.Title = "SNAKE - Team Aratoht";
  26.             Console.CursorVisible = false;
  27.             Console.WindowHeight = 30;
  28.             Console.WindowWidth = 60;
  29.             Console.BufferHeight = Console.WindowHeight;
  30.             Console.BufferWidth = Console.WindowWidth;
  31.             int negativePoints = 0;
  32.  
  33.             //print title
  34.  
  35.             Console.SetCursorPosition(0, 0);
  36.             Console.WriteLine(@"     _____  _   _            _  __ ______ ");
  37.             Console.WriteLine(@"    / ____|| \ | |    /\    | |/ /|  ____|");
  38.             Console.WriteLine(@"   | (___  |  \| |   /  \   | ' / | |__   ");
  39.             Console.WriteLine(@"    \___ \ | . ` |  / /\ \  |  <  |  __|  ");
  40.             Console.WriteLine(@"    ____) || |\  | / ____ \ | . \ | |____ ");
  41.             Console.WriteLine(@"   |_____/ |_| \_|/_/    \_\|_|\_\|______|");
  42.  
  43.             Console.SetCursorPosition(5, 18);
  44.             Console.Write("Use up/down arrow and press enter to make your choice.");
  45.             Console.SetCursorPosition(15, 21);
  46.             Console.Write("Exit");
  47.             Console.SetCursorPosition(15, 20);
  48.             Console.ForegroundColor = ConsoleColor.Green;
  49.             Console.Write("New Game");
  50.             Console.ResetColor();
  51.  
  52.             //Main menu
  53.  
  54.             bool inMenu = true;
  55.  
  56.             while (inMenu)
  57.             {
  58.                 while (Console.KeyAvailable)
  59.                 {
  60.                     ConsoleKeyInfo input = Console.ReadKey(true);
  61.  
  62.                     switch (input.Key)
  63.                     {
  64.                         case ConsoleKey.UpArrow:
  65.                         case ConsoleKey.DownArrow:
  66.                             {
  67.                                 if (Console.CursorTop == 20)
  68.                                 {
  69.                                     Console.SetCursorPosition(15, 20);
  70.                                     Console.Write("New Game");
  71.                                     Console.SetCursorPosition(15, 21);
  72.                                     Console.ForegroundColor = ConsoleColor.Green;
  73.                                     Console.Write("Exit");
  74.                                     Console.ResetColor();
  75.                                 }
  76.                                 else
  77.                                 {
  78.                                     Console.SetCursorPosition(15, 21);
  79.                                     Console.Write("Exit");
  80.                                     Console.SetCursorPosition(15, 20);
  81.                                     Console.ForegroundColor = ConsoleColor.Green;
  82.                                     Console.Write("New Game");
  83.                                     Console.ResetColor();
  84.                                 }
  85.                             }
  86.                             break;
  87.                         case ConsoleKey.Enter:
  88.                             {
  89.                                 if (Console.CursorTop == 20)
  90.                                 {
  91.                                     Console.Clear();
  92.                                     inMenu = false;
  93.                                 }
  94.                                 else
  95.                                 {
  96.                                     return;
  97.                                 }
  98.                             }
  99.                             break;
  100.                         default: break;
  101.                     }
  102.                 }
  103.             }
  104.  
  105.             //Draws board
  106.             Position boardStart = new Position(4, 8);
  107.             Position boardEnd = new Position(25, 45);
  108.  
  109.             for (int i = boardStart.row; i < boardEnd.row; i++)
  110.             {
  111.                 Console.SetCursorPosition(8, i);
  112.                 for (int j = boardStart.col; j < boardEnd.col; j++)
  113.                 {
  114.                     if (i == 4 || i == 24)
  115.                     {
  116.                         Console.Write("=");
  117.                     }
  118.                     else if (j == 8 || j == 44)
  119.                     {
  120.                         Console.Write("|");
  121.                     }
  122.                     else
  123.                     {
  124.                         Console.Write(" ");
  125.                     }
  126.                 }
  127.             }
  128.  
  129.             //Random Generator
  130.             Random randomNumberGenerator = new Random();
  131.  
  132.             //starting snake size
  133.             Queue<Position> snake = new Queue<Position>();
  134.  
  135.             for (int i = 0; i < 10; i++)
  136.             {
  137.                 snake.Enqueue(new Position(boardStart.row + 1, boardStart.col + 1));
  138.             }
  139.  
  140.             //array to hold the directions
  141.             int direction = 0;
  142.  
  143.             Position[] directions = new Position[]
  144.             {
  145.             new Position(0, 1), //right
  146.             new Position(0, -1),//left
  147.             new Position(1, 0), //down
  148.             new Position(-1, 0) //up
  149.             };
  150.  
  151.             //Food - random coordinates for first food
  152.             Position food;
  153.             int foodPushTime;
  154.             do
  155.             {
  156.                 food = new Position(randomNumberGenerator.Next(boardStart.row + 1, boardEnd.row - 1), randomNumberGenerator.Next(boardStart.col + 1, boardEnd.col - 1));
  157.                 foodPushTime = Environment.TickCount;
  158.             }
  159.             while (snake.Contains(food));
  160.  
  161.             //Print food
  162.             Console.ForegroundColor = ConsoleColor.Green;
  163.             Console.SetCursorPosition(food.col, food.row);
  164.             Console.Write("$");
  165.             Console.ResetColor();
  166.  
  167.             //Obstacle
  168.             List<Position> obsts = new List<Position>();
  169.             for (int i = 0; i < randomNumberGenerator.Next(4, 16); i++)//
  170.             {
  171.                 do
  172.                 {
  173.                     obsts.Add(new Position(randomNumberGenerator.Next(boardStart.row + 1, boardEnd.row - 1), randomNumberGenerator.Next(boardStart.col + 1, boardEnd.col - 1)));
  174.                 }
  175.                 while (snake.Contains(obsts[i]) ||
  176.                        (food.row == obsts[i].row && food.col == obsts[i].col));
  177.             }
  178.  
  179.             //Print obstacles
  180.             foreach (Position rock in obsts)
  181.             {
  182.                 Console.ForegroundColor = ConsoleColor.Red;
  183.                 Console.SetCursorPosition(rock.col, rock.row);
  184.                 Console.Write("@");
  185.                 Console.ResetColor();
  186.             }
  187.  
  188.             //Main game loop
  189.             while (true)
  190.             {
  191.                 negativePoints++;
  192.                 //user input check
  193.                 while (Console.KeyAvailable)
  194.                 {
  195.                     ConsoleKeyInfo input = Console.ReadKey(true);
  196.  
  197.                     switch (input.Key)
  198.                     {
  199.                         case ConsoleKey.UpArrow: direction = 3; break;
  200.                         case ConsoleKey.DownArrow: direction = 2; break;
  201.                         case ConsoleKey.LeftArrow: direction = 1; break;
  202.                         case ConsoleKey.RightArrow: direction = 0; break;
  203.                         case ConsoleKey.Escape: return;
  204.                         default: break;
  205.                     }
  206.                 }
  207.                 //where the next element will appear
  208.                 Position snakeHead = snake.Last();
  209.                 Position newSnakeHead = new Position(snakeHead.row + directions[direction].row, snakeHead.col + directions[direction].col);
  210.  
  211.                
  212.                 //logic for snake eating itself - game over
  213.                 //if (snake.Contains(newSnakeHead))
  214.                 //{
  215.                 //    Console.SetCursorPosition(0, 1);
  216.                 //    Console.WriteLine("snake passed through itself.game over.");
  217.                 //    return;
  218.                 //}
  219.  
  220.  
  221.  
  222.  
  223.  
  224.                 //logic for passing through walls
  225.  
  226.                 if (newSnakeHead.col == boardEnd.col - 1)
  227.                 {
  228.                     newSnakeHead.col = boardStart.col + 1;
  229.                 }
  230.                 if (newSnakeHead.col == boardStart.col)
  231.                 {
  232.                     newSnakeHead.col = boardEnd.col - 2;
  233.                 }
  234.                 if (newSnakeHead.row == boardEnd.row - 1)
  235.                 {
  236.                     newSnakeHead.row = boardStart.row + 1;
  237.                 }
  238.                 if (newSnakeHead.row == boardStart.row)
  239.                 {
  240.                     newSnakeHead.row = boardEnd.row - 2;
  241.                 }
  242.  
  243.  
  244.  
  245.                 if (snake.Contains(newSnakeHead)
  246.                     || obsts.Contains(newSnakeHead))
  247.                    
  248.                 {
  249.                     Console.SetCursorPosition(0, 0);
  250.                     Console.ForegroundColor = ConsoleColor.Red;
  251.                     Console.WriteLine("Game over!");
  252.                     int userPoints =
  253.                        
  254.                         negativePoints;
  255.                     //if (userPoints < 0) userPoints = 0;
  256.                     userPoints = Math.Max(userPoints, 0);
  257.                     Console.WriteLine("Your points are: {0}",
  258.                         userPoints);
  259.                     Console.ReadLine();
  260.                     return;
  261.                 }
  262.  
  263.  
  264.  
  265.  
  266.                 //check if snake crashed into an obstacle
  267.  
  268.                 if (obsts.Contains(newSnakeHead))
  269.                 {
  270.                     Console.SetCursorPosition(0, 1);
  271.                     Console.WriteLine("dead snake is dead");
  272.                     return;
  273.                 }
  274.  
  275.                 //eat food, spawn new food
  276.  
  277.                 if (newSnakeHead.col == food.col &&
  278.                     newSnakeHead.row == food.row)
  279.                 {
  280.                     // feeding the snake
  281.                     do
  282.                     {
  283.                         food = new Position(randomNumberGenerator.Next(boardStart.row + 1, boardEnd.row - 1), randomNumberGenerator.Next(boardStart.col + 1, boardEnd.col - 1));
  284.                     }
  285.                     while (snake.Contains(food) ||
  286.                         obsts.Contains(food));
  287.  
  288.                     Console.SetCursorPosition(food.col, food.row);
  289.                     Console.ForegroundColor = ConsoleColor.Green;
  290.                     Console.Write("$");
  291.                     Console.ResetColor();
  292.  
  293.                 }
  294.  
  295.  
  296.                 snake.Enqueue(newSnakeHead);
  297.  
  298.                 //snake animation draw the new element, delete last element
  299.  
  300.                 Console.SetCursorPosition(newSnakeHead.col, newSnakeHead.row);
  301.                 Console.WriteLine("*");
  302.  
  303.                 Position snakeTail = snake.Dequeue();
  304.                 Console.SetCursorPosition(snakeTail.col, snakeTail.row);
  305.                 Console.Write(" ");
  306.  
  307.                 //game speed
  308.                 Thread.Sleep(150);
  309.             }
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement