// This builds the world static void BuildNewWorld(Cell[,] cell, CellSave[,] cellSave, int newGameFile) { Console.Clear(); Console.SetCursorPosition((Console.WindowWidth / 2) - 7, Console.CursorTop + 10); Console.WriteLine("Building World"); for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { cellSave[x, y] = new CellSave(); } } if (newGameFile == 0) { for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { cell[x, y] = new Cell(); cell[x, y].SetLandType(); System.Threading.Thread.Sleep(10); Console.Write("."); } } PlaceMobs(cell); PlaceKeys(cell); } }
// This loads a game static int LoadGame(Hero[] hero, HeroSave[] heroSave, Cell[,] cell, CellSave[,] cellSave, string[] savedInfo, int currentGame, int newGameFile) { string jsonHero = System.IO.File.ReadAllText(currentGame + ".json"); heroSave[currentGame] = Newtonsoft.Json.JsonConvert.DeserializeObject <HeroSave>(jsonHero); hero[currentGame] = new Hero(); hero[currentGame].SetName(heroSave[currentGame].GetName()); hero[currentGame].SetLevel(heroSave[currentGame].GetLevel()); hero[currentGame].SetXP(heroSave[currentGame].GetXP()); hero[currentGame].SetHealth(heroSave[currentGame].GetHealth()); hero[currentGame].SetAttack(heroSave[currentGame].GetAttack()); hero[currentGame].SetKeys(heroSave[currentGame].GetKeys()); hero[currentGame].SetPotion(heroSave[currentGame].GetPotion()); hero[currentGame].LoadPositionX(heroSave[currentGame].GetPositionX()); hero[currentGame].LoadPositionY(heroSave[currentGame].GetPositionY()); string directory = System.IO.Directory.GetCurrentDirectory(); string savePath = directory + @"\CellSave" + currentGame + @"\"; if (newGameFile == 1) { int z = 1; string cellSaveNum = "X"; for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { cell[x, y] = new Cell(); cellSave[x, y] = new CellSave(); cellSaveNum = "cellSave" + z; string jsonSaveCell = System.IO.File.ReadAllText(savePath + "cellSave" + z + ".json"); cellSave[x, y] = Newtonsoft.Json.JsonConvert.DeserializeObject <CellSave>(jsonSaveCell); cell[x, y].LoadLandType(cellSave[x, y].GetLandType()); cell[x, y].LoadMobs(cellSave[x, y].GetMobs()); cell[x, y].SetVisited(cellSave[x, y].GetVisited()); cell[x, y].SetPotion(cellSave[x, y].GetPotion()); cell[x, y].SetKey(cellSave[x, y].GetKey()); cell[x, y].LoadMob(); z++; } } } return(currentGame); }
// ============================================================================================ // ============================================================================================ // MAIN static void Main(string[] args) { Console.SetCursorPosition((Console.WindowWidth - 8) / 2, Console.CursorTop + 6); Console.WriteLine("KeyQuest"); Console.SetCursorPosition((Console.WindowWidth - 23) / 2, Console.CursorTop + 4); Console.WriteLine("Press ENTER to continue"); BuildVersion(); Console.ReadLine(); Console.Clear(); Console.Write("\n\n\n\n\n\n\t\tLoading awesome adventures."); for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(200); Console.Write("."); } string choice = "0"; int alive = 1, clearGame = 0, newGameFile = 0; Hero[] hero = new Hero[10]; HeroSave[] heroSave = new HeroSave[10]; Cell[,] cell = new Cell[10, 10]; CellSave[,] cellSave = new CellSave[10, 10]; string[] savedInfo = new string[10]; bool exit = false; while (!exit) { newGameFile = 0; int savedGames = MainMenu(savedInfo); BuildVersion(); bool runGame = false, newGame = false, loadGame = false; int currentGame = 0; Console.SetCursorPosition((Console.WindowWidth) / 2 - 7, Console.CursorTop - 12); choice = Console.ReadLine(); if (choice == "1") { if (savedGames < 10) { currentGame = NewGame(hero, heroSave, cellSave, cell, savedInfo, currentGame, newGameFile); runGame = true; newGame = true; } else { ErrorInput(); Console.SetCursorPosition((Console.WindowWidth / 2) - 14, Console.CursorTop + 2); Console.WriteLine("There are no free slots for new games"); Console.SetCursorPosition((Console.WindowWidth / 2) - 14, Console.CursorTop + 2); Console.WriteLine("Please delete a saved game to be able to create a new game"); Console.SetCursorPosition((Console.WindowWidth / 2) - 14, Console.CursorTop + 2); Console.WriteLine("Press ENTER to continue"); Console.ReadLine(); } } else if (choice == "2") { if (savedGames > 0) { exit = false; while (!exit) { if (savedGames > 0) { choice = ManageGamesMenu(); if (choice == "1") { int answer = LoadGameMenu(savedInfo, savedGames); if (answer >= 0 && answer < savedGames) { currentGame = answer; runGame = true; loadGame = true; newGameFile = 1; exit = true; } } else if (choice == "2") { if (savedGames > 0) { int answer = DeleteGameMenu(savedInfo, savedGames); if (answer >= 0 && answer <= savedGames) { savedGames = DeleteGame(answer, savedInfo); } else { exit = true; } } else { LoadGameError(); exit = true; } } else if (choice == "3") { exit = true; } } else { LoadGameError(); exit = true; } } exit = false; if (loadGame) { exit = true; } } else { ErrorInput(); LoadGameError(); } } else if (choice == "3") { exit = true; } else { ErrorInput(); } if (runGame) { if (newGame) { BuildNewWorld(cell, cellSave, newGameFile); newGameFile = 1; } else if (loadGame) { newGameFile = 1; BuildNewWorld(cell, cellSave, newGameFile); currentGame = LoadGame(hero, heroSave, cell, cellSave, savedInfo, currentGame, newGameFile); } choice = "0"; exit = false; while (!exit) { Console.Clear(); choice = HeroAction(hero, ref currentGame, ref cell); int test = 0; switch (choice) { case "1": test = hero[currentGame].GetPositionY(); if (test == 1) { WallError(); } else { hero[currentGame].SetPositionY(-1); } break; case "2": test = hero[currentGame].GetPositionX(); if (test == 10) { WallError(); } else { hero[currentGame].SetPositionX(+1); } break; case "3": test = hero[currentGame].GetPositionY(); if (test == 10) { WallError(); } else { hero[currentGame].SetPositionY(+1); } break; case "4": test = hero[currentGame].GetPositionX(); if (test == 1) { WallError(); } else { hero[currentGame].SetPositionX(-1); } break; case "5": if (hero[currentGame].GetPotion() >= 1 && hero[currentGame].GetHealth() < 100) { int potion = hero[currentGame].GetHealth(); potion += 10; hero[currentGame].SetHealth(potion); hero[currentGame].SetPotion(-1); } break; case "6": SaveGame(hero, heroSave, cell, cellSave, savedInfo, ref currentGame, newGameFile); break; case "7": exit = true; break; case "2020": ShowKey(cell); break; default: break; } if (choice == "1" || choice == "2" || choice == "3" || choice == "4") { int heroX = hero[currentGame].GetPositionX() - 1; int heroY = hero[currentGame].GetPositionY() - 1; clearGame = Landscape(cell, ref hero, ref currentGame, ref heroX, ref heroY); if (cell[heroX, heroY].GetMobs() > 0) { choice = Encounter(cell, ref hero, ref currentGame, ref heroX, ref heroY); if (choice == "1" || choice == "2") { alive = MonsterFight(cell, hero, ref currentGame, ref heroX, ref heroY); } else { Console.Clear(); Console.SetCursorPosition((Console.WindowWidth / 2) - 36, Console.CursorTop + 6); Console.WriteLine("You turn around and run for your life. You can hear the evil snarls and shouts behind you.."); Console.SetCursorPosition((Console.WindowWidth / 2) - 36, Console.CursorTop + 2); System.Console.WriteLine("Press ENTER to continue your adventure"); Console.ReadLine(); } } else { if (cell[heroX, heroY] == cell[0, 9] || cell[heroX, heroY] == cell[9, 0]) { Console.SetCursorPosition((Console.WindowWidth / 2) - 34, Console.CursorTop + 2); Console.WriteLine("Once again you go to face the dangers of the mysterious world"); Console.SetCursorPosition((Console.WindowWidth / 2) - 34, Console.CursorTop + 2); System.Console.WriteLine("Press ENTER to continue your adventure"); Console.ReadLine(); } else { Console.SetCursorPosition((Console.WindowWidth / 2) - 34, Console.CursorTop + 2); Console.WriteLine("Your exceptional senses can not find anything hiding"); Console.SetCursorPosition((Console.WindowWidth / 2) - 34, Console.CursorTop + 2); System.Console.WriteLine("Press ENTER to continue your adventure"); Console.ReadLine(); } } FindKey(cell, hero, ref currentGame, ref heroX, ref heroY); } if (alive == 0 || clearGame == 1) { exit = true; } } exit = false; } } Console.Clear(); Console.SetCursorPosition((Console.WindowWidth - 21) / 2, Console.CursorTop + 6); Console.WriteLine("Thank you for playing!"); Console.SetCursorPosition((Console.WindowWidth - 19) / 2, Console.CursorTop + 4); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); Console.Clear(); Console.SetCursorPosition((Console.WindowWidth / 2) - 30, Console.CursorTop + 10); Console.Write("Chasing away the last monsters."); for (int i = 0; i < 15; i++) { System.Threading.Thread.Sleep(200); Console.Write("."); if (i == 4) { Console.Write("Shoo shoo."); } } }