public int Lose(Hero a) { if (this.x == a.x && this.y == a.y) { Console.WriteLine("Zombie " + Name + " ate the human!"); return 1; } else { return 0; } }
public void zombieMove(Hero A, int[,] grid) { int xdirection = -Math.Sign(this.x - A.x); // 4|2 int ydirection = -Math.Sign(this.y - A.y);// 7|2 int xsteps = Math.Abs(this.x - A.x); int ysteps = Math.Abs(this.y - A.y); int maxMove = 3; if (xsteps >= ysteps) { if (maxMove > xsteps) { for (int i = 0; i < xsteps; i++) { this.Move(xdirection, 0, grid); Thread.Sleep(100); } } else { for (int i = 0; i < maxMove; i++) { this.Move(xdirection, 0, grid); Thread.Sleep(100); } } } else { if (maxMove > ysteps) { for (int i = 0; i < ysteps; i++) { this.Move(0, ydirection, grid); Thread.Sleep(100); } } else { for (int i = 0; i < maxMove; i++) { this.Move(0, ydirection, grid); Thread.Sleep(100); } } } }
public void HeroUpgradePickup(Hero A, int[,] grid) { if (this.x == A.x && this.y == A.y) { grid[x, y] = 2; A.HeroUpgrade(this.potency); } }
static void Main(string[] args) { Console.BufferHeight = Console.WindowHeight = 25; Console.BufferWidth = Console.WindowWidth = 80; int[,] matrix = new int[20, 40]; Array.Clear(matrix, 0, matrix.Length); List<Aliens> All = new List<Aliens>(); List<Rock> allRocks = new List<Rock>(); string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Ivko_N76\Desktop\DataBase.txt"); foreach (string value in lines) { string[] temp = value.Split(','); All.Add(new Aliens(int.Parse(temp[0]), int.Parse(temp[1]), temp[2])); } string[] lines1 = System.IO.File.ReadAllLines(@"C:\Users\Ivko_N76\Desktop\Rocks.txt"); foreach (string value in lines1) { string[] temp = value.Split(','); allRocks.Add(new Rock(int.Parse(temp[0]), int.Parse(temp[1]))); } //Read From file Hero c = new Hero(10, 15); c.placeInGrid(matrix); Upgrade up = new Upgrade(10, 16); up.PlaceInGrid(matrix); foreach (Aliens value in All) { value.placeInGrid(matrix); } foreach (Rock value in allRocks) { value.placeInGrid(matrix); } int count = 0; Console.WriteLine(printGrid(matrix)); while (count < 20) { int heromove = c.heroMoves; for (int i = 0; i < heromove; i++) { Console.SetCursorPosition(50, 10); Console.WriteLine("Moves:{0}/{1}", i + 1, heromove); ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow && c.y >= 1) { c.Move(0, -1, matrix); up.HeroUpgradePickup(c, matrix); } if (userInput.Key == ConsoleKey.RightArrow && c.y <= Console.BufferWidth - 1) { c.Move(0, 1, matrix); up.HeroUpgradePickup(c, matrix); } if (userInput.Key == ConsoleKey.UpArrow && c.x >= 1) { c.Move(-1, 0, matrix); up.HeroUpgradePickup(c, matrix); } if (userInput.Key == ConsoleKey.DownArrow && c.x < Console.BufferHeight - 1) { c.Move(1, 0, matrix); up.HeroUpgradePickup(c, matrix); } if (userInput.Key == ConsoleKey.Spacebar) { c.Move(0, 0, matrix); } if (userInput.Key == ConsoleKey.Z && matrix[0,0] == 0 ) { Aliens newAlien = c.NewAlien(); newAlien.placeInGrid(matrix); All.Add(newAlien); Console.WriteLine(printGrid(matrix)); } } foreach (Aliens value in All) { value.zombieMove(c, matrix); if (value.Lose(c) == 1) { Console.WriteLine(count); return; } } count++; } if (count > 20) { Console.Clear(); Console.WriteLine("You have escape"); } }