private static void GetUserInput() { while (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.LeftArrow) { player.MoveLeft(); DetermineCollisions(); } else if (pressedKey.Key == ConsoleKey.RightArrow) { player.MoveRight(); DetermineCollisions(); } } }
static void Main(string[] args) { Console.WindowHeight = 40; Console.WindowWidth = 40; Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.BufferWidth; List <GameObject> allObject = new List <GameObject>(); List <Rock> rocks = new List <Rock>(); List <Bullet> bullets = new List <Bullet>(); Random random = new Random(); Dwarf dwarf = new Dwarf(new Position(20, 39)); allObject.Add(dwarf); Draw(allObject); while (true) { Thread.Sleep(150); Console.Clear(); Console.WriteLine("point = " + point); Rock rock = new Rock(new Position(random.Next(39), 1), new Position(0, 1)); allObject.Add(rock); rocks.Add(rock); point += CollisionDetection.BulletDetection(bullets, rocks); if (CollisionDetection.RockSmash(dwarf, rocks)) { EndGame(); } if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow) { if (dwarf.Position.Row > 0) { dwarf.MoveLeft(); } } if (userInput.Key == ConsoleKey.RightArrow) { if (dwarf.Position.Row < Console.WindowWidth - 2) { dwarf.MoveRight(); } } if (userInput.Key == ConsoleKey.Spacebar) { Bullet bullet = new Bullet(new Position(dwarf.Position.Row, dwarf.Position.Col - 1), new Position(0, -1)); bullets.Add(bullet); allObject.Add(bullet); } } Draw(allObject); } }
static void Main() { Initialisation(); Dwarf dwarf = Dwarf.GetInstance(); List <Rock> removeObsoletes; bool hit = false; ConsoleKeyInfo pressedKey; while (true) { for (int i = 0, count = 1; i < count; i++) { rocks.Add(new Rock()); } while (Console.KeyAvailable) { pressedKey = Console.ReadKey(true); if (pressedKey.Key == ConsoleKey.LeftArrow) { dwarf.MoveLeft(); } if (pressedKey.Key == ConsoleKey.RightArrow) { dwarf.MoveRight(); } } removeObsoletes = new List <Rock>(); hit = false; foreach (Rock rock in rocks) { if (!rock.MoveDown()) { removeObsoletes.Add(rock); } if (dwarf.Overlap(rock)) { hit = true; Console.Beep(); Console.Out.WriteLine("GAME OVER !!!"); Environment.Exit(0); } } Console.Clear(); foreach (Rock old in removeObsoletes) { rocks.Remove(old); } if (hit) { rocks.Clear(); } foreach (Rock rock in rocks) { rock.Print(); } dwarf.Print(hit); Thread.Sleep(150); } }