Advertisement
GoDVeR

Untitled

Feb 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.96 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 Snake
  8. {
  9.     class Program
  10.     {
  11.         static Point apple = null;
  12.         static bool AppleE = false;
  13.  
  14.         enum Directions
  15.         {
  16.             Up,
  17.             Right,
  18.             Down,
  19.             Left
  20.         }
  21.         class Snake
  22.         {
  23.             public List<Point> points;
  24.             public Point head;
  25.             public Directions direction;
  26.  
  27.             public Snake(Point start)
  28.             {
  29.                 points = new List<Point> {start};
  30.                 head = start;
  31.                 direction = Directions.Down;
  32.             }
  33.  
  34.             public void Move(Point point)
  35.             {
  36.  
  37.                 points.Add(point);
  38.                 head = point;
  39.                 Console.SetCursorPosition(point.x, point.y);
  40.                 Console.Write("#");
  41.  
  42.                 if (point == apple)
  43.                 {
  44.                     AppleE = false;
  45.                 }
  46.                 else
  47.                 {
  48.                     Console.SetCursorPosition(points[0].x, points[0].y);
  49.  
  50.                     Console.Write(" ");
  51.                     points.RemoveAt(0);
  52.                 }
  53.             }
  54.         }
  55.         class Point : IEquatable<Point>
  56.         {
  57.             public int x, y;
  58.  
  59.             public Point()
  60.             {
  61.                 y = size / 2;
  62.                 x = size;
  63.             }
  64.  
  65.             public Point(int x, int y)
  66.             {
  67.                 this.x = x;
  68.                 this.y = y;
  69.             }
  70.  
  71.             public bool Equals(Point other)
  72.             {
  73.                 if (other == null)
  74.                 {
  75.                     return false;
  76.                 }
  77.                 return x == other.x && y == other.y;
  78.             }
  79.  
  80.             public override bool Equals(object obj)
  81.             {
  82.                 if (obj == null)
  83.                 {
  84.                     return false;
  85.                 }
  86.  
  87.                 Point other = obj as Point;
  88.                 return Equals(other);              
  89.             }
  90.  
  91.             static public bool operator ==(Point left, Point right)
  92.             {
  93.                 if (left is null || right is null)
  94.                 {
  95.                     return false;
  96.                 }
  97.                 return ((left.x == right.x) && (left.y == right.y));
  98.             }
  99.             static public bool operator !=(Point left, Point right)
  100.             {
  101.                 if (left is null || right is null)
  102.                 {
  103.                     return false;
  104.                 }
  105.                 return ((left.x != right.x) || (left.y != right.y));
  106.             }
  107.  
  108.         }
  109.         static int size = 20;
  110.         static void Map()
  111.         {
  112.             for (int i = 0; i <= size + 1; i++)
  113.             {
  114.                 Console.SetCursorPosition(i * 2, 0); //Верхняя горизонталь
  115.                 Console.Write("X");
  116.                 Console.SetCursorPosition(0, i); //Левая горизонталь
  117.                 Console.Write("X");
  118.                 Console.SetCursorPosition(i * 2, size + 1); //Нижняя горизонталь
  119.                 Console.Write("X");
  120.                 Console.SetCursorPosition((size + 1) * 2, i); //Правая горизонталь
  121.                 Console.Write("X");
  122.             }
  123.         }
  124.         static void GameOver()
  125.         {
  126.             Console.Clear();
  127.             Console.ForegroundColor = ConsoleColor.Red;
  128.             Console.WriteLine("Game Over!");
  129.         }
  130.         static bool Cont(Snake player)
  131.         {
  132.             ConsoleKeyInfo button;
  133.  
  134.             Point HeadPos = new Point(player.head.x, player.head.y);
  135.             button = Console.ReadKey(true);
  136.             switch (button.Key)
  137.             {
  138.                 case ConsoleKey.W:
  139.                     if(player.direction == Directions.Down)
  140.                     {
  141.                         return true;
  142.                     }
  143.                     if (HeadPos.y == 1)
  144.                     {
  145.                         GameOver();
  146.                         return false;
  147.                     }
  148.                     else
  149.                     {
  150.                         HeadPos.y--;
  151.                         player.direction = Directions.Up;
  152.                     }
  153.                     break;
  154.                 case ConsoleKey.A:
  155.                     if(player.direction == Directions.Right)
  156.                     {
  157.                         return true;
  158.                     }
  159.                     if (HeadPos.x == 1)
  160.                     {
  161.                         GameOver();
  162.                         return false;
  163.                     }
  164.                     else
  165.                     {
  166.                         HeadPos.x -= 2;
  167.                         player.direction = Directions.Left;
  168.                     }
  169.                     break;
  170.                 case ConsoleKey.S:
  171.                     if(player.direction == Directions.Up)
  172.                     {
  173.                         return true;
  174.                     }
  175.                     if (HeadPos.y == size)
  176.                     {
  177.                         GameOver();
  178.                         return false;
  179.                     }
  180.                     else
  181.                     {
  182.                         HeadPos.y++;
  183.                         player.direction = Directions.Down;
  184.                     }
  185.                     break;
  186.                 case ConsoleKey.D:
  187.                     if (player.direction == Directions.Left)
  188.                     {
  189.                         return true;
  190.                     }
  191.                     if (HeadPos.x == size * 2 + 1)
  192.                     {
  193.                         GameOver();
  194.                         return false;
  195.                     }
  196.                     else
  197.                     {
  198.                         HeadPos.x += 2;
  199.                         player.direction = Directions.Right;
  200.                     }
  201.                     break;
  202.                 default:
  203.                     return true;
  204.             }
  205.             player.Move(HeadPos);
  206.  
  207.             return true;
  208.         }
  209.         static void Main(string[] args)
  210.         {
  211.             Console.CursorVisible = false;
  212.             Map();
  213.  
  214.             Random r = new Random();
  215.             Snake snake = new Snake(new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1)));
  216.             Console.SetCursorPosition(snake.points[0].x, snake.points[0].y);
  217.             Console.Write("#");
  218.  
  219.            
  220.            
  221.  
  222.             while (Cont(snake))
  223.             {
  224.                 if (!AppleE)
  225.                 {
  226.                     do
  227.                     {
  228.                         apple = new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1));
  229.                     } while (snake.points.Contains(apple));
  230.                      Console.SetCursorPosition(apple.x, apple.y);
  231.                     Console.Write("*");
  232.                     AppleE = true;
  233.                 }
  234.             }
  235.         }
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement