示例#1
0
 public void HitProve(Walls walls)
 {
     if (walls.isHit(pList.Last()))
     {
         OnHit(new SnakeEventArgs("Игра окончена!"));
     }
 }
示例#2
0
文件: Program.cs 项目: Mee7891/Snake
        static void Main(string[] args)
        {
            //подготовка "площадки"
            Console.SetWindowSize(xM, yM);
            Console.SetBufferSize(xM, yM);

            Walls walls = new Walls(xM, yM);

            walls.setDrawEventHandler(DrawPoint);
            walls.Draw();
            ///////////////////////////////////

            //Создадим змейку
            Snake snake = new Snake(2, 10, 4, Direction.Right);

            snake.setEventHandler(DrawPoint);
            snake.Draw();
            ///////////////////////////////////////

            //Создадим "создатель еды"
            FoodCreator foodCreator = new FoodCreator(xM, yM, '$');

            foodCreator.setEventHandler(DrawPoint);
            foodCreator.CreateFood();

            snake.Eaten += foodCreator.CreateFood; //сигнал от змейки, что она поела, надо создавать еще еду
            snake.Hit   += SnakeHit;               //сигнал от змейки, что она ударилась

            //для красоты
            Console.CursorVisible = false;
            Console.SetCursorPosition(40, 10);

            while (!gameOver)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey(true); //без true рамка будет съедаться змейкой
                    if (keyInfo.Key == ConsoleKey.Escape)
                    {
                        break;
                    }

                    snake.KeyHandleConsole(keyInfo.Key);
                }

                Thread.Sleep(300);
                snake.Move();
                snake.Eat(foodCreator.food);
                snake.HitProve(walls);
            }


            Console.ReadKey();
        }
示例#3
0
        static Timer time;              // Час гри, він зупиняється якщо змійка зіштовхнулася з стіною або з своїм хвостом

        static void Main()
        {
            Console.SetWindowSize(x + 1, y + 1);      // метод, що встановлює ширину і висоту консолі, взяті з static readonly int x та int y
            Console.SetBufferSize(x + 1, y + 1);      // мутод, що встановлює ширину і висоту буферної області(обл де і буде наш текст), взято х+1 та у+1, щоб не виникла помилка типу ArgumentOutOfRangeException
            Console.CursorVisible = false;            // курсор не видно

            walls = new Walls(x, y, '#');             // стінки задаються таким символом: #
            snake = new Snake(x / 2, y / 2, 4);       // Координати змійки та її початкова довжина

            foodFactory = new FoodFactory(x, y, '@'); // координати їжі для змійки та її символ типу char
            foodFactory.CreateFood();                 // створення їжі

            time = new Timer(Loop, null, 0, 200);

            while (true)                  // безкінечний цикл для зчитування клавіш на клавіатурі
            {
                if (Console.KeyAvailable) // якщо нажимаємо клавішу, то передаємо її в метод зч клавіш
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.Rotation(key.Key);
                }
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(80, 30);

            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point p = new Point(4, 5, '*');

            Snakee snake = new Snakee(p, 4, Direction.RIGHT);

            snake.Draw();

            FoodCreator foodCreator = new FoodCreator(80, 25, '$');
            Point       food        = foodCreator.CreateFood();

            food.Draw();

            Params settings = new Params();
            Sound  sound    = new Sound(settings.GetResourcesFolder());

            sound.Play();

            Sound sound1 = new Sound(settings.GetResourcesFolder());

            Sound sound2 = new Sound(settings.GetResourcesFolder());

            int i = 0;

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    Gameover gameover = new Gameover();
                    gameover.Game();
                    sound2.PlayDied();

                    Console.Write("Ente your name: ");
                    String nimi = (Console.ReadLine());

                    Console.WriteLine(nimi + " scored " + i + " Points");

                    string answer = nimi + ": " + i + " Points";

                    StreamWriter to_file = new StreamWriter(@"C:\Users\Game\source\repos\Snake1\Snake1\resources\results.txt", true);
                    to_file.WriteLine(answer);
                    to_file.Close();
                    Console.ForegroundColor = ConsoleColor.White;

                    StreamReader from_file = new StreamReader(@"C:\Users\Game\source\repos\Snake1\Snake1\resources\results.txt");
                    string       text      = from_file.ReadToEnd();
                    Console.WriteLine(text);
                    from_file.Close();
                    break;
                }
                Score score = new Score();
                score.Scoree(i);

                if (snake.Eat(food))
                {
                    i++;

                    food = foodCreator.CreateFood();
                    food.Draw();
                    sound1.PlayEat();
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }
        }