/// <summary> /// Takes an array of enemies, a game area and a Random class instance as arguments, and moves the enemies’ coordinates /// depending on their EmergingSide. Returns void. /// </summary> /// <param name="enemies"></param> /// <param name="area"></param> /// <param name="rnd"></param> public static void MoveEnemies(Enemy[] enemies, GameArea area, Random rnd) { if (CheckIfAtEnd(enemies, area)) { DeleteWaveAtEnd(enemies, area); ChangeSide(enemies, area, rnd); } else { for (int i = 0; i < enemies.Length; i++) { switch (enemies[0].EmergingSide) { case Sides.Top: enemies[i].EnemyYPos++; break; case Sides.Right: enemies[i].EnemyXPos--; break; case Sides.Bottom: enemies[i].EnemyYPos--; break; case Sides.Left: enemies[i].EnemyXPos++; break; } //end of switch } //end of loop } } //end of method
/// <summary> /// Takes an array of enemies, a game area and a Random class instance as arguments, and changes the emerging side of the /// enemies once its wave is complete by setting its initial position to a randomly generated side. Returns void. /// </summary> /// <param name="enemies"></param> /// <param name="area"></param> /// <param name="rnd"></param> public static void ChangeSide(Enemy[] enemies, GameArea area, Random rnd) { enemies[0].EmergingSide = (Sides)rnd.Next((int)Sides.Top, (int)Sides.Left + 1); //Generates a number from 1-4 representing the side the enemies will scroll across the screen from for (int i = 0; i < enemies.Length; i++) { switch (enemies[0].EmergingSide) { case Sides.Top: enemies[i].EnemyXPos = rnd.Next(area.BorderWidth, area.Width + area.BorderWidth); enemies[i].EnemyYPos = 1; break; case Sides.Right: enemies[i].EnemyXPos = area.Width + area.BorderWidth - 1; enemies[i].EnemyYPos = rnd.Next(1, area.Height - 1); break; case Sides.Bottom: enemies[i].EnemyXPos = rnd.Next(area.BorderWidth, area.Width + area.BorderWidth); enemies[i].EnemyYPos = area.Height - 2; break; case Sides.Left: enemies[i].EnemyXPos = area.BorderWidth; enemies[i].EnemyYPos = rnd.Next(1, area.Height - 1); break; } } }
/// <summary> /// Overload takes the number of coins, a Random class instance, the game’s play area and the player object, /// a coin character along with a coin foreground and background color. Generates an array of coins of the /// specified size numberOfCoins. All of the coins are initialized using the triple-parameter constructor. /// Ensures no coins spawn on top of the player. Returns the array. /// </summary> /// <param name="numberOfCoins"></param> /// <param name="rnd"></param> /// <param name="area"></param> /// <param name="player"></param> /// <param name="coinChar"></param> /// <param name="coinForeColor"></param> /// <param name="coinBackColor"></param> /// <returns></returns> public static Coin[] GenerateCoinArray(int numberOfCoins, Random rnd, GameArea area, Player player, char coinChar, ConsoleColor coinForeColor, ConsoleColor coinBackColor) { Coin[] coins = new Coin[numberOfCoins]; int tempX; int tempY; for (int i = 0; i < coins.Length; i++) { coins[i] = new Coin(coinChar, coinForeColor, coinBackColor); } coins[0].CoinXPos = rnd.Next(area.LeftLimit, area.RightLimit + 1); coins[0].CoinYPos = rnd.Next(area.UpLimit, area.DownLimit + 1); for (int i = 0; i < coins.Length; i++) { tempX = rnd.Next(area.LeftLimit, area.RightLimit + 1); tempY = rnd.Next(area.UpLimit, area.DownLimit + 1); for (int j = 0; j < i; j++) { //ensures coins dont spawn on top of each other //ensures coins dont spawn on top of the player if ((tempX != coins[j].CoinXPos && tempY != coins[j].CoinYPos) && (tempY != player.XPos && tempY != player.YPos)) { coins[i].CoinXPos = tempX; coins[i].CoinYPos = tempY; } else { i--; } } } return(coins); }
/// <summary> /// Takes the user’s key press and the current game area as arguments, and moves the player by its /// speed in the direction corresponding to the key press. Collision with the outer game area limits /// is checked here too. Returns void. /// </summary> /// <param name="userKeyPress"></param> /// <param name="area"></param> public void Move(ConsoleKey userKeyPress, GameArea area) { OldXPos = XPos; OldYPos = YPos; //move player up if (userKeyPress == KeyUp && YPos != area.UpLimit) //up = decrease Y coord { YPos -= Speed; } //move player down if (userKeyPress == KeyDown && YPos != area.DownLimit) //down = increase Y coord { YPos += Speed; } //move player left if (userKeyPress == KeyLeft && XPos != area.LeftLimit) //left = decrease X coord { XPos -= Speed; } //move player right if (userKeyPress == KeyRight && XPos != area.RightLimit) //right = increase X coord { XPos += Speed; } }
} //end of method /// <summary> /// Takes an array of enemies, a Player object instance, a GameArea instance, a Coin array and a Random /// class instance as arguments. Erases previous enemy icons after the enemies have moved and re-draws them as well as /// the coins so their icons do not disappear. Returns void. /// </summary> /// <param name="enemies"></param> /// <param name="player"></param> /// <param name="area"></param> /// <param name="coins"></param> /// <param name="rnd"></param> public static void Render(Enemy[] enemies, Player player, GameArea area, Coin[] coins, Random rnd) { for (int i = 0; i < enemies.Length; i++) { ForegroundColor = area.ScreengrassForeColor; BackgroundColor = area.ScreengrassBackColor; switch (enemies[0].EmergingSide) { case Sides.Top: if (enemies[i].EnemyYPos != 1) //if the enemies were not just drawn, erase their previous instance before drawing them in their new position { SetCursorPosition(enemies[i].EnemyXPos, enemies[i].EnemyYPos - 1); WriteLine(area.ScreengrassChar); } break; case Sides.Right: if (enemies[i].EnemyXPos != area.Width + area.BorderWidth - 1) { SetCursorPosition(enemies[i].EnemyXPos + 1, enemies[i].EnemyYPos); WriteLine(area.ScreengrassChar); } break; case Sides.Bottom: if (enemies[i].EnemyYPos != area.Height - 2) { SetCursorPosition(enemies[i].EnemyXPos, enemies[i].EnemyYPos + 1); WriteLine(area.ScreengrassChar); } break; case Sides.Left: if (enemies[i].EnemyXPos != area.BorderWidth) { SetCursorPosition(enemies[i].EnemyXPos - 1, enemies[i].EnemyYPos); WriteLine(area.ScreengrassChar); } break; } } foreach (Coin c in coins) { c.WriteOnScreen(); } foreach (Enemy e in enemies) { ForegroundColor = e.EnemyForeColor; BackgroundColor = e.EnemyBackColor; SetCursorPosition(e.EnemyXPos, e.EnemyYPos); WriteLine(e.EnemyChar); } player.Draw(area); ResetColor(); }
/// <summary> /// Initializes Rnd and Player1 with their default constructors, initializes Area with its dual-parameter/simple /// editor constructor (see GameArea.cs). Generates the number of coins and enemies based on the parameters provided /// (both using their initializer overloads that use default constructors for each object). Difficulty is set using /// the argument provided and Music.SoundLocation is set to its default value. Used only with Simple Editor Mode in the /// Custom Level Editor /// </summary> /// <param name="width_"></param> /// <param name="height_"></param> /// <param name="numberOfCoins_"></param> /// <param name="numberOfEnemies_"></param> /// <param name="difficulty_"></param> public Level(int width_, int height_, int numberOfCoins_, int numberOfEnemies_, int difficulty_) //Simple editor { Rnd = new Random(); Player1 = new Player(); Area = new GameArea(width_, height_); Coins = Coin.GenerateCoinArray(numberOfCoins_, Rnd, Area, Player1); Enemies = Enemy.GetArrayOfEnemies(numberOfEnemies_, Area); Difficulty = difficulty_; Music.SoundLocation = DEFAULT_SOUNDLOCATION; }
private int _difficulty; //The amount of times the game loop needs to run before moving enemies. #region //CONSTRUCTORS /// <summary> /// Takes no arguments. Initializes Rnd, Player1, and Area with their default constructors. Coins and /// Enemies are initialized using their initializer method (GenerateCoinArray, GetArrayOfEnemies) overloads /// that use the default contructor for both objects. Difficulty and Music.SoundLocation are defined using their public constants. /// </summary> public Level() //default settings { Rnd = new Random(); Player1 = new Player(); Area = new GameArea(); Coins = Coin.GenerateCoinArray(DEFAULT_NUMBER_OF_COINS, Rnd, Area, Player1); Enemies = Enemy.GetArrayOfEnemies(DEFAULT_NUMBER_OF_ENEMIES, Area); Difficulty = DEFAULT_DIFFICULTY; Music.SoundLocation = DEFAULT_SOUNDLOCATION; }
/// <summary> /// Takes an array of enemies and a GameArea instance as arguments, and erases all of the enemies once they’ve reached /// the end of their wave and replaces their icons with the screengrass of the GameArea instance passed to it. Returns void. /// </summary> /// <param name="enemies"></param> /// <param name="area"></param> private static void DeleteWaveAtEnd(Enemy[] enemies, GameArea area) { foreach (Enemy e in enemies) { SetCursorPosition(e.EnemyXPos, e.EnemyYPos); ForegroundColor = area.ScreengrassForeColor; BackgroundColor = area.ScreengrassBackColor; WriteLine(area.ScreengrassChar); } }
/// <summary> /// Overload takes an integer representing the amount of enemies to be generated, the game area, an enemy character, and enemy /// foreground/background colors. Creates a new Enemy array and initializes each element with its Advanced Editor Constructor. /// Returns the array. /// </summary> /// <param name="numberOfEnemies"></param> /// <param name="area"></param> /// <param name="enemyChar"></param> /// <param name="enemyForeColor"></param> /// <param name="enemyBackColor"></param> /// <returns></returns> public static Enemy[] GetArrayOfEnemies(int numberOfEnemies, GameArea area, char enemyChar, ConsoleColor enemyForeColor, ConsoleColor enemyBackColor) { Enemy[] enemies = new Enemy[numberOfEnemies]; for (int i = 0; i < enemies.Length; i++) { enemies[i] = new Enemy(enemyChar, enemyForeColor, enemyBackColor); } return(enemies); }
/// <summary> /// Overload takes an integer representing the amount of enemies to be generated and the GameArea they will be used on. Creates a new Enemy /// array and initializes each element with its default constructor. Returns the array. /// </summary> /// <param name="numberOfEnemies"></param> /// <param name="area"></param> /// <returns></returns> public static Enemy[] GetArrayOfEnemies(int numberOfEnemies, GameArea area) { Enemy[] enemies = new Enemy[numberOfEnemies]; for (int i = 0; i < enemies.Length; i++) { enemies[i] = new Enemy(); } return(enemies); }
/// <summary> /// Initializes Rnd with its default constructor. Initializes Player1 and Area using their Advanced Editor /// Constructors. Initializes Coins and Enemies using their initializer overloads that use their advanced editor /// constructors as well. Difficulty and Music.SoundLocation are set to their provided parameters. /// </summary> /// <param name="playerChar_"></param> /// <param name="playerStartingLives_"></param> /// <param name="playerForeColor_"></param> /// <param name="playerBackColor_"></param> /// <param name="screengrassChar_"></param> /// <param name="borderChar_"></param> /// <param name="screengrassForeColor_"></param> /// <param name="screengrassBackColor_"></param> /// <param name="borderForeColor_"></param> /// <param name="borderBackColor_"></param> /// <param name="width_"></param> /// <param name="height_"></param> /// <param name="borderWidth_"></param> /// <param name="coinChar_"></param> /// <param name="coinForeColor_"></param> /// <param name="coinBackColor_"></param> /// <param name="numberOfCoins"></param> /// <param name="numberOfEnemies_"></param> /// <param name="enemyChar_"></param> /// <param name="enemyForeColor_"></param> /// <param name="enemyBackColor_"></param> /// <param name="difficulty_"></param> /// <param name="soundLocation_"></param> public Level(char playerChar_, int playerStartingLives_, ConsoleColor playerForeColor_, ConsoleColor playerBackColor_, //Advanced editor //Player parameters char screengrassChar_, char borderChar_, ConsoleColor screengrassForeColor_, ConsoleColor screengrassBackColor_, ConsoleColor borderForeColor_, ConsoleColor borderBackColor_, int width_, int height_, int borderWidth_, //Game area parameters char coinChar_, ConsoleColor coinForeColor_, ConsoleColor coinBackColor_, int numberOfCoins, //Coin parameters int numberOfEnemies_, char enemyChar_, ConsoleColor enemyForeColor_, ConsoleColor enemyBackColor_, //enemy parameters int difficulty_, string soundLocation_) { Rnd = new Random(); Player1 = new Player(playerChar_, playerStartingLives_, playerForeColor_, playerBackColor_); Area = new GameArea(screengrassChar_, borderChar_, screengrassForeColor_, screengrassBackColor_, borderForeColor_, borderBackColor_, width_, height_, borderWidth_); Coins = Coin.GenerateCoinArray(numberOfCoins, Rnd, Area, Player1, coinChar_, coinForeColor_, coinBackColor_); Enemies = Enemy.GetArrayOfEnemies(numberOfEnemies_, Area, enemyChar_, enemyForeColor_, enemyBackColor_); Difficulty = difficulty_; Music.SoundLocation = soundLocation_; }
/// <summary> /// Takes the current game area as an argument. Erases the old player icon after moving and /// draws the new one with its current X and Y positions. Returns void. /// </summary> /// <param name="area"></param> public void Draw(GameArea area) { if (OldXPos != XPos || OldYPos != YPos) { if (OldXPos != 0 && OldYPos != 0) { SetCursorPosition(OldXPos, OldYPos); ForegroundColor = area.ScreengrassForeColor; BackgroundColor = area.ScreengrassBackColor; WriteLine(area.ScreengrassChar); } SetCursorPosition(XPos, YPos); ForegroundColor = ForeColor; BackgroundColor = BackColor; WriteLine(PlayerChar); ResetColor(); } }
/// <summary> /// Takes an array of enemies and a GameArea instance as arguments, and utilizes a checksum to see if all of the /// enemies have reached their ending position for that specific wave. Returns true if the enemies are at the end of /// the wave, returns false if not. /// </summary> /// <param name="enemies"></param> /// <param name="area"></param> /// <returns></returns> private static bool CheckIfAtEnd(Enemy[] enemies, GameArea area) { int checksum = 0; for (int i = 0; i < enemies.Length; i++) { switch (enemies[0].EmergingSide) { case Sides.Top: if (enemies[i].EnemyYPos == area.Height - 2) { checksum++; } break; case Sides.Right: if (enemies[i].EnemyXPos == area.BorderWidth) { checksum++; } break; case Sides.Bottom: if (enemies[i].EnemyYPos == 1) { checksum++; } break; case Sides.Left: if (enemies[i].EnemyXPos == area.Width + area.BorderWidth - 1) { checksum++; } break; } } return(checksum == enemies.Length); }
/// <summary> /// Takes current game area as an argument and uses its dimensions to initialize the player’s /// position in the center of the play area. Returns void. /// </summary> /// <param name="area"></param> public void InitializePosition(GameArea area) { XPos = (area.BorderWidth * 2 + area.Width) / 2; YPos = area.Height / 2; DrawDirectly(); }