Esempio n. 1
0
        static void Main(string[] args)
        {
            int   score = 0;
            Walls walls = new Walls(119, 29);

            walls.Draw();
            Point snakeTail = new Point(5, 5, 's');
            Snake snake     = new Snake(snakeTail, 10, Direction.RIGHT);

            snake.Draw();
            FoodGenerator foodGenerator = new FoodGenerator(119, 29, '$');
            Point         food          = foodGenerator.GenerateFood();

            food.Draw();


            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodGenerator.GenerateFood();
                    food.Draw();


                    Random rnd = new Random();
                    int    num = rnd.Next(1, 3);
                    if (num == 1)
                    {
                        score = score + 2;
                    }
                    else
                    {
                        score--;
                    }
                }
                else
                {
                    snake.Move();
                }


                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKeys(key.Key);
                }

                Thread.Sleep(300);
            }

            string str_score = Convert.ToString(score);

            WriteGameOver(str_score);
            Console.ReadLine();
        }
Esempio n. 2
0
        private static void StartGame()
        {
            Graphics.initGraphics(Console.BufferWidth, Console.BufferHeight);
            Walls walls = new Walls(79, 25, '+');

            walls.Draw();
            Point point = new Point(4, 5, '*');
            Snake snake = new Snake(point, 4, Direction.RIGHT);

            snake.Draw();
            FoodCreator foodCreator = new FoodCreator(Console.BufferWidth, Console.BufferHeight, '$');
            Point       food        = foodCreator.GenerateFood();

            food.Draw();
            int count = 0;

            while (game)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey(true);
                    switch (key.Key)
                    {
                    case ConsoleKey.RightArrow:
                        snake.Turn(Direction.RIGHT);
                        break;

                    case ConsoleKey.LeftArrow:
                        snake.Turn(Direction.LEFT);
                        break;

                    case ConsoleKey.UpArrow:
                        snake.Turn(Direction.UP);
                        break;

                    case ConsoleKey.DownArrow:
                        snake.Turn(Direction.DOWN);
                        break;
                    }
                }
                snake.Move();
                if (snake.isHit(food))
                {
                    snake.Grow();
                    food = foodCreator.GenerateFood();
                    food.Draw();
                    count++;
                }
                if (snake.isHit(walls.GetPoints()) || snake.isDead(snake.GetPoints()))
                {
                    snake.Clear();
                    food.Clear();
                    GameOver(count);
                }
                Thread.Sleep(100);
            }
            Console.ReadKey();
        }
Esempio n. 3
0
        public void MenuSelect()
        {
            Console.Clear();

            // Установка размера окна меню
            int width  = 33;
            int height = 6;

            Console.SetWindowSize(width + 1, height + 1);

            Walls frame = new Walls(width, height, '*');

            // Название игры и пункты меню
            TextItem title = new TextItem(1, 1, "~~~~~~~~~~~~ЗМЕЙКА~~~~~~~~~~~~".PadBoth(31));

            // Передача пунктов в меню
            Menu menu = new Menu(new List <TextItem>
            {
                new TextItem(1, 2, "НАЧАТЬ ИГРУ".PadBoth(31)),
                new TextItem(1, 3, "НАСТРОЙКИ".PadBoth(31)),
                new TextItem(1, 4, "ВЫХОД".PadBoth(31))
            });

            // Цвета
            frame.SetColor(ConsoleColor.Black, ConsoleColor.Yellow);
            title.SetColor(ConsoleColor.DarkBlue, ConsoleColor.Green);
            menu.SetColor(ConsoleColor.DarkGreen, ConsoleColor.Green);

            // Отрисовка рамки вокруг меню
            frame.Draw();

            // Показ названия и меню
            title.Show();
            menu.Show();

            // Индекс выбранного пункта
            int selected = menu.SelectItem();

            switch (selected)
            {
            case 0:
                Game();
                break;

            case 1:
                Settings();
                break;

            case 2:
                Exit();
                break;
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            int   score = 0;
            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point snakeTail = new Point(15, 15, 's');
            Snake snake     = new Snake(snakeTail, 5, Direction.RIGHT);

            snake.Draw();

            FoodGenerator foodGenerator = new FoodGenerator(80, 25, '$');
            Point         food          = foodGenerator.GenerateFood();

            food.Draw();

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodGenerator.GenerateFood();
                    food.Draw();
                    score++;
                }
                else
                {
                    snake.Move();
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKays(key.Key);
                }

                Thread.Sleep(300);
            }
            string str_score = Convert.ToString(score);

            WriteGameOver(str_score);
            Console.ReadLine();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            string name;

            while (true)
            {
                Console.Write("Type your name: ");
                name = Console.ReadLine();
                if (name.Length < 3)
                {
                    Console.Clear();
                    Console.WriteLine("Name will be longer than 3 symbols.");
                    continue;
                }
                else
                {
                    Console.Clear();
                    break;
                }
            }

            bool soundSwitch;

            while (true)
            {
                Console.Write("Do you want to turn on sounds? (Y/N): ");
                ConsoleKeyInfo answerkey = Console.ReadKey();
                if (answerkey.Key == ConsoleKey.Y)
                {
                    Console.Clear();
                    soundSwitch = true;
                    break;
                }
                else if (answerkey.Key == ConsoleKey.N)
                {
                    Console.Clear();
                    soundSwitch = false;
                    break;
                }
                else
                {
                    Console.WriteLine("\nPress \'Y\' or \'N\' key.");
                    continue;
                }
            }

            Score score = new Score(9);
            Level level = new Level(1);

            Params settings   = new Params();
            Sounds sound      = new Sounds(settings.GetResourceFolder());
            Sounds pointsound = new Sounds(settings.GetResourceFolder());

            if (soundSwitch == true)
            {
                sound.Play(level.level);
            }

            Console.SetWindowSize(80, 26);

            Walls walls = new Walls(80, 25);

            walls.Draw();

            Point p     = new Point(4, 5, '*');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            Console.ForegroundColor = ConsoleColor.Red;
            snake.Draw();
            Console.ForegroundColor = ConsoleColor.White;

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

            Console.ForegroundColor = ConsoleColor.Yellow;
            food.Draw();
            Console.ForegroundColor = ConsoleColor.White;

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food, score, level, sound, soundSwitch))
                {
                    if (soundSwitch == true)
                    {
                        pointsound.Play("point");
                    }
                    food = foodCreator.CreateFood();
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    food.Draw();
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    snake.Move();
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }

                if (level.level == 1)
                {
                    Thread.Sleep(100);
                }
                else if (level.level == 2)
                {
                    Thread.Sleep(75);
                }
                else if (level.level == 3)
                {
                    Thread.Sleep(50);
                }
                else
                {
                    Thread.Sleep(40);
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }
            Messages gameover = new Messages();

            gameover.WriteGameOver(name, score.score, level.level);
            if (soundSwitch == true)
            {
                sound.Stop(level.level);
                pointsound.Play("lose");
            }
            ConsoleKeyInfo _key = Console.ReadKey();

            if (_key.Key == ConsoleKey.Enter)
            {
                Application.Restart();
            }
        }
Esempio n. 6
0
        public void Game()
        {
            // Символы поля и стен
            char fieldCh = ' ';
            char wallCh  = '#';

            // Установка размера окна игры
            int width  = 55;
            int height = 15;

            Console.SetWindowSize(width + 1, height + 1);

            // Размеры поля и стен
            int columns = 30;
            int rows    = 15;

            // Инициализация статистики
            Stats.Initialize(32, 10);
            Stats.Score  = 0;
            Stats.Moves  = 0;
            Stats.Speed  = speed;
            Stats.Length = length;
            Stats.Show();

            // Отрисовка поля и стен
            Field field = new Field(columns, rows, fieldCh);
            Walls walls = new Walls(columns, rows, wallCh);

            // Отрисовка змейки
            Point tail  = new Point(1, 1, 'o');
            Snake snake = new Snake(tail, Stats.Length, Direction.Right);

            // Еда
            FoodSpawner foodSpawner = new FoodSpawner(walls.Width, walls.Height, "♥♦♣♠");

            foodSpawner.Initialize();

            // Ускорение змейки в процентах
            double modifier = 5;

            // Установка цвета
            field.SetColor(ConsoleColor.DarkCyan, ConsoleColor.Cyan);
            walls.SetColor(ConsoleColor.Black, ConsoleColor.DarkGreen);
            snake.SetColor(ConsoleColor.DarkCyan, ConsoleColor.Green);
            FoodSpawner.Food.SetColor(ConsoleColor.DarkCyan, ConsoleColor.Red);

            // Отображение
            field.Draw();
            walls.Draw();
            snake.Draw();
            FoodSpawner.Food.Draw();

            // Движение змейки
            while (true)
            {
                // Выбор следующего направления
                snake.HandleKey();

                // Условия смерти змейки
                if (snake.IsWallHit(walls.Width, walls.Height) || snake.IsTailHit())
                {
                    GameOver();
                    Thread.Sleep(2000);
                    MenuSelect();
                }
                // Змейка ест
                if (snake.Eat(FoodSpawner.Food))
                {
                    do
                    {
                        foodSpawner.Initialize();
                    } while (snake.IsHit(FoodSpawner.Food));
                    FoodSpawner.Food.Draw();
                    // Увеличение очков
                    Stats.Score += 10;
                    // Параллельное увеличение рекорда
                    if (Stats.Score >= Stats.HighScore)
                    {
                        Stats.HighScore = Stats.Score;
                    }
                    // Увеличение длины
                    Stats.Length++;
                    Stats.Speed += Stats.Speed / 100 * modifier;
                }
                else
                {
                    // Движение змейки
                    snake.Move();
                    // Увеличение шагов
                    Stats.Moves++;
                }

                // Показ статистики
                Stats.Show();
                // Установка скорости
                Thread.Sleep(1000 / (int)Stats.Speed);
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            //Console.SetBufferSize(80, 25);

            Walls walls = new Walls(80, 25);

            walls.Draw();



            //Рамочка



            HorizontalLines upline    = new HorizontalLines(0, 78, 0, '+');
            HorizontalLines downline  = new HorizontalLines(0, 78, 24, '+');
            VerticalLines   leftline  = new VerticalLines(0, 24, 0, '+');
            VerticalLines   rightline = new VerticalLines(0, 24, 78, '+');

            upline.Draw();
            downline.Draw();
            leftline.Draw();
            rightline.Draw();

            // отрисовка точек
            Point p     = new Point(4, 5, '*');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            snake.Draw();

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

            food.Draw();


            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

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


            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
                Thread.Sleep(100);
                snake.Move();
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(80, 24);
            Console.SetBufferSize(80, 24);


            Menu();


            // отрисовка рамки

            Walls walls = new Walls(80, 24);

            walls.Draw();



            Point p     = new Point(4, 4, '*');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            snake.Drow();

            FoodCreator foodCreator = new FoodCreator(78, 22, '$');
            Point       food        = foodCreator.CreateFood();

            food.Draw();


            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    Console.ForegroundColor = ConsoleColor.Red;

                    WriteAt("G A M E       O V E R", 34, 12);

                    Console.ResetColor();
                    Console.ReadLine();

                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }


                Thread.Sleep(70);



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



            Console.ReadKey();
        }
Esempio n. 9
0
        private static void Start()
        {
            Walls walls = new Walls(Width, Height);

            walls.Draw();

            Point p     = new Point(4, 5, '@');
            Snake snake = new Snake(p, 4, Direction.RIGHT);

            snake.Draw();

            FoodCreator foodCreator = new FoodCreator(Width, Height, '$');
            Point       food        = foodCreator.CreateFood(snake);

            food.Draw();
            int[,] s = new int[Width, Height];

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    do
                    {
                        food = foodCreator.CreateFood(snake);
                    } while (IsColision(snake, food));

                    food.Draw();
                }
                else
                {
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            s[i, j] = (int)Figures.EmptySpace;
                        }
                    }

                    foreach (var item in walls.Points)
                    {
                        s[item.x, item.y] = (int)Figures.Barrier;
                    }

                    s[food.x, food.y] = (int)Figures.Destination;

                    foreach (var item in snake.Points)
                    {
                        var first = snake.Points.Last();
                        if (item == first)
                        {
                            s[item.x, item.y] = (int)Figures.StartPosition;
                        }
                        else
                        {
                            s[item.x, item.y] = (int)Figures.Barrier;
                        }
                    }
                    var li = new LeeAlgorithm(s);
                    System.Drawing.Point head = new System.Drawing.Point(0, 0);
                    System.Drawing.Point nextstep;
                    if (!li.PathFound)
                    {
                        food = foodCreator.CreateFood(snake);
                    }
                    else
                    {
                        foreach (var item in li.Path)
                        {
                            if (item == li.Path.Last())
                            {
                                s[item.Item1, item.Item2] = (int)Figures.StartPosition;
                                head = new System.Drawing.Point(item.Item1, item.Item2);
                            }
                            else if (item == li.Path.First())
                            {
                                s[item.Item1, item.Item2] = (int)Figures.Destination;
                            }
                            else
                            {
                                s[item.Item1, item.Item2] = (int)Figures.Path;
                            }
                        }

                        nextstep = new System.Drawing.Point(li.Path[li.Path.Count - 2].Item1, li.Path[li.Path.Count - 2].Item2);



                        var dir = GetDirection(head, nextstep);

                        switch (dir)
                        {
                        case Direction.LEFT:
                            snake.HandleKey(ConsoleKey.LeftArrow);
                            break;

                        case Direction.RIGHT:
                            snake.HandleKey(ConsoleKey.RightArrow);
                            break;

                        case Direction.UP:
                            snake.HandleKey(ConsoleKey.UpArrow);
                            break;

                        case Direction.DOWN:
                            snake.HandleKey(ConsoleKey.DownArrow);
                            break;
                        }
                    }
                    int speed = 1;
                    snake.Move();
                    Thread.Sleep(speed);
                }
            }
            WriteGameOver();
            Console.ReadLine();
        }