コード例 #1
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();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: ilja0303/SnakeGame
        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);
                }
            }
        }