Advertisement
fom1nov

Zmeyka na c#

Jan 21st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace SnakeGame
  7. {
  8.     class Game
  9.     {
  10.         static readonly int x = 80;
  11.         static readonly int y = 26;
  12.  
  13.         static Walls walls;
  14.         static Snake snake;
  15.         static FoodFactory foodFactory;
  16.         static Timer time;
  17.  
  18.         static void Main()
  19.         {
  20.             Console.SetWindowSize(x + 1, y + 1);
  21.             Console.SetBufferSize(x + 1, y + 1);
  22.             Console.CursorVisible = false;
  23.  
  24.             walls = new Walls(x, y, '#');
  25.             snake = new Snake(x / 2, y / 2, 3);
  26.  
  27.             foodFactory = new FoodFactory(x, y, '@');
  28.             foodFactory.CreateFood();
  29.  
  30.             time = new Timer(Loop, null, 0, 200);
  31.  
  32.             while (true)
  33.             {
  34.                 if (Console.KeyAvailable)
  35.                 {
  36.                     ConsoleKeyInfo key = Console.ReadKey();
  37.                     snake.Rotation(key.Key);
  38.                 }
  39.             }
  40.         }// Main()
  41.  
  42.         static void Loop(object obj)
  43.         {
  44.             if (walls.IsHit(snake.GetHead()) || snake.IsHit(snake.GetHead()))
  45.             {
  46.                 time.Change(0, Timeout.Infinite);
  47.             }
  48.             else if (snake.Eat(foodFactory.food))
  49.             {
  50.                 foodFactory.CreateFood();
  51.             }
  52.             else
  53.             {
  54.                 snake.Move();
  55.             }
  56.         }// Loop()
  57.     }// class Game
  58.  
  59.     struct Point
  60.     {
  61.         public int x { get; set; }
  62.         public int y { get; set; }
  63.         public char ch { get; set; }
  64.        
  65.         public static implicit operator Point((int, int, char) value) =>
  66.               new Point {x = value.Item1, y = value.Item2, ch = value.Item3};
  67.  
  68.         public static bool operator ==(Point a, Point b) =>
  69.                 (a.x == b.x && a.y == b.y) ? true : false;
  70.         public static bool operator !=(Point a, Point b) =>
  71.                 (a.x != b.x || a.y != b.y) ? true : false;
  72.  
  73.         public void Draw()
  74.         {
  75.             DrawPoint(ch);
  76.         }
  77.         public void Clear()
  78.         {
  79.             DrawPoint(' ');
  80.         }
  81.  
  82.         private void DrawPoint(char _ch)
  83.         {
  84.             Console.SetCursorPosition(x, y);
  85.             Console.Write(_ch);
  86.         }
  87.     }
  88.  
  89.     class Walls
  90.     {
  91.         private char ch;
  92.         private List<Point> wall = new List<Point>();
  93.  
  94.         public Walls(int x, int y, char ch)
  95.         {
  96.             this.ch = ch;
  97.  
  98.             DrawHorizontal(x, 0);
  99.             DrawHorizontal(x, y);
  100.             DrawVertical(0, y);
  101.             DrawVertical(x, y);
  102.         }
  103.  
  104.         private void DrawHorizontal(int x, int y)
  105.         {
  106.             for (int i = 0; i < x; i++)
  107.             {
  108.                 Point p = (i, y, ch);
  109.                 p.Draw();
  110.                 wall.Add(p);
  111.             }
  112.         }
  113.  
  114.         private void DrawVertical(int x, int y)
  115.         {
  116.             for (int i = 0; i < y; i++)
  117.             {
  118.                 Point p = (x, i, ch);
  119.                 p.Draw();
  120.                 wall.Add(p);
  121.             }
  122.         }
  123.  
  124.         public bool IsHit(Point p)
  125.         {
  126.             foreach (var w in wall)
  127.             {
  128.                 if (p == w)
  129.                 {
  130.                     return true;
  131.                 }
  132.             }
  133.             return false;
  134.         }
  135.     }// class Walls
  136.  
  137.     enum Direction
  138.     {
  139.         LEFT,
  140.         RIGHT,
  141.         UP,
  142.         DOWN
  143.     }
  144.  
  145.     class Snake
  146.     {
  147.         private List<Point> snake;
  148.  
  149.         private Direction direction;
  150.         private int step = 1;
  151.         private Point tail;
  152.         private Point head;
  153.  
  154.         bool rotate = true;
  155.  
  156.         public Snake(int x, int y, int length)
  157.         {
  158.             direction = Direction.RIGHT;
  159.  
  160.             snake = new List<Point>();
  161.             for (int i = x - length; i < x; i++)
  162.             {
  163.                 Point p = (i, y, '*');
  164.                 snake.Add(p);
  165.  
  166.                 p.Draw();
  167.             }
  168.         }
  169.  
  170.         public Point GetHead() => snake.Last();
  171.  
  172.         public void Move()
  173.         {
  174.             head = GetNextPoint();
  175.             snake.Add(head);
  176.  
  177.             tail = snake.First();
  178.             snake.Remove(tail);
  179.  
  180.             tail.Clear();
  181.             head.Draw();
  182.  
  183.             rotate = true;
  184.         }
  185.  
  186.         public bool Eat(Point p)
  187.         {
  188.             head = GetNextPoint();
  189.             if (head == p)
  190.             {
  191.                 snake.Add(head);
  192.                 head.Draw();
  193.                 return true;
  194.             }
  195.             return false;
  196.         }
  197.  
  198.     public Point GetNextPoint ()
  199.     {
  200.         Point p = GetHead ();
  201.  
  202.         switch (direction)
  203.         {
  204.         case Direction.LEFT:
  205.             p.x -= step;
  206.             break;
  207.         case Direction.RIGHT:
  208.             p.x += step;
  209.             break;
  210.         case Direction.UP:
  211.             p.y -= step;
  212.             break;
  213.         case Direction.DOWN:
  214.             p.y += step;
  215.             break;
  216.         }
  217.         return p;
  218.     }
  219.  
  220.     public void Rotation (ConsoleKey key)
  221.     {
  222.         if (rotate)
  223.         {
  224.             switch (direction)
  225.             {
  226.             case Direction.LEFT:
  227.             case Direction.RIGHT:
  228.                 if (key == ConsoleKey.DownArrow)
  229.                     direction = Direction.DOWN;
  230.                 else if (key == ConsoleKey.UpArrow)
  231.                     direction = Direction.UP;
  232.                 break;
  233.             case Direction.UP:
  234.             case Direction.DOWN:
  235.                 if (key == ConsoleKey.LeftArrow)
  236.                     direction = Direction.LEFT;
  237.                 else if (key == ConsoleKey.RightArrow)
  238.                     direction = Direction.RIGHT;
  239.                 break;
  240.             }
  241.             rotate = false;
  242.         }
  243.  
  244.     }
  245.  
  246.         public bool IsHit(Point p)
  247.         {
  248.             for (int i = snake.Count - 2; i > 0; i--)
  249.             {
  250.                 if (snake[i] == p)
  251.                 {
  252.                     return true;
  253.                 }
  254.             }
  255.             return false;
  256.         }
  257.     }//class Snake
  258.  
  259.     class FoodFactory
  260.     {
  261.         int x;
  262.         int y;
  263.         char ch;
  264.         public Point food { get; private set; }
  265.  
  266.         Random random = new Random();
  267.  
  268.         public FoodFactory(int x, int y, char ch)
  269.         {
  270.             this.x = x;
  271.             this.y = y;
  272.             this.ch = ch;
  273.         }
  274.  
  275.         public void CreateFood()
  276.         {
  277.             food = (random.Next(2, x - 2), random.Next(2, y - 2), ch);
  278.             food.Draw();
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement