//TO DO public static void LoadPauseMenu(PlayerProfile currProfile) { List<MenuItem> pausedMenuItems = new List<MenuItem>() { new MenuItem(10, 43,"Resume game", true ), new MenuItem(25, 43, "Exit game", false) }; PrintMenu(pausedMenuItems); int selectedItem = 0; while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.LeftArrow) { if (selectedItem > 0) { pausedMenuItems[selectedItem - 1].Selected = true; pausedMenuItems[selectedItem].Selected = false; selectedItem--; } } else if (pressedKey.Key == ConsoleKey.RightArrow) { if (selectedItem < pausedMenuItems.Count - 1) { pausedMenuItems[selectedItem + 1].Selected = true; pausedMenuItems[selectedItem].Selected = false; selectedItem++; } } else if (pressedKey.Key == ConsoleKey.Enter) { if (pausedMenuItems[0].Selected) { ConsoleAction.Clear(10, 43, 30, 1); return; } else { Console.Clear(); Engine.SetDefaults(currProfile); LoadMainMenu(currProfile); } } PrintMenu(pausedMenuItems); } } }
public static void LoadMainMenu(PlayerProfile currProfile) { Console.Clear(); PrintLogo(); List<MenuItem> menuItems = new List<MenuItem>() { new MenuItem(30, 20, "New Game", true), new MenuItem(30, 25, "Select Level", false), new MenuItem(30, 30, "Hight scores",false), new MenuItem(30, 35, "Credits",false), new MenuItem(30, 40, "Exit game",false) }; string command = LoadCommand(menuItems); switch (command) { case "New Game": Console.Clear(); Engine.StartGame(1, currProfile); break; case "Select Level": ConsoleAction.Clear(30, 20, 20, 25); LoadLevelsMenu(currProfile); break; case "Hight scores": ConsoleAction.Clear(30, 20, 20, 25); VisualiseHightScores(currProfile); while (true) { if (Console.KeyAvailable) LoadMainMenu(currProfile); } case "Credits": ConsoleAction.Clear(30, 20, 20, 25); ConsoleAction.PrintOnPos("Comming soon", 30, 20, ConsoleColor.Red); ConsoleAction.PrintOnPos("Enter any key to return", 30, 22, ConsoleColor.Red); while (true) { if (Console.KeyAvailable) LoadMainMenu(currProfile); } case "Exit game": Exit(); break; case "return": LoadProfileMenu(); break; default: break; } }
//Converts score from PlayerProfile to string dor Profile.txt private static string ScoresAsString(PlayerProfile profile) { StringBuilder builder = new StringBuilder(); if (profile.BestScores.Count == 0) { //number of levels for (int i = 0; i < Level.NumberOfLevels(); i++) { builder.AppendFormat("{0}-0;", i + 1); } } foreach (var item in profile.BestScores) { builder.AppendFormat("{0}-{1};", item.Key, item.Value); } return builder.ToString(); }
//Write created profile public static void WriteToFile(PlayerProfile profile) { string[] profiles = new string[numberOfProfiles]; using (StreamReader reader = new StreamReader(profilesPath)) { for (int i = 0; i < numberOfProfiles; i++) { profiles[i] = reader.ReadLine(); } string scores = ScoresAsString(profile); profiles[profile.ProfileNumber] = string.Format("{0}.Name:{1},Color:{2},Scores:{3}", profile.ProfileNumber + 1, profile.Name, profile.PersonalTank.Color, scores); } using (StreamWriter writer = new StreamWriter(profilesPath)) { for (int i = 0; i < numberOfProfiles; i++) { writer.WriteLine(profiles[i]); } } }
public static void StartGame(int level, PlayerProfile player) { DateTime timeGameStart = DateTime.Now; //To use it for add score for every play time minute int passedMinutes = 1; int waves = 1 + level; int lives = gamerLives; Level currentLevel = new Level(level); List<CannonBall> cannonBalls = new List<CannonBall>(); List<Tank> enemyTanks = new List<Tank>(); SoundEngine.StartGameSound(); PlayerTank playerTank = player.PersonalTank; enemyTanks.Add(player.PersonalTank); List<LevelObject> allLevelObjects = new List<LevelObject>(currentLevel.LoadLevel()); allLevelObjects.Add(player.PersonalTank); int enemyTankPosition = 1; int enemyTanksCount = 5; for (int i = 0; i < enemyTanksCount; i++) { allLevelObjects.Add(new EnemySmartTank(playerTank, allLevelObjects, enemyTankPosition, 1, Directions.Down)); enemyTankPosition += 15; } enemyTankPosition = 1; currentLevel.PrintLevel(); playerTank.Print(); for (int i = 0; i < allLevelObjects.Count; i++) { if (allLevelObjects[i] is EnemySmartTank) { allLevelObjects[i].Print(); } } while (true) { while (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.UpArrow || pressedKey.Key == ConsoleKey.DownArrow || pressedKey.Key == ConsoleKey.LeftArrow || pressedKey.Key == ConsoleKey.RightArrow) { if (HitManager.ManageTankAndWallHit(playerTank, allLevelObjects, pressedKey)) // changed from LevelObjects { playerTank.Move(pressedKey); if (DateTime.Now > timeGameStart.AddSeconds(5)) { SoundEngine.MoveSound(); } } } else if (pressedKey.Key == ConsoleKey.Spacebar) { //implement fire SoundEngine.FireSound(); int[] barrelCoords = playerTank.GetTankBarrel(); cannonBalls.Add(new SimpleCannonBall(playerTank.Y + barrelCoords[0], playerTank.X + barrelCoords[1], 1, 100, playerTank.Direction, true)); } else if (pressedKey.Key == ConsoleKey.Escape) { Menu.LoadPauseMenu(player); } } Thread.Sleep(50); // If players tank is hitted, lose life // Shoot if possible and update direction and position for (int i = 0; i < allLevelObjects.Count; i++) { if (allLevelObjects[i] is PlayerTank) { PlayerTank currentPlayerTank = allLevelObjects[i] as PlayerTank; if (currentPlayerTank.IsDestroyed) { lives--; currentPlayerTank.LooseLive(); //clear position if (currentPlayerTank.Direction == Directions.Up || currentPlayerTank.Direction == Directions.Down) { ConsoleAction.Clear(playerTank.X, playerTank.Y, 6, 2); } else if (currentPlayerTank.Direction == Directions.Left || currentPlayerTank.Direction == Directions.Right) { ConsoleAction.Clear(playerTank.X, playerTank.Y, 3, 3); } currentPlayerTank.SetDefaultPosition(); currentPlayerTank.Print(); if (currentPlayerTank.IsGameOver) { EndGame(player, currentLevel.LevelNumber); } continue; } } if (allLevelObjects[i] is EnemySmartTank) { EnemySmartTank currentEnemyTank = allLevelObjects[i] as EnemySmartTank; if (currentEnemyTank.CanShootToPlayertank()) { int[] barrelCoords = (allLevelObjects[i] as EnemySmartTank).GetTankBarrel(); cannonBalls.Add(new SimpleCannonBall(allLevelObjects[i].Y + barrelCoords[0], allLevelObjects[i].X + barrelCoords[1], 1, 100, currentEnemyTank.Direction, false)); } currentEnemyTank.Update(); } } // Remove destroyed objects for (int i = 0; i < allLevelObjects.Count; i++) { if ((allLevelObjects[i] is IDestroyable) && !(allLevelObjects[i] is PlayerTank)) { if ((allLevelObjects[i] as IDestroyable).IsDestroyed) { // Check coordinate to clear for different type of objects(tank, cannonball, brick) ConsoleAction.Clear(allLevelObjects[i].X, allLevelObjects[i].Y, allLevelObjects[i].LoadVisual()[0].Length, allLevelObjects[i].LoadVisual().Length); allLevelObjects.RemoveAt(i); } } } //check if base is dstroyed if (HitManager.IsBaseDestroyed(allLevelObjects)) { EndGame(player, currentLevel.LevelNumber); } // Move all cannonballs for (int i = 0; i < cannonBalls.Count; i++) { ConsoleAction.Clear(cannonBalls[i].Y, cannonBalls[i].X, 1, 1); cannonBalls[i].Move(); cannonBalls[i].Print(); } // Check if some cannonball hit an object HitManager.ManageShotsAndLevelObject(cannonBalls, allLevelObjects, player); // Remove destroyed cannonballs for (int i = 0; i < cannonBalls.Count; i++) { if (cannonBalls[i].IsDestroyed) { ConsoleAction.Clear(cannonBalls[i].Y, cannonBalls[i].X, 1, 1); cannonBalls.RemoveAt(i); } } //Destroy enemies if (!allLevelObjects.Any(x => x is EnemyTank)) { waves--; if (waves == 0) { player.AddScore(500); WinGame(player, currentLevel.LevelNumber); } else { for (int i = 0; i < enemyTanksCount; i++) { allLevelObjects.Add(new EnemySmartTank(playerTank, allLevelObjects, enemyTankPosition, 1, Directions.Down)); enemyTankPosition += 15; } for (int i = 0; i < allLevelObjects.Count; i++) { if (allLevelObjects[i] is EnemySmartTank) { allLevelObjects[i].Print(); } } enemyTankPosition = 1; } } //Add score point for playing minute if (DateTime.Now.Minute == timeGameStart.AddMinutes(passedMinutes).Minute) { passedMinutes++; player.AddScore(10); } RePrintLevelObjects(allLevelObjects); PrintStats(player, lives); } }
private static void WinGame(PlayerProfile player, int levelNumber) { Console.Clear(); SoundEngine.StartGameSound(); player.SetScore(player.CurrentScore, (byte)levelNumber); ProfileManager.WriteToFile(player); ConsoleAction.PrintOnPos("YOU WIN !!!", 35, 20, ConsoleColor.Green); ConsoleAction.PrintOnPos(string.Format("Your Score: {0} points", player.CurrentScore), 35, 24, ConsoleColor.Green); SetDefaults(player); ConsoleAction.PrintOnPos("Press Esc to go back to the main menu", 35, 28, ConsoleColor.White); if (levelNumber < Level.NumberOfLevels()) { ConsoleAction.PrintOnPos("Press Enter for the next level", 35, 32, ConsoleColor.White); } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.Escape) { Console.Clear(); Menu.LoadMainMenu(player); } else if (pressedKey.Key == ConsoleKey.Enter) { Console.Clear(); StartGame(levelNumber + 1, player); } } } }
//stack overflow problem !!!! private static void PrintStats(PlayerProfile player, int lives) { ConsoleAction.PrintOnPos(string.Format("Name: {0}", player.Name), 10, 42, ConsoleColor.Cyan); ConsoleAction.PrintOnPos(string.Format("Lives: {0}", lives), 19 + player.Name.Length, 42, ConsoleColor.Cyan); ConsoleAction.PrintOnPos(string.Format("Score: {0}", player.CurrentScore), 29 + player.Name.Length, 42, ConsoleColor.Cyan); }
private static void EndGame(PlayerProfile player, int levelNumber) { Console.Clear(); SoundEngine.EndGameSound(); ConsoleAction.PrintOnPos("GAME OVER !!!", 35, 20, ConsoleColor.Red); ConsoleAction.PrintOnPos(string.Format("Your Score: {0} points",player.CurrentScore ), 35, 24, ConsoleColor.Green); ConsoleAction.PrintOnPos("Press Esc or Enter key to continue", 35, 28, ConsoleColor.White); player.SetScore(player.CurrentScore, (byte)levelNumber); ProfileManager.WriteToFile(player); SetDefaults(player); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.Escape || pressedKey.Key == ConsoleKey.Enter) { Console.Clear(); Menu.LoadMainMenu(player); } } } }
public static void SetDefaults(PlayerProfile profile) { profile.PersonalTank.SetDefaultValues(); profile.ResetCurrentScore(); }
public static void ManageShotsAndLevelObject(List<CannonBall> shots, List<LevelObject> targets, PlayerProfile player) { foreach (var shot in shots) { foreach (var target in targets) { if (IsShotPositionEqualLevelObject(shot, target)) { if (!(target is IReprintable)) { SoundEngine.HitSound(); } if (target is IDestroyable) { int shotPower = shot.ShootPower; int targetHealth = (target as IDestroyable).Health; shot.LooseHealth(targetHealth); (target as IDestroyable).LooseHealth(shotPower); } else if (target is IHitable) { shot.LooseHealth(shot.ShootPower); } if (shot.IsFiredFromPlayer) { if (target as EnemyTank != null) { if ((target as EnemyTank).IsDestroyed) { player.AddScore(100); } } else if (target as BrickWall != null) { if ((target as BrickWall).IsDestroyed) { player.AddScore(25); } } else if (target as SteelWall != null) { if ((target as SteelWall).IsDestroyed) { player.AddScore(40); } } } } } } }
private static void VisualiseHightScores(PlayerProfile currProfile) { string line = ""; for (byte i = 0; i < Level.NumberOfLevels(); i++) { if (i < currProfile.BestScores.Count) { line = string.Format("Level {0} : {1} points", i+1, currProfile.GetLevelScore((byte)(i+1))); } else { line = string.Format("Level {0} : Not played", i+1); } ConsoleAction.PrintOnPos(line, 30, 20 + (i * 2), ConsoleColor.Green); } ConsoleAction.PrintOnPos("Enter any key to return", 30, 20 + (Level.NumberOfLevels() * 2), ConsoleColor.Red); }
private static void LoadLevelsMenu(PlayerProfile profile) { List<MenuItem> levelMenuItems = new List<MenuItem>(); for (int i = 0; i < Level.NumberOfLevels(); i++) { levelMenuItems.Add(new MenuItem(30, 20 + (i * 2), string.Format("Level {0}", i + 1), false)); } levelMenuItems[0].Selected = true; string selectedLevelCommand = LoadCommand(levelMenuItems); int selectedLevel = 1; foreach (var item in levelMenuItems) { if (item.Name == selectedLevelCommand) { Console.Clear(); Engine.StartGame(selectedLevel, profile); } selectedLevel++; } }
public static PlayerProfile LoadProfileMenu() { List<MenuItem> menuItems = new List<MenuItem>() { new MenuItem(30, 18, "Create Profile", true), new MenuItem(30, 23, "Load Profile", false) }; string command = LoadCommand(menuItems); if (command == "Create Profile") { Console.Clear(); PrintLogo(); sbyte profileNumber = ProfileManager.GetEmptySloth(); if (profileNumber < 0) { ConsoleAction.PrintOnPos("No empty sloth! Choose a profile to replace it!", 30, 18, ConsoleColor.Red); List<MenuItem> profileItems = ProfileManager.GetProfilesAsMenu(); string deleteCommand = LoadCommand(profileItems); sbyte position = 0; foreach (var item in profileItems) { if (deleteCommand == item.Name) { break; } position++; } //PlayerProfile.DeleteProfile(position); profileNumber = position; } ConsoleAction.PrintOnPos("Profile name: ", 30, 17, ConsoleColor.Green); string name = Console.ReadLine(); while (name.Length > 10 || name.Length < 3) { ConsoleAction.Clear(30, 17, 30, 2); ConsoleAction.PrintOnPos("Name must has 3-10 symbols: ", 30, 17, ConsoleColor.Red); ConsoleAction.PrintOnPos("Profile name: ", 30, 18, ConsoleColor.Green); name = Console.ReadLine(); } ConsoleAction.PrintOnPos("Choose tank color: ", 30, 19, ConsoleColor.Green); //TO DO : Check for same name ConsoleColor color = LoadColorsMenu(); PlayerProfile player = new PlayerProfile(name, new Dictionary<byte, ulong>(), new PlayerTank(color), profileNumber); ProfileManager.WriteToFile(player); LoadMainMenu(player); } else if (command == "Load Profile") { Console.Clear(); PrintLogo(); List<MenuItem> profileItems = ProfileManager.GetProfilesAsMenu(); string selectedCommand = LoadCommand(profileItems); if (selectedCommand == "return") { LoadProfileMenu(); } sbyte position = 0; foreach (var item in profileItems) { if (selectedCommand == item.Name) { LoadMainMenu(ProfileManager.LoadProfile(position)); } position++; } } return new PlayerProfile("guest", new Dictionary<byte, ulong>(), new PlayerTank(), 9); }