/// <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> /// Prints the menu and it´s option, handles the player input regarding itself /// </summary> /// <param name="rows"> States the rows for the game generation</param> /// <param name="columns"> States the columns for the game generation</param> static void Menu(int rows, int columns) { //Variables string answer; bool loop = true; string wall = "----------------"; int level = 1; while (loop) { //The Menu itself Console.WriteLine(wall); Console.WriteLine("1. New Game"); Console.WriteLine("2. High Scores"); Console.WriteLine("3. Instructions"); Console.WriteLine("4. Credits"); Console.WriteLine("5. Quit"); Console.WriteLine(wall); answer = Console.ReadLine(); Console.WriteLine(wall); switch (answer) { //Starts new game case "1": Game(rows, columns, level); loop = false; break; //Shows the HighScore case "2": HighScore.PrintHighScoreList(); Console.WriteLine(wall); Console.WriteLine("Press Enter to continue."); while (Console.ReadKey().Key != ConsoleKey.Enter) { } break; //Print the intructions of the game case "3": Console.WriteLine(" - Use the keys WASD/arrows to move" + " across the board."); Console.WriteLine(" - Reach the objective to pass to" + " the next level"); Console.WriteLine(" - You can't pass through enemies" + " and walls"); Console.WriteLine(" - Enemies will drain your HP if by" + " your side"); Console.WriteLine(" - Power ups will give you HP"); Console.WriteLine(" - For each move you do, you lose 1" + " HP"); Console.WriteLine(" - You can use a random teleport" + " to the first column by pressing the Q key"); Console.WriteLine(" - You can quit at any moment of" + "the game by pressing the Escape key(esc)."); Console.WriteLine(wall); Console.WriteLine("Press Enter to continue."); while (Console.ReadKey().Key != ConsoleKey.Enter) { } break; //Prints the names of the developers case "4": Console.WriteLine("This game was developed by:"); Console.WriteLine("Antonio Branco (21906811)"); Console.WriteLine("Joao Goncalves (21901696)"); Console.WriteLine("Vasco Duarte (21905658)"); Console.WriteLine(wall); Console.WriteLine("Press Enter to continue."); while (Console.ReadKey().Key != ConsoleKey.Enter) { } break; //Leaves the game case "5": return; //An option when the awnser is not valid default: Console.WriteLine("That's not a valid option"); Console.WriteLine( "Please type the number of the option"); break; } } }
public void Update(FileParser p) { world = new World(); visualization = new Interface(); levelGen = new LevelGenerator(); player = new Player(); parser = p; messages = new List <string>(); Tuple <int, int> playerPos; Tuple <int, int> exitPos; List <Object> currentTile; List <IDealsDamage> tileDmg; List <NPC> tileNPC; List <IItem> tileItems; List <IItem> inventoryItems; int level = 1; bool quit = false; bool action; ConsoleKeyInfo option; HighScore hS; short itemNum, npcNum; if (keyBinds.Count == 0) { AddKeys(); } messages.Add("Welcome to the game!"); do { exitPos = levelGen.GenerateLevel(world, player, level, parser); playerPos = new Tuple <int, int>(player.X, player.Y); do { // Clear our command flags to update next CommandFlag = Command.None; action = false; tileItems = new List <IItem>(); tileDmg = new List <IDealsDamage>(); tileNPC = new List <NPC>(); currentTile = world.WorldArray[player.X, player.Y]. GetInfo().ToList(); foreach (IItem obj in currentTile.OfType <IItem>()) { tileItems.Add(obj); } foreach (IDealsDamage obj in currentTile.OfType <IDealsDamage>()) { tileDmg.Add(obj); } foreach (IDealsDamage obj in tileDmg) { if (obj is Trap) { if (!(obj as Trap).FallenInto) { obj.OnDetectingPlayer(this); } } if (obj is NPC) { tileNPC.Add(obj as NPC); if (((obj as NPC).Hostile)) { obj.OnDetectingPlayer(this); } } } inventoryItems = player.Inventory.GetInfo().ToList(); if (player.HP <= 0) { break; } visualization.ShowWorld(world, player, level); visualization.ShowStats(world, player); visualization.ShowLegend(world); visualization.ShowMessages(world, messages); visualization.ShowSurrounds (world.GetSurroundingInfo(player)); visualization.ShowOptions(new List <ConsoleKey>(keyBinds.Keys)); messages.Clear(); // Update our input for everything else to use option = Console.ReadKey(); if (keyBinds.TryGetValue(option.Key, out var command)) { CommandFlag |= command; switch (CommandFlag) { case Command.Quit: string wantsQuit; do { visualization.AskQuit(); wantsQuit = Console.ReadLine(); } while ((wantsQuit.ToUpper() != "Y") && (wantsQuit.ToUpper() != "N")); if (wantsQuit.ToUpper() == "Y") { quit = true; } break; case Command.MoveNorth: if (player.MoveNorth()) { playerPos = world.UpdatePlayer(playerPos, player); action = true; messages.Add("You moved NORTH"); } else { messages.Add("You tried to move NORTH." + " But you hit a wall instead"); } break; case Command.MoveSouth: if (player.MoveSouth(world.X)) { playerPos = world.UpdatePlayer(playerPos, player); action = true; messages.Add("You moved SOUTH"); } else { messages.Add("You tried to move SOUTH." + " But you hit a wall instead"); } break; case Command.MoveWest: if (player.MoveWest()) { playerPos = world.UpdatePlayer(playerPos, player); action = true; messages.Add("You moved WEST"); } else { messages.Add("You tried to move WEST." + " But you hit a wall instead"); } break; case Command.MoveEast: if (player.MoveEast(world.Y)) { playerPos = world.UpdatePlayer(playerPos, player); action = true; messages.Add("You moved EAST"); } else { messages.Add("You tried to move EAST." + " But you hit a wall instead"); } break; case Command.AttackNPC: if ((tileNPC.Count > 0)) { if (player.SelectedWeapon != null) { do { visualization.ShowNPCsToAttack( tileNPC); short.TryParse( Console.ReadLine(), out npcNum); } while ((npcNum < 0) || (npcNum > tileItems.Count)); if (npcNum != tileItems.Count) { player.AttackNPC(this, tileNPC[npcNum]); action = true; } } else { messages.Add("You tried to attack an" + " NPC but you don't have a " + "weapon equipped"); } } else { messages.Add("You tried to attack an NPC" + " but there are no NPC in the " + "current tile"); } break; case Command.PickUpItem: if (tileItems.Count > 0) { do { visualization.ShowItems(tileItems, "Pick Up"); short.TryParse( Console.ReadLine(), out itemNum); } while ((itemNum < 0) || (itemNum > tileItems.Count)); if (itemNum != tileItems.Count) { tileItems[itemNum].OnPickUp(this); action = true; } } else { messages.Add("You tried to PICK UP an " + "item but there are no items " + "available to be picked up"); } break; case Command.UseItem: if (inventoryItems.Count > 0) { do { visualization.ShowItems(inventoryItems, "Use"); short.TryParse( Console.ReadLine(), out itemNum); } while ((itemNum < 0) || (itemNum > inventoryItems.Count)); if (itemNum != inventoryItems.Count) { inventoryItems[itemNum].OnUse(this); action = true; } } else { messages.Add("You tried to USE an " + "item but you currently don't have" + " any items in the inventory"); } break; case Command.DropItem: if (inventoryItems.Count > 0) { do { visualization.ShowItems(inventoryItems, "Drop"); short.TryParse( Console.ReadLine(), out itemNum); } while ((itemNum < 0) || (itemNum > inventoryItems.Count)); if (itemNum != inventoryItems.Count) { inventoryItems[itemNum].OnDrop(this); action = true; } } else { messages.Add("You tried to DROP an " + "item but you currently don't have" + " any items in the inventory"); } break; case Command.Information: messages.Add("You sought more info in the " + "elder scrolls"); visualization.ShowInformation(parser); Console.ReadKey(); break; } if (action) { player.LoseHP(1); } } else { Console.WriteLine(); visualization.WrongOption(option.Key.ToString()); Console.ReadKey(); } } while ((!playerPos.Equals(exitPos)) && (!quit) && (player.HP > 0)); if (!quit) { level++; } } while ((player.HP > 0) && (!quit)); if (!quit) { visualization.ShowWorld(world, player, level); visualization.ShowStats(world, player); visualization.ShowLegend(world); visualization.ShowDeath(level); } if (CheckHighScore(level)) { visualization.Success(level); string name = Console.ReadLine(); if (name.Length > 3) { name = name.Substring(0, 3); } hS = new HighScore(name, level); parser.UpdateHighScores(hS); } else { visualization.Failure(level); Console.ReadKey(); } }