Esempio n. 1
0
        /// <summary>
        /// Check the player's input and set the snake's direction.
        /// </summary>
        public void CheckUserInput()
        {
            Facade facade = new Facade();

            if (Console.KeyAvailable)
            {
                GetKey = Console.ReadKey(true).Key;
                switch (GetKey)
                {
                case ConsoleKey.UpArrow:
                    if (Facade.GetDirection != Direction.Down)
                    {
                        Facade.GetDirection = Direction.Up;
                    }

                    break;

                case ConsoleKey.DownArrow:
                    if (Facade.GetDirection != Direction.Up)
                    {
                        Facade.GetDirection = Direction.Down;
                    }

                    break;

                case ConsoleKey.LeftArrow:
                    if (Facade.GetDirection != Direction.Right)
                    {
                        Facade.GetDirection = Direction.Left;
                    }

                    break;

                case ConsoleKey.RightArrow:
                    if (Facade.GetDirection != Direction.Left)
                    {
                        Facade.GetDirection = Direction.Right;
                    }

                    break;

                case ConsoleKey.Escape:
                    facade.Menu(false, false);
                    break;

                default:
                    Facade.GetDirection = Direction.None;
                    break;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// GameOver page.
        /// </summary>
        public void GameOverMenu()
        {
            Facade facade = new Facade();

            const string fileName = "Score.txt";
            bool         retry;
            string       result = $"Date: {DateTime.Now} | Score: {Facade.GetScore}";

            do
            {
                Console.Clear();

                // Write on a file the final score.
                if (File.Exists(fileName))
                {
                    File.AppendAllText(fileName, Environment.NewLine + result);
                }
                else
                {
                    using StreamWriter sr = new StreamWriter(fileName, true);
                    sr.WriteLine(Environment.NewLine + "Date:" +
                                 $" {DateTime.Now} " +
                                 "| Score: " + $"{Facade.GetScore}");
                }

                Console.WriteLine("\n\tYou have met an unfortunate end.");
                Console.WriteLine("\n\t\tFinal score: " + Facade.GetScore);

                if (Facade.GetScore < 0)
                {
                    Console.WriteLine("\n\tHow did you get this score!? Cheater!\n");
                }
                else if (Facade.GetScore == 0)
                {
                    Console.WriteLine("\n\t     You didn't even try...\n");
                }
                else if (Facade.GetScore >= 1 && Facade.GetScore < 10)
                {
                    Console.WriteLine("\n       You can do better I believe in you!\n");
                }
                else if (Facade.GetScore >= 10 && Facade.GetScore < 20)
                {
                    Console.WriteLine("\n\t    Not bad at all, good job!\n");
                }
                else if (Facade.GetScore >= 20)
                {
                    Console.WriteLine("\n\t  Great score, congratulations!\n");
                }
                while (!Console.KeyAvailable)
                {
                    Console.WriteLine("\tPress ESQ to retreat to the menu.");
                    Thread.Sleep(250);

                    // Set cursor in the line before.
                    Console.SetCursorPosition(0, Console.CursorTop - 1);

                    // Delete the line before.
                    Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" +
                                  "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
                    Console.WriteLine("\tPress     to retreat to the menu.");
                    Thread.Sleep(250);

                    // Set cursor in the line before.
                    Console.SetCursorPosition(0, Console.CursorTop - 1);

                    // Delete the line before.
                    Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" +
                                  "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
                }

                // The player's choice.
                ConsoleKey key = Console.ReadKey(true).Key;

                if (key == ConsoleKey.Escape)
                {
                    facade.Menu(false, false);
                    retry = false;
                }
                else
                {
                    retry = true;
                }
            }while (retry);
        }