Exemple #1
0
        public void PlayGame()
        {
            do
            {
                DisplayWorld();
                DisplayStats();
                AskForMovement();
                CheckRoom();
            } while (player.Health > 0 && Monster.remainingMonsters > 0);


            void DisplayWorld()
            {
                Console.Clear();
                Console.WriteLine();
                Console.Write("              ");
                Console.WriteLine("Dungeons of Doom");

                Console.WriteLine(" ___________________________________________");
                for (int y = 0; y < world.GetLength(1); y++)
                {
                    Console.WriteLine("|                                           |");
                    Console.Write("| ");
                    for (int x = 0; x < world.GetLength(0); x++)
                    {
                        Console.Write(" ");
                        Room room = world[x, y];
                        if (player.X == x && player.Y == y)
                        {
                            Console.Write("P");
                        }
                        else if (room.Monster != null)
                        {
                            Console.Write("M");
                        }
                        else if (room.Item != null)
                        {
                            Console.Write("I");
                        }
                        else if (room.Obstacle != null)
                        {
                            Console.Write("#");
                        }
                        else
                        {
                            Console.Write(".");
                        }
                    }
                    Console.Write("  |");
                    Console.WriteLine();
                }
                Console.WriteLine("|___________________________________________|");
            }

            void DisplayStats()
            {
                Console.WriteLine();
                Console.WriteLine($"Health: {player.Health}");
                Console.WriteLine($"Strength: {player.Strength}");

                Console.WriteLine();
                Console.WriteLine($"Remaining monsters: {Monster.remainingMonsters}");

                Console.WriteLine();
                Console.WriteLine("Press 'm' to see backpack");
            }

            void DisplayBackpack()
            {
                Console.WriteLine("*---------------------*");
                Console.WriteLine("|      BackPack       |");
                Console.WriteLine("*---------------------*");
                foreach (List <IBackpackAble> itemList in player.backpack)
                {
                    Console.Write("| ");
                    if (itemList[0] is Monster)
                    {
                        Console.Write($"{itemList[0].Name} corpses: {itemList.Count()}".PadRight(19));
                    }
                    else
                    {
                        Console.Write($"{itemList[0].Name}s: {itemList.Count()}".PadRight(19));
                    }
                    Console.WriteLine(" |");
                }
                Console.WriteLine("*---------------------*");
                askForMenuInput();
            }

            void askForMenuInput()
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                if (char.IsNumber(keyInfo.KeyChar))
                {
                    player.useItemInBackpack(int.Parse(keyInfo.KeyChar.ToString()));
                }
            }

            void AskForMovement()
            {
                int  newX       = player.X;
                int  newY       = player.Y;
                bool isValidKey = true;

                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                switch (keyInfo.Key)
                {
                case ConsoleKey.RightArrow: newX++; break;

                case ConsoleKey.LeftArrow: newX--; break;

                case ConsoleKey.UpArrow: newY--; break;

                case ConsoleKey.DownArrow: newY++; break;

                case ConsoleKey.M: DisplayBackpack(); break;

                default: isValidKey = false; break;
                }

                if (isValidKey &&
                    newX >= 0 && newX < world.GetLength(0) &&
                    newY >= 0 && newY < world.GetLength(1))
                {
                    player.X = newX;
                    player.Y = newY;
                }
            }

            void CheckRoom()
            {
                Room currentRoom = world[player.X, player.Y];

                if (currentRoom.Item != null)
                {
                    Console.WriteLine($"You find a {currentRoom.Item.Name}");
                    player.addItemToBackpack(currentRoom.Item);
                    Console.ReadKey();
                }
                if (currentRoom.Monster != null)
                {
                    Console.WriteLine($"Eeek, a {currentRoom.Monster.Name}!");
                    Console.ReadKey();

                    bool willThereBeAFight = currentRoom.Monster.Encounter(player);

                    if (willThereBeAFight)
                    {
                        bool wonFight = Fight(player, currentRoom.Monster);
                        if (wonFight)
                        {
                            player.addItemToBackpack(currentRoom.Monster);
                        }
                    }
                }
                if (currentRoom.Obstacle != null)
                {
                    while (currentRoom.Obstacle.Health > 0)
                    {
                        player.Attack(currentRoom.Obstacle);
                        Console.WriteLine($"You attack the Obstacle");
                        Console.ReadKey();
                    }
                }
                currentRoom.ResetRoom();
            }

            bool Fight(Character inputPlayer, Monster monster)
            {
                bool wonFight = false;

                Console.WriteLine($"The {monster.Name} wants to fight, brace yourself!");
                Console.ReadKey(false);
                Console.WriteLine($"Player Health:       {monster.Name} Health:");
                Console.Write($" {inputPlayer.Health}                   {monster.Health}");
                Console.WriteLine();

                while (inputPlayer.Health > 0 && monster.Health > 0)
                {
                    //Console.WriteLine($"You attack {monster.Name}");
                    monster.Attack(inputPlayer);
                    Console.ReadKey();
                    Console.Write($"{inputPlayer.Health}                  ");

                    if (inputPlayer.Health > 0)
                    {
                        //Console.WriteLine($"{monster.Name} attacks you");
                        inputPlayer.Attack(monster);
                        Console.ReadKey();
                        Console.Write($"{monster.Health}");

                        Console.WriteLine();
                    }
                }
                Console.ReadKey();
                if (inputPlayer.Health > 0)
                {
                    wonFight = true;
                }
                else
                {
                    wonFight = false;
                }
                return(wonFight);
            }
        }
Exemple #2
0
        private void FightWithMonster(int x, int y)
        {
            Monster monster = world[x, y].Monster;
            Room    room    = world[x, y];

            //Clear fighting-text-field
            for (int i = 2; i < 13; i++)
            {
                Console.SetCursorPosition(52, i);
                Console.WriteLine("                                                                    ");
            }
            int dice  = random.Next(1, 4);
            int input = 1;

            if (world[x, y].Monster != null)
            {
                Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY);
                TextProcessor.AnimateText(TextToDisplayLibrary.textToDisplayList[0], 500);
                Console.SetCursorPosition(103, 3);

                //Control input for dice/randomnumber comparer
                bool inputBool = false;
                while (!inputBool)
                {
                    try
                    {
                        int userInput = int.Parse(Console.ReadLine());
                        if (userInput == 1 || userInput == 2 || userInput == 3)
                        {
                            input     = userInput;
                            inputBool = true;
                        }
                        else
                        {
                            Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 1);
                            Console.WriteLine("Ange 1 till 3");
                            Console.SetCursorPosition(108, 3);
                        }
                    }
                    catch (Exception)
                    {
                        Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 1);
                        Console.WriteLine("Ange 1 till 3");
                        Console.SetCursorPosition(108, 3);
                    }
                }
                Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 2);

                //Player attacks
                player.Attack(monster, dice, input, world);
                //Console information for player.attacks():
                if (dice != input)
                {
                    Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 2);
                    TextProcessor.AnimateText($"Du smaskade till {monster.Name} med en Hawaii!  ", 0);


                    if (monster.IsDead)
                    {
                        Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY);
                        TextProcessor.AnimateText($"Snyggt pizzat! Du besegrade {monster.Name}!                        ", 2000);

                        Console.WriteLine();

                        //Clear text-display-field
                        for (int i = 4; i < 16; i++)
                        {
                            Console.SetCursorPosition(54, i);
                            Console.WriteLine("                                                               ");
                        }
                    }
                    if (monster != null)
                    {
                        Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 3);
                        TextProcessor.AnimateText($"{monster.Name} health: {monster.Health}", 500);
                    }
                }
                else if (input == dice)
                {
                    Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 2);
                    Console.WriteLine($"Aj aj! {monster.Name} högg dig i vaden!                ");
                    Console.SetCursorPosition(SetCursorConstant.SETCURSORFORFIGHTINGINFOX, SetCursorConstant.SETCURSORFORFIGHTINGINFOY + 3);
                    Console.WriteLine($"{monster.Name} health: {monster.Health}");
                }
            }

            //If monster still exists, monster attacks:
            if (room.Monster != null)
            {
                Console.SetCursorPosition(55, 9);
                TextProcessor.AnimateText($"{monster.Name} kontrar! Fly din dåre!              ", 2000);

                if (!Console.KeyAvailable)
                {
                    room.Monster.Attack(player, dice, input, world);
                    switch (dice)
                    {
                    case 1:
                        if (player.Backpack.Count() > 0)
                        {
                            Console.SetCursorPosition(55, 11);
                            TextProcessor.AnimateText($"{monster.Name} fick tag på dig och stal en pizza eller nått! Attans!                 ", 1000);
                        }
                        else
                        {
                            Console.SetCursorPosition(55, 11);
                            TextProcessor.AnimateText($"{monster.Name} fick tag på dig men du hade en tom backpack!             ", 1000);
                        }
                        break;

                    case 2:
                        if (player.Backpack.Count() > 0)
                        {
                            Console.SetCursorPosition(55, 11);
                            TextProcessor.AnimateText($"{monster.Name} fick tag på dig och stal en pizza eller nått! Attans!                 ", 1000);
                        }
                        else
                        {
                            Console.SetCursorPosition(55, 11);
                            Console.WriteLine($"{monster.Name} fick tag på dig men du hade en tom backpack!   ");
                        }
                        break;

                    case 3:
                        Console.SetCursorPosition(55, 11);
                        TextProcessor.AnimateText($"Smidig som en iller undvek du {monster.Name}! Du får en öl!            ", 0);
                        break;

                    default:
                        break;
                    }
                }
            }
        }