Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Snake
- {
- class Program
- {
- static Point apple = null;
- static bool AppleE = false;
- class Snake
- {
- public Point head;
- public List<Point> points;
- public Snake(Point start)
- {
- points = new List<Point> { start };
- }
- public void Move(Point point)
- {
- points.Add(point);
- Console.SetCursorPosition(point.x, point.y);
- Console.Write("#");
- if (point == apple)
- {
- AppleE = false;
- }
- else
- {
- Console.SetCursorPosition(points[0].x, points[0].y);
- Console.Write(" ");
- points.RemoveAt(0);
- }
- }
- }
- class Point
- {
- public int x, y;
- public Point()
- {
- y = size / 2;
- x = size;
- }
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- static public bool operator ==(Point left, Point right)
- {
- if (left is null || right is null)
- {
- return false;
- }
- return ((left.x == right.x) && (left.y == right.y));
- }
- static public bool operator !=(Point left, Point right)
- {
- if (left is null || right is null)
- {
- return false;
- }
- return ((left.x != right.x) || (left.y != right.y));
- }
- }
- static int size = 20;
- static void Map()
- {
- for (int i = 0; i <= size + 1; i++)
- {
- Console.SetCursorPosition(i * 2, 0); //Верхняя горизонталь
- Console.Write("X");
- Console.SetCursorPosition(0, i); //Левая горизонталь
- Console.Write("X");
- Console.SetCursorPosition(i * 2, size + 1); //Нижняя горизонталь
- Console.Write("X");
- Console.SetCursorPosition((size + 1) * 2, i); //Правая горизонталь
- Console.Write("X");
- }
- }
- static void GameOver()
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Game Over!");
- }
- static bool Cont(Snake player)
- {
- ConsoleKeyInfo button;
- Point HeadPos = new Point(player.points[0].x, player.points[0].y);
- button = Console.ReadKey(true);
- switch (button.Key)
- {
- case ConsoleKey.W:
- if (HeadPos.y == 1)
- {
- GameOver();
- return false;
- }
- else
- {
- HeadPos.y--;
- }
- break;
- case ConsoleKey.A:
- if (HeadPos.x == 1)
- {
- GameOver();
- return false;
- }
- else
- {
- HeadPos.x -= 2;
- }
- break;
- case ConsoleKey.S:
- if (HeadPos.y == size)
- {
- GameOver();
- return false;
- }
- else
- {
- HeadPos.y++;
- }
- break;
- case ConsoleKey.D:
- if (HeadPos.x == size * 2 + 1)
- {
- GameOver();
- return false;
- }
- else
- {
- HeadPos.x += 2;
- }
- break;
- }
- player.Move(HeadPos);
- return true;
- }
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- Map();
- Random r = new Random();
- Snake snake = new Snake(new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1)));
- Console.SetCursorPosition(snake.points[0].x, snake.points[0].y);
- Console.Write("#");
- while (Cont(snake))
- {
- if (snake.points[0] == apple)
- {
- AppleE = false;
- }
- if (!AppleE)
- {
- apple = new Point(r.Next(1, size + 1) * 2 - 1, r.Next(1, size + 1));
- Console.SetCursorPosition(apple.x, apple.y);
- Console.Write("*");
- AppleE = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement