Esempio n. 1
0
        private static void RunGame()
        {
            state          = GameState.GAME_RUNNING;
            OnScreenShapes = GameScreen;
            if (!mapInitialized)
            {
                InitGameScreen();
            }
            Snake snake = new Snake(25, 9, 4);

            GameScreen.Add(snake);
            Fruit fruit = new Fruit();

            GameScreen.Add(fruit);
            while (state != GameState.MAIN_MENU)
            {
                int        time   = -DateTime.Now.Millisecond;
                ConsoleKey?button = null;
                while (Console.KeyAvailable)
                {
                    button = Console.ReadKey(true).Key;
                }
                switch (button)
                {
                case ConsoleKey.W:
                case ConsoleKey.A:
                case ConsoleKey.S:
                case ConsoleKey.D:
                    snake.Move(button);
                    break;

                case ConsoleKey.Escape:
                    state = GameState.MAIN_MENU;
                    break;

                default:
                    snake.Move();
                    break;
                }
                if (fruit.OverlapsTile(snake.Body[0].x, snake.Body[0].y))
                {
                    snake.Expand();
                    fruit.FindEmptySpot();
                    if (snake.Body.Count > 15)
                    {
                        state = GameState.MAIN_MENU;
                        ShowGameWonScreen();
                    }
                }
                if (GameScreen.GetRange(2, GameScreen.Count - 2).
                    FindTopmostShapeForTile(snake.Body[0].x, snake.Body[0].y) != null ||
                    snake.OverlapsSelf())
                {
                    state = GameState.MAIN_MENU;
                    ShowGameLostScreen();
                }
                DrawScreen();
                time = +DateTime.Now.Millisecond;
                if (state != GameState.MAIN_MENU)
                {
                    System.Threading.Thread.Sleep(200);
                }
            }
            GameScreen.Remove(snake);
            GameScreen.Remove(fruit);
            OnScreenShapes = MenuScreenShapes;
        }