コード例 #1
0
        /// <summary>
        /// The game loop, verifies when the player HP drops to 0
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="columns"></param>
        static void Game(int rows, int columns, int level)
        {
            //Variables
            int[]  vector = new int[] { 0, 0 };
            Map    map;
            Player player;
            Ending end;

            Enemy[]   enemies;
            PowerUp[] powerup;

            //This is the game loop
            while (true)
            {
                //Generate map depending of the level
                player  = new Player(rows, columns);
                end     = new Ending();
                enemies = new Enemy[999];
                powerup = new PowerUp[999];
                map     = new Map(rows, columns, level, player, end, enemies,
                                  powerup);

                //This is the level loop
                while (true)
                {
                    //This for is the player round,
                    //doing twice so the player moves/tp twice
                    for (int i = 0; i < 2; i++)
                    {
                        //Draw the map in the console
                        MapDraw(rows, columns, enemies, map,
                                player.HP, level);
                        //Goes to the fonction that does the player movement
                        player = player.PlayerMovement(player, map, rows,
                                                       columns);
                        Console.WriteLine("----------------------------------");
                        //Condition to see if the player died, quiting the for
                        //to finish the game
                        if (player.HP <= 0)
                        {
                            break;
                        }
                        //Condition to see if the player reached the end of
                        //the level, quiting the for to go to the next level
                        if ((player.position[0] == end.position[0]) &&
                            (player.position[1] == end.position[1]))
                        {
                            Console.WriteLine(
                                $"Congrats, you passed to level {level+1}!");
                            break;
                        }
                    }
                    //Condition to see if the player died, quiting the while
                    //to finish the game
                    if (player.HP <= 0)
                    {
                        break;
                    }
                    //Condition to see if the player reached the end of
                    //the level, quiting the while do go to the next level
                    if ((player.position[0] == end.position[0]) &&
                        (player.position[1] == end.position[1]))
                    {
                        break;
                    }
                    //This for is for each enemy to move/attack
                    for (int i = 0; i < 1000; i++)
                    {
                        //By using try and catch, the for will stop if there's
                        //no more enemies to move/attack
                        try
                        {
                            //Call the function that does the enemy action
                            enemies[i] = enemies[i].EnemiesMovement(enemies, player,
                                                                    map, rows, columns, i, vector);
                            //This give the player some time to read what
                            //the enemies are doing
                            Thread.Sleep(500);
                        }
                        //Breaks the for if no more enemies
                        catch (NullReferenceException)
                        {
                            break;
                        }
                    }
                    Console.WriteLine("----------------");
                    //Condition to see if the player died, quiting the while
                    //to finish the game
                    if (player.HP <= 0)
                    {
                        break;
                    }
                }
                //If the player died, this will show of their score and player
                //goes back to the menu
                if (player.HP <= 0)
                {
                    Console.WriteLine("You dropped to 0 HP.");
                    Console.WriteLine("Game Over");
                    Console.WriteLine("Final Score: " + level);
                    //This will call the function do see if the player
                    //beats the highscore
                    HighScore.AddToHighScoreList(new HighScoreList("Pattern",
                                                                   level));
                    //Calls the menu
                    Menu(rows, columns);
                    break;
                }
                level += 1;
                //Saves the level if the player wants
                if (AskSaveGame())
                {
                    SaveGame.NewSaveGame(level, rows, columns);
                }
            }
        }