/// <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); } } }
/// <summary> /// Handles the map generation /// </summary> /// <returns> The map </returns> public int[,] MapCreation(Player player, Ending end, Enemy[] enemies, PowerUp[] powerup) { // Variables map = new int [rows, columns]; Random rnd = new Random(); int x; int y; int luck; int nObstacles; int nEnemy; int nPowerUPs; //Generates a random number of obstacles depending of the map size nObstacles = rnd.Next(Math.Min(rows, columns) - 1); //Generates a random number of enemies depending of the map //size and level nEnemy = rnd.Next(((rows + columns) / 2) + level); //Conditions to not have more than half of the board with enemies if (nEnemy > ((rows * columns) / 2)) { nEnemy = (rows * columns) / 2; } //Generates a random number of power-ups depending of the map //size and level nPowerUPs = rnd.Next(((rows + columns) / 2) + level); //Condition to have a minimum of 1 power-up if (((rows + columns) / 2) - level <= 1) { nPowerUPs = 1; } //Create the spawns of the player in the first column x = rnd.Next(0, rows); player.position[0] = x; map[player.position[0], 0] = 1; //Create the spawns of the end in the last column x = rnd.Next(0, rows); end.position[0] = x; end.position[1] = columns - 1; map[x, columns - 1] = 2; //Puts the obstacles randomly in the map for (int i = 0; i < nObstacles; i++) { x = rnd.Next(0, rows); y = rnd.Next(0, columns); if (map[x, y] == 0) { map[x, y] = 3; } else { i--; } } //Puts the enemies randomly in the map for (int i = 0; i < nEnemy; i++) { x = rnd.Next(0, rows); y = rnd.Next(0, columns); if (map[x, y] == 0) { luck = rnd.Next(0, 6); enemies[i] = new Enemy(luck); enemies[i].position[0] = x; enemies[i].position[1] = y; //Conditions to see if the enemy is normal or strong if (enemies[i].HP == 5) { map[x, y] = 4; } else { map[x, y] = 5; } } else { i--; } } //Puts the power-ups randomly in the map for (int i = 0; i < nPowerUPs; i++) { x = rnd.Next(0, rows); y = rnd.Next(0, columns); if (map[x, y] == 0) { luck = rnd.Next(0, 9); powerup[i] = new PowerUp(luck); powerup[i].position[0] = x; powerup[i].position[1] = y; //Conditions to see if the power-up is small, medium or big if (powerup[i].HP == 4) { map[x, y] = 6; } else if (powerup[i].HP == 8) { map[x, y] = 7; } else { map[x, y] = 8; } } } return(map); }