Advertisement
Guest User

змейка

a guest
Dec 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 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 ConsoleApplication25
  8. {
  9.     class Program
  10.     {
  11.         class snake
  12.         {
  13.             public List<Point> points;
  14.             public int lenght;
  15.             Point head;
  16.             public void move(Point point)
  17.             {
  18.                 points.Insert(0, point);
  19.                 points.RemoveAt(points.Count - 1);
  20.                 Point haed;
  21.             }
  22.             public void Rost(Point point)
  23.             {
  24.                 points.Insert(0, point);
  25.                 Point haed;
  26.             }
  27.  
  28.             public snake(Point point)
  29.             {
  30.                 points.Add(point);
  31.             }
  32.         }
  33.  
  34.         class Point
  35.         {
  36.             public int x;
  37.             public int y;
  38.  
  39.  
  40.             public Point()
  41.             {
  42.                 x = 11;
  43.                 y = 11;
  44.             }
  45.  
  46.             public Point(int x, int y)
  47.             {
  48.                 this.x = x;
  49.                 this.y = y;
  50.             }
  51.  
  52.             public static bool operator == (Point p1,Point p2)
  53.             {
  54.                 return (p1.x == p2.x) && (p1.y == p2.y);
  55.             }
  56.             public static bool operator !=(Point p1, Point p2)
  57.             {
  58.                 return (p1.x != p2.x) || (p1.y != p2.y);
  59.             }
  60.  
  61.         }
  62.  
  63.         static void pole(int size)
  64.         {
  65.  
  66.             for (int i = 0; i < size + 2; i++)
  67.             {
  68.                 Console.SetCursorPosition(2 * i, 0); //вг
  69.                 Console.Write("#");
  70.                 Console.SetCursorPosition((size + 1) * 2, i);//пв
  71.                 Console.Write("#");
  72.                 Console.SetCursorPosition(2 * i, size + 1);//нг  
  73.                 Console.Write("#");
  74.                 Console.SetCursorPosition(0, i);//лв
  75.                 Console.Write("#");
  76.             }
  77.  
  78.         }
  79.  
  80.  
  81.         static bool CharacterControl(ref Point point, int size, ref bool isEat, Point apple)
  82.         {
  83.             ConsoleKeyInfo button;
  84.             button = Console.ReadKey(true);
  85.  
  86.             if (button.Key == ConsoleKey.Escape)
  87.             {
  88.                 return true;
  89.             }
  90.  
  91.  
  92.  
  93.             Console.SetCursorPosition(point.x, point.y);
  94.             Console.Write(" ");
  95.  
  96.             switch (button.Key)
  97.             {
  98.                 case ConsoleKey.W:
  99.                     point.y = (point.y - 1 == 0) ? size : point.y - 1;
  100.                     break;
  101.  
  102.                 case ConsoleKey.A:
  103.                     point.x = (point.x - 2 < 0) ? size * 2 + 1 : point.x - 2;
  104.                     break;
  105.  
  106.  
  107.                 case ConsoleKey.S:
  108.                     point.y = (point.y + 1 == size + 1) ? 1 : point.y + 1;
  109.                     break;
  110.                 case ConsoleKey.D:
  111.                     point.x = (point.x + 2 > size * 2 + 1) ? 1 : point.x + 2;
  112.                     break;
  113.  
  114.             }
  115.  
  116.             if (point == apple)
  117.             {
  118.                 isEat = true;
  119.             }
  120.  
  121.             Console.SetCursorPosition(point.x, point.y);
  122.             Console.Write("0");
  123.  
  124.             return false;
  125.  
  126.         }
  127.  
  128.  
  129.  
  130.         static void Main(string[] args)
  131.         {
  132.             Console.CursorVisible = false;
  133.             snake snake = new snake(new Point());
  134.             Point apple= new Point();
  135.             Random r = new Random();
  136.             Point point = new Point();
  137.             Console.SetCursorPosition(point.x, point.y);
  138.             Console.Write("0");
  139.             int size = 20;
  140.             bool isEat = true;
  141.             pole(size);
  142.  
  143.            
  144.             bool exit;
  145.  
  146.  
  147.             do
  148.             {
  149.                 exit = CharacterControl( ref point, size , ref isEat, apple);
  150.                 if (isEat)
  151.                 {
  152.  
  153.  
  154.                     do
  155.                     {
  156.                         apple.x = r.Next(0, size + 1) * 2 + 1;
  157.                         apple.y = r.Next(1, size + 1);
  158.  
  159.                     } while (point == apple);
  160.                     Console.SetCursorPosition(apple.x, apple.y);
  161.                     Console.Write("@");
  162.                     isEat = false;
  163.                 }
  164.  
  165.  
  166.             } while (!exit);
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.         }
  175.     }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement