Beispiel #1
0
        public void StartGame(Snake snake, Board board)
        {
            // Customize Snake
            snake = CustomizeSnake();

            // set the snake in the middle of the board
            snake.XPosition = board.Boardwidth / 2;
            snake.YPosition = board.Boardheight / 2;

            // set defaults
            //game is in play, no history of moves, snake length is 1 and game speed is 75
            IsGameOver = false;
            DidHitWall = false;
            Eaten      = new Dictionary <string, bool>();
            int GameSpeed = 75;

            snake.Length = 1;

            //do not show the cursor and initialize a variable for key input
            Console.CursorVisible = false;
            ConsoleKeyInfo command;

            while (!IsGameOver)
            {
                // clear the console, set the title bar, and draw the board
                outputProvider.Clear();
                outputProvider.CreateTitle(Message.Instructions);
                board.DrawBoard();

                //clear move history, and add current position, then draw the snake
                Eaten.Clear();
                Eaten.Add(snake.XPosition.ToString() + snake.YPosition.ToString(), true);
                snake.DrawSnake();

                //wait for the player to move
                WaitForMove();

                //set the speed by checking for keystrokes at the gamespeed in miliseconds
                DateTime nextCheck = DateTime.Now.AddMilliseconds(GameSpeed);

                while (!IsGameOver && !DidHitWall)
                {
                    //Display the length at the top of the screen
                    outputProvider.CreateTitle(Message.Score + snake.Length.ToString());

                    //wait for the next time you can check for keys
                    while (nextCheck > DateTime.Now)
                    {
                        // see if the player has changed direction
                        if (Console.KeyAvailable)
                        {
                            //read the key and map it to a direction
                            command = Console.ReadKey(true);
                            MapCommandToDirection(command);
                        }
                    }

                    if (!IsGameOver)
                    {
                        ChangeDirection(snake, board);

                        if (!DidHitWall)
                        {
                            //format the current positions to two rounded digits
                            string positions = snake.XPosition.ToString("00") + snake.YPosition.ToString("00");
                            //if the snake hasn't been to the current positions, add the length and keep going
                            if (!Eaten.ContainsKey(positions))
                            {
                                snake.Length++;
                                Eaten.Add(positions, true);
                                snake.DrawSnake();
                            }
                            //otherwise say they hit the wall
                            else
                            {
                                DidHitWall = true;
                            }
                        }

                        nextCheck = DateTime.Now.AddMilliseconds(GameSpeed);
                    }
                }

                if (DidHitWall)
                {
                    outputProvider.CreateTitle(Message.You_Died + snake.Length.ToString());
                    SetConsoleToDefault();
                    outputProvider.WriteLine(Message.SkullArt);
                    JustWait();
                    IsGameOver = true;
                }
            }
            if (IsGameOver)
            {
                SetConsoleToDefault();
                outputProvider.CreateTitle(Message.Youre_Done + snake.Length.ToString());
                outputProvider.Write(Message.SnakeArt);
                JustWait();
            }
        }