Beispiel #1
0
        static void Main(string[] args)
        {
            bool newGame = true;

            do//restart game loop
            {
                //menu setup
                string difficulty = "Easy";
                Dictionary <string, int> diffparameters = setdifficultydata(difficulty);
                List <string>            easyLocation   = new List <string>()
                {
                    "22,12", "23,12"
                };
                List <string> MediumLocation = new List <string>()
                {
                    "26,12", "27,12", "28,12"
                };
                List <string> HardLocation = new List <string>()
                {
                    "31,12", "32,12"
                };
                Snake snake;
                bool  closeMenu = false;
                //MENU
                while (!closeMenu)
                {
                    Console.SetCursorPosition(48, 14);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("Easy    ");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("Medium    ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("Hard");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.SetCursorPosition(52, 0);
                    Console.Write("Move with W-A-S-D");
                    buildBoard(diffparameters);
                    snake = new Snake(diffparameters);
                    while (snake.Alive)
                    {
                        do
                        {
                            snake.Move();
                            Thread.Sleep(100);
                            string diffSelect = snake.getSnakeHeadLocations();
                            if (easyLocation.Contains(diffSelect))
                            {
                                difficulty = "Easy";
                                closeMenu  = true;
                            }
                            if (MediumLocation.Contains(diffSelect))
                            {
                                difficulty = "Medium";
                                closeMenu  = true;
                            }
                            if (HardLocation.Contains(diffSelect))
                            {
                                difficulty = "Hard";
                                closeMenu  = true;
                            }
                            if (!snake.Alive || closeMenu)
                            {
                                break;
                            }
                        }while (!Console.KeyAvailable);
                        if (!snake.Alive || closeMenu)
                        {
                            break;
                        }
                        snake.UpdateDirection(Console.ReadKey().KeyChar);
                    }
                    Console.Clear();
                }

                //game setup
                diffparameters = setdifficultydata(difficulty);
                snake          = new Snake(diffparameters);
                buildBoard(diffparameters);
                double speed = diffparameters["speed"];
                int    score = -1;
                //START GAME!
                while (snake.Alive)
                {
                    do
                    {
                        snake.Move();
                        Thread.Sleep((int)speed);
                        if (!snake.Alive)
                        {
                            break;
                        }
                        if (snake.FoodPosition == "")
                        {
                            snake.FoodPosition = spawnFood(snake.getSnakeLocations(), diffparameters);
                            if (snake.FoodPosition != "")
                            {
                                score++;
                                speed -= 0.2;
                                updateScore(diffparameters, score);
                            }
                        }
                    }while (!Console.KeyAvailable);
                    if (!snake.Alive)
                    {
                        break;
                    }
                    snake.UpdateDirection(Console.ReadKey().KeyChar);
                }
                snake.DeathAnimation();
                //High Score
                Console.SetCursorPosition(49, 0);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Press ENTER to continue");
                Console.ForegroundColor = ConsoleColor.White;
                Console.SetCursorPosition(0, 1);
                Console.ReadLine();//wait for input
                Console.Clear();
                diffparameters = setdifficultydata("Easy");
                buildBoard(diffparameters);
                highScore(score, difficulty);

                //do we want to restart the game?
                Console.SetCursorPosition(49, 0);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Start New Game? (Y) (N)");
                Console.ForegroundColor = ConsoleColor.White;
                bool wait = true;
                while (wait)
                {
                    Console.SetCursorPosition(0, 1);
                    char newGameImput = Console.ReadKey().KeyChar;
                    if (newGameImput == 'y')
                    {
                        newGame = true;
                        wait    = false;
                    }
                    else if (newGameImput == 'n')
                    {
                        newGame = false;
                        wait    = false;
                    }
                }
                Console.Clear();
            }while (newGame);