/// <summary> /// Starts the game, prompting the user to enter player 1 and 2 names and select color. /// </summary> private static void Initialize_Game_PVAI() { bool Replay = true; Console.Title = "Stick Fight - Player versus Artificial Intelligence"; Console.Out.WriteLine("Enter Player 1 name."); string plr1Name = Console.ReadLine(); while (plr1Name == "") { Console.Out.WriteLine("You need a name."); plr1Name = Console.ReadLine(); } #region Player 1 Color Selecting ConsoleColor Plr1Color; #region Player 1 color listing and selection var colorName1 = new List <string>(); var colorConsole1 = new List <ConsoleColor>(); foreach (ConsoleColor item in Enum.GetValues(typeof(ConsoleColor))) { string colorName = item.ToString().ToLower(); if (!colorName.Contains("black")) { colorName1.Add(item.ToString()); colorConsole1.Add(item); } } #endregion Console.Clear(); if ( !Enum.TryParse( ConsoleOptions.SendChoices("Select " + plr1Name + "'s Color", colorName1, colorConsole1), out Plr1Color)) { throw new Exception("Error on converting player 1's color"); } #endregion #region Player 2 Color Selecting List <ConsoleColor> colors = Enum.GetValues(typeof(ConsoleColor)).Cast <ConsoleColor>().Where(item => item != Plr1Color && item != ConsoleColor.Black).ToList(); ConsoleColor plr2Color = colors[new Random().Next(0, colors.Count)]; Console.Clear(); #endregion #region Handles Map Selection if (!Directory.Exists("Maps")) { Directory.CreateDirectory("Maps"); } List <string> mapPaths = Directory.GetFiles("Maps").ToList(); if (mapPaths.Count == 0) { File.WriteAllText(@"Maps\Map_00.txt", Settings.Default.StandardMap); mapPaths = Directory.GetFiles("Maps").ToList(); } for (int i = 0; i < mapPaths.Count; i++) { mapPaths[i] = mapPaths[i].Replace(@"Maps\", ""); mapPaths[i] = mapPaths[i].Replace(".txt", ""); } string chosenMap = @"Maps\" + ConsoleOptions.SendChoices("Choose your Arena", mapPaths) + ".txt"; #endregion Console.Out.WriteLine("Game Starting in 3 seconds"); Thread.Sleep(3000); Player.EffectPlayer = new List <Effect> { new Effect(Resources.Block), new Effect(Resources.Punch_1), new Effect(Resources.Punch_2), new Effect(Resources.Punch_3) }; Music = new BackgroundMusic("BackgroundMusic", Resources.Background_Theme, true); #region Controls the actual game process. while (Replay) { try { Player1 = new Player( plr1Name, Plr1Color, new Point(20, 20 + Offset)) { HpBarPosition = 1 }; Player2 = new Player( "Computer AI", plr2Color, new Point(60, 20 + Offset)) { HpBarPosition = 59 }; AiCombat = new AiCombat(Player2); //P1Status = new StatusBar(new Point(1, 31), 30, true); //P2Status = new StatusBar(new Point(79, 31), 30, false); GameWon = false; Console.CursorVisible = false; Map.CreateMap(chosenMap); Player1.UpdateAnim(); Player2.UpdateAnim(); #region Countdown to start. Thread.Sleep(1000); SetHeaderMessage("Ready"); Thread.Sleep(1000); SetHeaderMessage("Set!"); if (!Music.IsPlaying) { Music.StartMusic(); } Thread.Sleep(1000); SetHeaderMessage("GO!"); #endregion #region The actual main loop, checking for user input, controlling the players according to the buttons pressed. int plr1Left = Settings.Default.Plr1Left; int plr1Right = Settings.Default.Plr1Right; int plr1Down = Settings.Default.Plr1Down; int plr1Up = Settings.Default.Plr1Up; int plr1Hit = Settings.Default.Plr1Hit; int plr1Kick = Settings.Default.Plr1Kick; int plr1Block = Settings.Default.Plr1Block; while (!GameWon) { if (NativeKeyboard.IsKeyDown(plr1Left)) { Player1.Move(Direction.Left); } else if (NativeKeyboard.IsKeyDown(plr1Right)) { Player1.Move(Direction.Right); } else if (NativeKeyboard.IsKeyDown(plr1Down)) { Player1.Move(Direction.Down); } if (NativeKeyboard.IsKeyDown(plr1Up)) { Player1.Jump(); } if (NativeKeyboard.IsKeyDown(plr1Hit)) { Player1.Hit(); } if (NativeKeyboard.IsKeyDown(plr1Kick)) { Player1.Kick(); } if (NativeKeyboard.IsKeyDown(plr1Block)) { Player1.Block(); } Player1.UpdateMovement(); AiCombat.Update(Player1); Player2.UpdateMovement(); GameWon = CheckForWinner(); Thread.Sleep(30); } #endregion #region After game ends, moves the player models back to default position. for (int i = Player1.Collision.X; i < Player1.Collision.X + 5; i++) { for (int u = Player1.Collision.Y; u < Player1.Collision.Y + 5; u++) { Console.SetCursorPosition(i, u); Console.Out.Write(" "); } } for (int i = Player2.Collision.X; i < Player2.Collision.X + 5; i++) { for (int u = Player2.Collision.Y; u < Player2.Collision.Y + 5; u++) { Console.SetCursorPosition(i, u); Console.Out.Write(" "); } } Player1.Collision = new Rectangle(20, 20 + Offset, 5, 5); Player2.Collision = new Rectangle(60, 20 + Offset, 5, 5); Player1.UpdateAnim(); Player2.UpdateAnim(); #endregion if (Player1.Health == 0 && Player2.Health == 0) { SetHeaderMessage("It is tied!"); } else if (Player1.Health == 0) { SetHeaderMessage(Player2.Name + " has won the Game!"); } else if (Player2.Health == 0) { SetHeaderMessage(Player1.Name + " has won the Game!"); } else { SetHeaderMessage("Unknown Player has won the Game!"); } Thread.Sleep(3000); // Sleeps to allow for the winning to be displayed. } catch (Exception e) { Debug.WriteLine(e.ToString()); Console.ReadLine(); Environment.Exit(0); } SetHeaderMessage("Play Again? Y/N"); while (true) { if (NativeKeyboard.IsKeyDown(Convert.ToInt32('Y'))) { Replay = true; break; } else if (NativeKeyboard.IsKeyDown(Convert.ToInt32('N'))) { Replay = false; break; } Thread.Sleep(10); } } #endregion }
/// <summary> /// Starts the game, prompting the user to enter player 1 and 2 names and select color. /// </summary> private static void Initialize_Game_PVAI() { bool Replay = true; Console.Title = "Stick Fight - Player versus Artificial Intelligence"; Console.Out.WriteLine("Enter Player 1 name."); string plr1Name = Console.ReadLine(); while (plr1Name == "") { Console.Out.WriteLine("You need a name."); plr1Name = Console.ReadLine(); } #region Player 1 Color Selecting ConsoleColor Plr1Color; #region Player 1 color listing and selection var colorName1 = new List<string>(); var colorConsole1 = new List<ConsoleColor>(); foreach (ConsoleColor item in Enum.GetValues(typeof(ConsoleColor))) { string colorName = item.ToString().ToLower(); if (!colorName.Contains("black")) { colorName1.Add(item.ToString()); colorConsole1.Add(item); } } #endregion Console.Clear(); if ( !Enum.TryParse( ConsoleOptions.SendChoices("Select " + plr1Name + "'s Color", colorName1, colorConsole1), out Plr1Color)) throw new Exception("Error on converting player 1's color"); #endregion #region Player 2 Color Selecting List<ConsoleColor> colors = Enum.GetValues(typeof (ConsoleColor)).Cast<ConsoleColor>().Where(item => item != Plr1Color && item != ConsoleColor.Black).ToList(); ConsoleColor plr2Color = colors[new Random().Next(0, colors.Count)]; Console.Clear(); #endregion #region Handles Map Selection if (!Directory.Exists("Maps")) Directory.CreateDirectory("Maps"); List<string> mapPaths = Directory.GetFiles("Maps").ToList(); if (mapPaths.Count == 0) { File.WriteAllText(@"Maps\Map_00.txt", Settings.Default.StandardMap); mapPaths = Directory.GetFiles("Maps").ToList(); } for (int i = 0; i < mapPaths.Count; i++) { mapPaths[i] = mapPaths[i].Replace(@"Maps\", ""); mapPaths[i] = mapPaths[i].Replace(".txt", ""); } string chosenMap = @"Maps\" + ConsoleOptions.SendChoices("Choose your Arena", mapPaths) + ".txt"; #endregion Console.Out.WriteLine("Game Starting in 3 seconds"); Thread.Sleep(3000); Player.EffectPlayer = new List<Effect> { new Effect(Resources.Block), new Effect(Resources.Punch_1), new Effect(Resources.Punch_2), new Effect(Resources.Punch_3) }; Music = new BackgroundMusic("BackgroundMusic", Resources.Background_Theme, true); #region Controls the actual game process. while (Replay) { try { Player1 = new Player( plr1Name, Plr1Color, new Point(20, 20 + Offset)) { HpBarPosition = 1 }; Player2 = new Player( "Computer AI", plr2Color, new Point(60, 20 + Offset)) { HpBarPosition = 59 }; AiCombat = new AiCombat(Player2); //P1Status = new StatusBar(new Point(1, 31), 30, true); //P2Status = new StatusBar(new Point(79, 31), 30, false); GameWon = false; Console.CursorVisible = false; Map.CreateMap(chosenMap); Player1.UpdateAnim(); Player2.UpdateAnim(); #region Countdown to start. Thread.Sleep(1000); SetHeaderMessage("Ready"); Thread.Sleep(1000); SetHeaderMessage("Set!"); if (!Music.IsPlaying) Music.StartMusic(); Thread.Sleep(1000); SetHeaderMessage("GO!"); #endregion #region The actual main loop, checking for user input, controlling the players according to the buttons pressed. int plr1Left = Settings.Default.Plr1Left; int plr1Right = Settings.Default.Plr1Right; int plr1Down = Settings.Default.Plr1Down; int plr1Up = Settings.Default.Plr1Up; int plr1Hit = Settings.Default.Plr1Hit; int plr1Kick = Settings.Default.Plr1Kick; int plr1Block = Settings.Default.Plr1Block; while (!GameWon) { if (NativeKeyboard.IsKeyDown(plr1Left)) Player1.Move(Direction.Left); else if (NativeKeyboard.IsKeyDown(plr1Right)) Player1.Move(Direction.Right); else if (NativeKeyboard.IsKeyDown(plr1Down)) Player1.Move(Direction.Down); if (NativeKeyboard.IsKeyDown(plr1Up)) Player1.Jump(); if (NativeKeyboard.IsKeyDown(plr1Hit)) Player1.Hit(); if (NativeKeyboard.IsKeyDown(plr1Kick)) Player1.Kick(); if (NativeKeyboard.IsKeyDown(plr1Block)) Player1.Block(); Player1.UpdateMovement(); AiCombat.Update(Player1); Player2.UpdateMovement(); GameWon = CheckForWinner(); Thread.Sleep(30); } #endregion #region After game ends, moves the player models back to default position. for (int i = Player1.Collision.X; i < Player1.Collision.X + 5; i++) { for (int u = Player1.Collision.Y; u < Player1.Collision.Y + 5; u++) { Console.SetCursorPosition(i, u); Console.Out.Write(" "); } } for (int i = Player2.Collision.X; i < Player2.Collision.X + 5; i++) { for (int u = Player2.Collision.Y; u < Player2.Collision.Y + 5; u++) { Console.SetCursorPosition(i, u); Console.Out.Write(" "); } } Player1.Collision = new Rectangle(20, 20 + Offset, 5, 5); Player2.Collision = new Rectangle(60, 20 + Offset, 5, 5); Player1.UpdateAnim(); Player2.UpdateAnim(); #endregion if (Player1.Health == 0 && Player2.Health == 0) { SetHeaderMessage("It is tied!"); } else if (Player1.Health == 0) { SetHeaderMessage(Player2.Name + " has won the Game!"); } else if (Player2.Health == 0) { SetHeaderMessage(Player1.Name + " has won the Game!"); } else { SetHeaderMessage("Unknown Player has won the Game!"); } Thread.Sleep(3000); // Sleeps to allow for the winning to be displayed. } catch (Exception e) { Debug.WriteLine(e.ToString()); Console.ReadLine(); Environment.Exit(0); } SetHeaderMessage("Play Again? Y/N"); while (true) { if (NativeKeyboard.IsKeyDown(Convert.ToInt32('Y'))) { Replay = true; break; } else if (NativeKeyboard.IsKeyDown(Convert.ToInt32('N'))) { Replay = false; break; } Thread.Sleep(10); } } #endregion }