Exemple #1
0
        public void Main()
        {
            int gameFieldRow = 40;
            int uiFieldRow   = 5;

            int gameFieldColumn = 40;
            int uiFieldColumn   = 0;

            int wallLength = 1;

            int gameSpeed = 50; //low - means faster

            ConsoleWindow console =
                new ConsoleWindow
                    (gameFieldRow + uiFieldRow + wallLength * 2,
                    gameFieldColumn + uiFieldColumn + wallLength * 2,
                    "ASCII Snake");

            while (true)
            {
                World world = new World();

                world.Objects.Add(createSnake(gameFieldRow, uiFieldRow, gameFieldColumn, uiFieldColumn));

                foreach (Wall wall in createWalls(gameFieldRow, uiFieldRow, gameFieldColumn, uiFieldColumn))
                {
                    world.Objects.Add(wall);
                }

                createApple(gameFieldRow, uiFieldRow, gameFieldColumn, uiFieldColumn, wallLength, console, world);

                CancellationTokenSource cts   = new CancellationTokenSource();
                CancellationToken       token = cts.Token;

                checkKeyboardAsync(console, world, world.GetSnake(), token);

                while (console.WindowUpdate())
                {
                    Thread.Sleep(gameSpeed);

                    Snake snake = world.GetSnake();

                    if (snake != null)
                    {
                        snake.Move(console.GetKey());
                    }

                    GroupObj thatContactWithSnake = world.WithWhatSnakeIsContact();

                    if (thatContactWithSnake != null)
                    {
                        if (thatContactWithSnake.GetType() == new Apple(null).GetType())
                        {
                            world.Objects.Remove(world.GetApple());
                            snake.Lengthen();
                        }
                        else if ((thatContactWithSnake.GetType() == new Wall(null).GetType()))
                        {
                            break;
                        }
                        else if (thatContactWithSnake.GetType() == new Snake().GetType())
                        {
                            break;
                        }
                    }

                    if (!world.IsAppleExist())
                    {
                        createApple(gameFieldRow, uiFieldRow, gameFieldColumn, uiFieldColumn, wallLength, console, world);
                    }

                    Renderer.Render(console, world.Objects);
                }

                cts.Cancel();

                if (!console.WindowUpdate())
                {
                    break;
                }
            }
        }