public bool CanTankMove(Tank tank, List<Tank> enemies, Directions direction) { NewPosition newTankPosition = new NewPosition(tank, direction); bool collides = false; if (newTankPosition.Row >= 2 && newTankPosition.Row < Data.GetWindowHeight() - 4 && newTankPosition.Column >= 0 && newTankPosition.Column < Data.GetWindowWidth() - 3) { collides = true; } for (int elemRow = 0; elemRow < mapElements.GetLength(0); elemRow++) { for (int elemCol = 0; elemCol < mapElements.GetLength(1); elemCol++) { if (mapElements[elemRow, elemCol] != null) { if (DoesItCollideTank(mapElements[elemRow, elemCol], tank, newTankPosition.Row, newTankPosition.Column)) { collides = false; } } } } if (collides) { collides = TankIsOnAtherTank(tank, enemies, newTankPosition); } return collides; }
public static Fire Shoot(Tank player, Tank enemy, Random rand) { int shotChance = rand.Next(1, 101); Fire fire = null; if (shotChance < 13) { fire = new Fire(enemy); } return fire; }
public void DeleteTank(Tank tank) { Console.BackgroundColor = ConsoleColor.White; string str = new string(' ', tank.Dimention); Console.CursorVisible = false; for (int row = 0; row < tank.Dimention; row++) { for (int col = 0; col < tank.Dimention; col++) { Console.SetCursorPosition(tank.OldCol + col, tank.OldRow + row); Console.Write(str[col]); } } }
private static Directions MoveToPlayer(Tank player, Tank enemy, Random rand) { List<Directions> directionsToChooseFrom = new List<Directions>(); if (player.CurrentCol < enemy.CurrentCol) directionsToChooseFrom.Add(Directions.Left); else if(player.CurrentCol > enemy.CurrentCol) directionsToChooseFrom.Add(Directions.Right); if (player.CurrentRow < enemy.CurrentRow) directionsToChooseFrom.Add(Directions.Up); else if (player.CurrentRow > enemy.CurrentRow) directionsToChooseFrom.Add(Directions.Down); return directionsToChooseFrom[rand.Next(0, directionsToChooseFrom.Count)]; }
public static Directions Move(Tank player, Tank enemy, Random rand) { Directions direction = Directions.Up; int chance = rand.Next(1, 101); int numberDirection = rand.Next(0, 4); if (chance < 10) { direction = MoveToPlayer(player, enemy, rand); } else if (chance < 20) { direction += numberDirection; } else { direction = enemy.Direction; } return direction; }
public Fire(Tank tank) { this.IsPlayerFire = tank.IsPlayr; this.Direction = tank.Direction; this.Color = ConsoleColor.Black; if (Direction == Directions.Down) { this.StartRow = tank.CurrentRow + tank.Dimention; this.StartCol = tank.CurrentCol + (tank.Dimention / 2); this.Symbol = '|'; } if (Direction == Directions.Right) { this.StartRow = tank.CurrentRow + (tank.Dimention / 2); this.StartCol = tank.CurrentCol + tank.Dimention; this.Symbol = '—'; } if (Direction == Directions.Up) { this.StartRow = tank.CurrentRow - 1; this.StartCol = tank.CurrentCol + (tank.Dimention / 2); this.Symbol = '|'; } if (Direction == Directions.Left) { this.StartRow = tank.CurrentRow + (tank.Dimention / 2); this.StartCol = tank.CurrentCol - 1; this.Symbol = '—'; } OldRow = StartRow; OldCol = StartCol; CurrentRow = StartRow; CurrentCol = StartCol; }
private void CreateEnemy() { if (createEnemyCallCount < 15) { createEnemyCallCount++; } int maxEnemyOnMap = 6; if (createEnemyCallCount > 10 && this.enemys.Count <= maxEnemyOnMap && this.enemyOnLevel > 0) { createEnemyCallCount = 0; int createdChance = rand.Next(1, 101); if (createdChance < 30) { int positionNumber = rand.Next(0, 3); this.enemyNumber++; Tank enemy = new Tank(false, startEnemyPositions[positionNumber].Row, startEnemyPositions[positionNumber].Column, 'E', this.enemyNumber); if (this.collision.CanTankMove(enemy, this.enemys, Directions.No)) { this.enemyOnLevel--; enemys.Add(enemy); render.Tank(enemy); } else { /// } } } }
public NewPosition(Tank tank, Directions direction) { this.Row = tank.CurrentRow; this.Column = tank.CurrentCol; ChangeCordinate(direction); }
private bool TankIsOnAtherTank(Tank tank, List<Tank> enemies, NewPosition newTankPosition) { bool collides = true; for (int i = 0; i < enemies.Count; i++) { if (tank.TankNumber != enemies[i].TankNumber) { if (IsTankColaidsAtherTank(newTankPosition, enemies[i])) { collides = false; break; } } } return collides; }
public void Tank(Tank tank) { DeleteTank(tank); switch (tank.Direction) { case Directions.Up: DrowTankUp(tank); break; case Directions.Down: DrowTankDown(tank); break; case Directions.Left: DrowTankLeft(tank); break; case Directions.Right: DrowTankRight(tank); break; default: break; } }
private void ShowInformation(Tank player) { if (this.oldenemyOnLevel != this.enemyOnLevel || this.oldScore != this.score || this.oldPlayerLives != player.Lives) { string info = String.Format("LIVES: {0} SCORE: {1,7} ENEMY to create: {2,2}", player.Lives, this.score, this.enemyOnLevel); this.render.MenuItem(0, 1, info, ConsoleColor.Yellow); this.oldenemyOnLevel = this.enemyOnLevel; this.oldScore = this.score; this.oldPlayerLives = player.Lives; } }
private void MovePlayesMisales(Tank player) { // enemy fires move List<Fire> misalesFire = new List<Fire>(); foreach (Fire misale in this.playerMisals) { if (!collision.HaveShotCollision(misale, player, this.enemys)) { misale.Move(); this.render.Misale(misale); //draw missile misalesFire.Add(misale); } else { misale.Move(); // if have collision delete missile from Console try { this.render.DeleteMisale(misale); } catch (ArgumentOutOfRangeException) { // missile i out of map don't delete it } for (int i = 0; i < this.enemys.Count; i++) { if (this.enemys[i].IsHited) { this.enemys[i].DecreaseLives(); this.enemys[i].IsHited = false; this.render.Tank(this.enemys[i]); if (this.enemys[i].Lives <= 0) { Console.Beep(5500, 40); this.score += this.enemys[i].Score; this.enemys[i].SetDefitedCordinates(); render.DeleteTank(this.enemys[i]); this.enemys.Remove(this.enemys[i]); } } } int eagleRow = this.level.GetLength(0) - 1; int eagleCol = (this.level.GetLength(1) - 1) / 2; // if there is no eagle set player live = 0 and this will make game over if (this.level[eagleRow, eagleCol] == null) { while (player.Lives > 0) { player.DecreaseLives(); } return; } } } this.playerMisals = misalesFire; }
public bool DoesItCollideTank(Element elemPos, Tank tank, int tankRow, int tankCol) { bool collidesDoes = false; for (int t = -1; t < 2; t++) { for (int k = -1; k < 2; k++) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if ((elemPos.Row + i == tankRow + t) && (elemPos.Column + j == tankCol + k)) { collidesDoes = true; } } } } } return collidesDoes; }
private void DrowTankUp(Tank tank) { Console.ForegroundColor = tank.Color; Console.BackgroundColor = ConsoleColor.White; string str = new string(tank.Sumbol, tank.Dimention); for (int row = 0; row < tank.Dimention; row++) { for (int col = 0; col < tank.Dimention; col++) { if (row == 0 && col == (tank.Dimention) / 2) { Console.ForegroundColor = ConsoleColor.Black; Console.SetCursorPosition(tank.CurrentCol + col, tank.CurrentRow + row); Console.Write(str[col]); Console.ForegroundColor = tank.Color; } else { Console.SetCursorPosition(tank.CurrentCol + col, tank.CurrentRow + row); Console.Write(str[col]); } } } Console.ResetColor(); }
private void EnemyPlay(Tank player) { this.enemyPlayCount++; if (enemyPlayCount > 4) { this.enemyPlayCount = 0; Directions directin = Directions.No; bool isValidDirection = false; // enemy Move and create missile foreach (Tank enemy in this.enemys) { // create misale Fire fire = EnemyAI.Shoot(player, enemy, this.rand); if (fire != null && !enemy.IsPlayr) { if (this.collision.CanFireMove(fire.CurrentRow, fire.CurrentCol)) { this.enemyMisals.Add(fire); } } // move directin = EnemyAI.Move(player, enemy, this.rand); isValidDirection = this.collision.CanTankMove(enemy, this.enemys, directin); if (isValidDirection) { List<Tank> playerList = new List<Tank>(); playerList.Add(player); isValidDirection = this.collision.CanTankMove(enemy, playerList, directin); } if (isValidDirection && !enemy.IsPlayr) { enemy.Move(directin); this.render.Tank(enemy); } } } }
private void MoveEnemyMisales(Tank player) { // enemy fires move List<Fire> misales = new List<Fire>(); foreach (Fire misale in this.enemyMisals) { if (!collision.HaveShotCollision(misale, player, this.enemys)) { misale.Move(); this.render.Misale(misale); misales.Add(misale); for (int i = 0; i < this.enemys.Count; i++) { if (this.collision.DoesItCollideFire(new Element(ElementType.Braket, enemys[i].CurrentRow, enemys[i].CurrentCol), misale.CurrentRow, misale.CurrentCol)) { this.render.Tank(enemys[i]); } } } else if (player.IsHited) { // if have collision delete missile from Console misale.Move(); try { this.render.DeleteMisale(misale); } catch (ArgumentOutOfRangeException) { // missile is out of map don't delete it } Console.Beep(280, 200); Console.Beep(480, 200); Console.Beep(680, 100); player.DecreaseLives(); player.IsHited = false; player.SetDefitedCordinates(); this.render.DeleteTank(player); if (player.Lives > 0) { player.MakeStartPosition(); render.Tank(player); } } else { misale.Move(); // if have collision delete misale from Console try { this.render.DeleteMisale(misale); } catch (ArgumentOutOfRangeException) { // missale i out of map donot delete it } int eagleRow = this.level.GetLength(0) - 1; int eagleCol = (this.level.GetLength(1) - 1) / 2; // if there is no eagle set player live = 0 and this will make game over if (this.level[eagleRow, eagleCol] == null) { while (player.Lives > 0) { player.DecreaseLives(); } return; } } } this.enemyMisals = misales; }
public bool HaveShotCollision(Fire fire, Tank player, List<Tank> enemies) { // if have collision return true // else return false - misale continue move // Gencho make simple collision NewPosition newFirePosition = new NewPosition(fire); if (!CanFireMove(newFirePosition.Row, newFirePosition.Column)) { return true; } if (newFirePosition.Row >= 2 && newFirePosition.Row < Data.GetWindowHeight() - 2 && newFirePosition.Column >= 0 && newFirePosition.Column < Data.GetWindowWidth() - 1) { // enemy fire if (!fire.IsPlayerFire) { if (IsMisaleHitTank(player, newFirePosition)) { player.IsHited = true; return true; } } else { bool haveHit = false; for (int i = 0; i < enemies.Count; i++) { if (IsMisaleHitTank(enemies[i], newFirePosition)) { enemies[i].IsHited = true; haveHit = true; } } if (haveHit) { return true; } } return false; } return true; //if (IsFireAgainstEnemy(fire)) //{ //} //if (IsFireAgainstPlayer(fire)) //{ //} //if (IsFireAgainstTerain(fire)) //{ //} }
private void PlayerMove(Tank player) { Directions newDirection = Directions.Up; newDirection = ReadDirectionAndFire(); if (newDirection != Directions.No) { if (this.collision.CanTankMove(player, this.enemys, newDirection)) { Console.Beep(100, 30); player.Move(newDirection); render.Tank(player); } else { player.Direction = newDirection; this.render.Tank(player); } } }
private static bool IsMisaleHitTank(Tank player, NewPosition newFirePosition) { return newFirePosition.Row >= player.CurrentRow && newFirePosition.Row <= (player.CurrentRow + Data.GetElementDimention() - 1) && newFirePosition.Column >= player.CurrentCol && newFirePosition.Column <= (player.CurrentCol + Data.GetElementDimention() - 1); }
public FinishGameMessage Start() { FinishGameMessage finishMessage = new FinishGameMessage(); render.Level(this.level); int playerStartRow = Data.GetWindowHeight() - 2 * Data.GetElementDimention(); int playerStartCol = (Data.GetWindowWidth() / 2) - 3 * Data.GetElementDimention(); Tank player = new Tank(true, playerStartRow, playerStartCol); render.Tank(player); int moveEnemyMisalesCount = 0; int playrMoveCount = 0; long playrFireCount = 0; this.oldPlayerLives = player.Lives; this.oldScore = 0; this.oldenemyOnLevel = this.enemyOnLevel; while (!isExit) { ShowInformation(player); moveEnemyMisalesCount++; if (moveEnemyMisalesCount > 1) { moveEnemyMisalesCount = 0; MoveEnemyMisales(player); } MovePlayesMisales(player); if (CheckPlayerWin()) { isExit = true; finishMessage.Score = this.score; finishMessage.IsGameOver = false; finishMessage.Message = "Player WIN no enemy left"; } else { playrMoveCount++; if (playrMoveCount > 1) { playrMoveCount = 0; PlayerMove(player); } // player fire playrFireCount++; if (havePlayerFire && playrFireCount > 10) { playrFireCount = 0; havePlayerFire = false; this.playerMisals.Add(new Fire(player)); // when shot move it draw. } CreateEnemy(); EnemyPlay(player); if (player.Lives <= 0) { isExit = true; finishMessage.Score = this.score; finishMessage.IsGameOver = true; finishMessage.Message = "lives player = 0"; } Thread.Sleep(10); } } if (this.isEscapePressed) { finishMessage.Score = -1; finishMessage.IsGameOver = true; finishMessage.Message = "Escape pressed"; } return finishMessage; }
private bool IsTankColaidsAtherTank(NewPosition newTankPosition, Tank tank) { for (int row = newTankPosition.Row; row < newTankPosition.Row + Data.GetElementDimention(); row++) { for (int col = newTankPosition.Column; col < newTankPosition.Column + Data.GetElementDimention(); col++) { if (row >= tank.CurrentRow && row < tank.CurrentRow + tank.Dimention && col >= tank.CurrentCol && col < tank.CurrentCol + tank.Dimention) { return true; } } } return false; }