private static Position DisplayNewPosition(Position dwarfElement, int currentDirection) { // set the cursor at the place of the current "dwarf" element Console.SetCursorPosition(dwarfElement.X, dwarfElement.Y); // set it to be blank space Console.Write(" "); // set the new position Position currentPosition = new Position(dwarfElement.X + currentDirection, dwarfElement.Y, dwarfElement.Symbol, dwarfElement.Color); // set the cursor there Console.SetCursorPosition(currentPosition.X, currentPosition.Y); // display it on the new position Console.Write(currentPosition.Symbol); return currentPosition; }
private static bool IsGameOver(List<Position> dwarfElements, Position currentRockPositions) { // looping through dwarf elements ( "(", "o" and ")" ) for (int i = 0; i < dwarfElements.Count; i++) { // if "rock" element is at the same position of any "dwarf" element the game is over if (dwarfElements[i].X == currentRockPositions.X && dwarfElements[i].Y == currentRockPositions.Y) { // set the cursor position at the left-top corner Console.SetCursorPosition(0, 0); // set its color to gray Console.ForegroundColor = ConsoleColor.Gray; // display game over and level message Console.WriteLine("Game Over ! Level " + gameLevel); return true; } } return false; }
static void Main(string[] args) { Console.Write("Level " + gameLevel); // initial random "rock" index set to 0 int randomRockIndex = 0; // initial random color index set to 0 int randomColorIndex = 0; // initial symbol or also called "rock" set to empty string string rock = String.Empty; // hides the visibility of the cursor Console.CursorVisible = false; // sets console buffer height Console.BufferHeight = Console.WindowHeight; // holds the "dwarf" elements List<Position> dwarfElements = new List<Position>(); // holds the "rocks" elements Queue<Position> rocksElements = new Queue<Position>(); // Creates the "dwarf" that will run away from the "rocks" SetDwarfElements(dwarfElements); int currentLoopCounter = 0; // starting infinit loop, if we exit the loop, its a game over. while (true) { //Read user key if (Console.KeyAvailable) { // setting the console cursor color to be green Console.ForegroundColor = ConsoleColor.Green; // read the pressed key from the keyboard ConsoleKeyInfo pressedKey = Console.ReadKey(); //initial current direction int currentDirection = 0; // if we have pressed right arrow... if (pressedKey.Key == ConsoleKey.RightArrow) { // 1 means right currentDirection = 1; // moving "dwarf" to the right MoveDwarfToRight(dwarfElements, currentDirection); } // if we have pressed left arrow... else if (pressedKey.Key == ConsoleKey.LeftArrow) { // -1 means left currentDirection = -1; // moving "dwarf" to the left MoveDwarfToLeft(dwarfElements, currentDirection); } } // get random number to pick "rock" from rocks array randomRockIndex = randomGenerator.Next(0, rocksArray.Length); // get random number to pick color from colors array randomColorIndex = randomGenerator.Next(0, rocksColors.Length); // pick a "rock" by the generated random rock number rock = rocksArray[randomRockIndex]; // creates the "rock" at a random position on the very top of the console with random color Position rockPosition = new Position(randomGenerator.Next(1, Console.WindowWidth - 1), 1, rock, rocksColors[randomColorIndex]); // adding the "rock" position into a queue rocksElements.Enqueue(rockPosition); // set the cursor at the "rock" position Console.SetCursorPosition(rockPosition.X, rockPosition.Y); // set the curson color to be the random color Console.ForegroundColor = rocksColors[randomColorIndex]; // display the "rock" on the console Console.Write(rock); bool isGameOver = false; // Move all the "rocks" to the bottom ( "rocks falling" ) MoveRocks(rocksElements, dwarfElements, out isGameOver); // if game is over, stops if (isGameOver) return; // wait before re-display as much as the sleepTime value is (in milliseconds) Thread.Sleep((int)gameSpeed); // increase the current loop counter by one currentLoopCounter++; // if current loop counter is bigger than the value of the loop counter limit // it is time to go one level up and increase the speed of the "rock falling" if(currentLoopCounter > loopCounterLimit) { // go one level up and speed up the "falling" SetOneLevelUp(); // reset the value to 0 currentLoopCounter = 0; } } }
private static void SetDwarfElements(List<Position> dwarfElements) { // "dwarf" will look like that: (o) string[] elementsArray = new string[] { "(", "o", ")" }; // looping each symbol and set its posion on the console for (int i = 0; i < 3; i++) { // setting the position to be in the middle-bottom of the console // setting the symbol // setting its color to be green Position element = new Position((Console.WindowWidth / 2) + i, Console.WindowHeight - 1, elementsArray[i], ConsoleColor.Green); // adding this element to the list that will be collection all "dwarf" elements for later usage dwarfElements.Add(element); // setting the console color to be the color of the dwarf element Console.ForegroundColor = element.Color; // setting the cursor posion of the console Console.SetCursorPosition(element.X, element.Y); // wrting the element on the console with the set color and positon (from above) Console.Write(element.Symbol); } }
//Dworf stuff static void PrintDwarf(Position p) { //clean the old dworf if (currentDirection == 1) { Console.SetCursorPosition(p.X + 3, p.Y); Console.ForegroundColor = Console.BackgroundColor; Console.Write(" "); } else if (currentDirection == 2 && p.X > 0) { Console.SetCursorPosition(p.X - 1, p.Y); Console.ForegroundColor = Console.BackgroundColor; Console.Write(" "); } Console.ForegroundColor = ConsoleColor.DarkCyan; Console.SetCursorPosition(p.X, p.Y); Console.Write("<@>"); }