Example #1
0
        public TextSnake(TextSnakeHead head, int startFragmentsCount, char fragmentChar)
        {
            this.Head = head;

            var fragments = new List <IDrawble>(startFragmentsCount);

            for (int i = 0; i < startFragmentsCount; i++)
            {
                fragments.Add(new TextSnakeFragment {
                    FragmentChar = fragmentChar, Position = new Numeric2D(head.Position.X + (i + 1), head.Position.Y)
                });
            }

            this.Fragments = fragments;
        }
Example #2
0
        private static async Task SnakeTestAsync()
        {
            Console.Title         = "Snake";
            Console.CursorVisible = false;

            const char snakeFragmentChar     = '=';
            const char snakeHeadFragmentChar = '*';
            const char fruitFragmentChar     = 'A';
            const char snakeTextGroundChar   = '.';

            const decimal speedMultiplier = 0.75M;
            const int     groundBoundX    = 50;
            const int     groundBoundY    = 25;

            decimal speedDelay = 1000;

            Numeric2D       groundBounds = new Numeric2D(groundBoundX, groundBoundY);
            SnakeTextGround ground       = new SnakeTextGround(snakeTextGroundChar, groundBounds.X, groundBounds.Y);

            TextSnakeHead snakeHead = new TextSnakeHead
            {
                FragmentChar = snakeHeadFragmentChar,
                Position     = groundBounds.RandomIn(),
                Health       = 100
            };

            var fruit = new TextSnakeFragment
            {
                Position     = groundBounds.RandomIn(),
                FragmentChar = fruitFragmentChar
            };

            TextSnake snake = new TextSnake(snakeHead, 2, snakeFragmentChar);

            ground.ToDraw.Add(fruit);
            ground.ToDraw.Add(snakeHead);
            ground.ToDraw.AddRange(snake.Fragments);

            Numeric2D newSnakePos = snake.Head.Position;

            void showInformation()
            {
                Console.WriteLine($"Length - {snake.Fragments.Count}");
                Console.WriteLine($"Health - {snake.Head.Health}");
                Console.WriteLine($"Speed - {Decimal.Round(1 / speedDelay, 3)}");
            }

            HandleConsoleInputAsync();

            while (snake.Head.Health > 0)
            {
                switch (direction)
                {
                case SnakeDirection.Back:
                    newSnakePos.Y--;
                    break;

                case SnakeDirection.Forward:
                    newSnakePos.Y++;
                    break;

                case SnakeDirection.Left:
                    newSnakePos.X--;
                    break;

                case SnakeDirection.Right:
                    newSnakePos.X++;
                    break;
                }

                Console.Clear();

                if (newSnakePos.Y >= ground.Heigth)
                {
                    newSnakePos.Y = 0;
                }
                else if (newSnakePos.X >= ground.Width)
                {
                    newSnakePos.X = 0;
                }
                else if (newSnakePos.Y < 0)
                {
                    newSnakePos.Y = ground.Heigth - 1;
                }
                else if (newSnakePos.X < 0)
                {
                    newSnakePos.X = ground.Width - 1;
                }

                snake.Move(newSnakePos);

                foreach (var fragment in ground.ToDraw)
                {
                    if (snake.Head.Position != fragment.Position)
                    {
                        continue;
                    }

                    if (snake.Fragments.Contains(fragment) && fragment != snake.Head)
                    {
                        snake.Head.Health = 0;

                        break;
                    }
                    else if (fragment == fruit)
                    {
                        fruit.Position = groundBounds.RandomIn();

                        var newFragmentToDraw = new TextSnakeFragment
                        {
                            Position     = snake.Head.Position,
                            FragmentChar = snakeFragmentChar
                        };

                        snake.Fragments.Add(newFragmentToDraw);
                        ground.ToDraw.Add(newFragmentToDraw);

                        speedDelay *= speedMultiplier;

                        break;
                    }
                }

                Console.WriteLine(ground.ToString());
                showInformation();

                await Task.Delay((int)Math.Ceiling(speedDelay));
            }

            Console.Clear();
            Console.WriteLine("GAME OVER!");
            showInformation();
        }