private static bool DinoJump(Dino dino, bool inJump, int startRow, ref int jumpCounter) { if (Console.KeyAvailable) { var pressedKey = Console.ReadKey(); if (pressedKey.Key == ConsoleKey.Spacebar) { dino.Y--; inJump = true; } } if (inJump) { jumpCounter++; if (jumpCounter < 3) { dino.Y -= jumpCounter; } else { dino.Y += 5 - jumpCounter; } } if (jumpCounter == 6) { inJump = false; jumpCounter = 0; dino.Y = startRow; } return inJump; }
private static void DisplayDino(Dino dino) { Console.SetCursorPosition(dino.X, dino.Y); Console.WriteLine(dino.dinoBody); }
private static void Main() { var gameOver = false; var level = 5; var randomGenerator = new Random(); char[] arrayCactuses = {'^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';'}; var obstacles = new Queue<Obstacle>(); var inJump = false; var jumpCounter = 0; var watch = new Stopwatch(); watch.Start(); Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; Console.ForegroundColor = ConsoleColor.Green; Console.CursorVisible = false; var startCol = 15; var startRow = Console.BufferWidth/5; var dino = new Dino(); dino.X = startCol; dino.Y = startRow; dino.dinoBody = "/\\"; while (true) { inJump = DinoJump(dino, inJump, startRow, ref jumpCounter); DisplayDino(dino); DisplayWatch(watch); MoveObstacles(obstacles); if (obstacles.Any()) { if (obstacles.Peek().X == dino.X && obstacles.Peek().Y == dino.Y) { gameOver = true; } } if (gameOver) { watch.Stop(); break; } Thread.Sleep(100); Console.Clear(); GenerateNewObstacle(randomGenerator, level, obstacles, startRow, arrayCactuses); } }