Example #1
0
        static void Main(string[] args)
        {
            Random rnd = new Random();

            string[] directions = new string[] { "w", "a", "s", "d" };
            string   direction_for_ghost, direction_for_smartghost;

            Console.Write("Введите размер поля М х М: ");
            int   m     = int.Parse(Console.ReadLine());
            Field field = new Field(m);


            Pacman     pacman     = field.FindPacman;
            Ghost      ghost      = field.FindGhost;
            SmartGhost smartghost = field.FindSmartGhost;
            string     command    = "ok";

            while (true)
            {
                field.MyPrint();
                while (command != "w" && command != "a" && command != "s" && command != "d" &&
                       command != "check" && command != "clear" && command != "exit" && !command.Contains("set"))
                {
                    Console.Write(
                        "Команды для Пакмана: W — вверх, A — вправо, S — вниз, D — влево" +
                        "\nЧтобы окончить игру, введи exit" +
                        "\nЧтобы узнать счет, введи check" +
                        "\nЧтобы очистить консоль, введи clear" +
                        "\nЧтобы изменить скорость пакмана, введи set <число> (доступно только 1 или 2): "
                        );
                    command = Console.ReadLine().ToLower();
                }
                if (command == "exit")
                {
                    break;
                }
                else if (command == "check")
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine("Ваш счет: {0}", pacman.Score);
                    Console.BackgroundColor = ConsoleColor.Black;
                }
                else if (command == "clear")
                {
                    Console.Clear();
                }
                else if (command.Contains("set"))
                {
                    int get_speed = int.Parse(command.Split(' ')[1]);
                    pacman.V = get_speed;
                }
                else if (command == "w" || command != "a" || command != "s" || command != "d")
                {
                    direction_for_ghost = directions[rnd.Next(0, 4)];
                    ghost.Move(direction_for_ghost, field);
                    direction_for_smartghost = directions[rnd.Next(0, 4)];
                    smartghost.Move(direction_for_ghost, field);
                    pacman.Move(command, field);
                    if (pacman.Score == 500)
                    {
                        Console.WriteLine("Вы выиграли! ");
                        break;
                    }
                }
                else
                {
                    continue;
                }
                command = "";
            }
        }
Example #2
0
        public void GenerateField()
        {
            int x, y;

            // Заполнение пустыми объектами
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    matrix[i, j] = new Empty(i, j);
                }
            }
            // Cтавлю стены
            for (int i = 0; i < 15; i++)
            {
                do
                {
                    x = rnd.Next(0, matrix.GetLength(0));
                    y = rnd.Next(0, matrix.GetLength(0));
                }while (matrix[x, y].ClassName != "Empty");
                matrix[x, y] = new Wall(x, y);
            }

            // Ставлю еду
            for (int i = 0; i < 10; i++)
            {
                do
                {
                    x = rnd.Next(0, matrix.GetLength(0));
                    y = rnd.Next(0, matrix.GetLength(0));
                }while (matrix[x, y].ClassName != "Empty");
                matrix[x, y] = new Food(x, y);
            }
            // Ставлю одну вишенку
            do
            {
                x = rnd.Next(0, matrix.GetLength(0));
                y = rnd.Next(0, matrix.GetLength(0));
            }while (matrix[x, y].ClassName != "Empty");
            matrix[x, y] = new Cherry(x, y);

            // Место для Pacman
            do
            {
                x = rnd.Next(0, matrix.GetLength(0));
                y = rnd.Next(0, matrix.GetLength(0));
            }while (matrix[x, y].ClassName == "Empty");
            matrix[x, y] = new Pacman(x, y, 1);

            // Место для Ghost
            do
            {
                x = rnd.Next(0, matrix.GetLength(0));
                y = rnd.Next(0, matrix.GetLength(0));
            }while (matrix[x, y].ClassName == "Empty");
            matrix[x, y] = new Ghost(x, y, 1);

            // Место для SmartGhost
            do
            {
                x = rnd.Next(0, matrix.GetLength(0));
                y = rnd.Next(0, matrix.GetLength(0));
            }while (matrix[x, y].ClassName == "Empty");
            matrix[x, y] = new SmartGhost(x, y, 1);
        }