/// <summary> /// Displays the header /// </summary> private void DisplayHeader() { // Set the color to red BufferEditor.WriteWithColor(0, 0, " ", ConsoleColor.Red); BufferEditor.WriteWithColor(0, 5, " ", ConsoleColor.Red); // Loop 100 times for (int i = 0; i < 100; i++) { // Write to the buffer BufferEditor.Write(i, 0, "-"); BufferEditor.Write(i, 5, "-"); } // Change the color to blue BufferEditor.WriteWithColor(0, 1, " ", ConsoleColor.Blue); // Write to the buffer BufferEditor.Write(1, 1, "Score:"); BufferEditor.Write(30, 1, "Level:"); BufferEditor.Write(47, 1, "Lifes:"); // Write the numbers to the buffer NumberManager.WriteScore(score); NumberManager.WriteLevel(level); NumberManager.WriteLifes(lifes); }
/// <summary> /// Write the bullet to the buffer /// </summary> private void Write() { // Write to the buffer BufferEditor.Delete(coordinates.X, coordinates.Y, bullet.ToString()); BufferEditor.WriteWithColor(0, coordinates.Y, " ", ConsoleColor.White); }
/// <summary> /// Set's the color for the barriers /// </summary> public void SetColor() { for (int i = 0; i < BARRIER_HEIGHT; i++) { BufferEditor.WriteWithColor(0, coordinates.Y + i - 1, " ", ConsoleColor.Green); } }
/// <summary> /// Moves the enemy based on it's current direction /// </summary> private void Move() { // If the enemy is to move down if (IncreaseY) { // Clear the area above the enemy BufferEditor.WriteWithColor(0, coordinates.Y, " ", ConsoleColor.Black); BufferEditor.Delete(coordinates.X, coordinates.Y, " "); // Moves the enemy down coordinates.Y++; // Reset the `IncreaseY` back to false IncreaseY = false; } // Else else { // If the movement type is left if (MovementType == MoveType.LEFT) { // Move the enemy left coordinates.X--; } // Else if the movement type is right else if (MovementType == MoveType.RIGHT) { // Move the enemy right coordinates.X++; } } }
/// <summary> /// Deletes the enemy from the buffer /// </summary> public void Delete() { // Go through all the strings in the sprite for (int i = 0; i < SPRITE_HEIGTH; i++) { // Write to the current buffer an empty string BufferEditor.Delete(coordinates.X, coordinates.Y + i, " "); } }
/// <summary> /// Writes the barriers to the buffer /// </summary> public void Write() { // Set the color to green BufferEditor.SetColor(ConsoleColor.Green); for (int i = 0; i < BARRIER_HEIGHT; i++) { BufferEditor.Delete(coordinates.X, coordinates.Y + i - 1, sprite[i]); } }
public void Delete() { // Go through the ship sprite array for (int i = 0; i < sprite.Length; i++) { // Write each string to the buffer BufferEditor.Delete(coordinates.X, coordinates.Y + i, " "); BufferEditor.WriteWithColor(0, coordinates.Y + i, " ", ConsoleColor.Black); } }
/// <summary> /// Writes the ship sprite to the buffer /// </summary> private void WriteToBuffer() { // Go through the ship sprite array for (int i = 0; i < sprite.Length; i++) { // Write each string to the buffer BufferEditor.Write(coordinates.X, coordinates.Y + i, sprite[i]); // Set the color to white BufferEditor.WriteWithColor(0, coordinates.Y + i, " ", ConsoleColor.White); } }
/// <summary> /// Moves all enemies down /// </summary> public void MoveDown() { // If the animation timer has finished counting if (!animationTimer.IsCounting()) { // Change the sprite to animate the enemy firstSprite = !firstSprite; } // If the moveDownTimer is not counting if (!moveDownTimer.IsCounting()) { //Increase the steps by 1 moveDownSteps++; // If the moveDowSteps is greater than the `MOVE_DOWN_STEPS` returns if (moveDownSteps > MOVE_DOWN_STEPS) { return; } // Create a new vector2 Vector2 coordinate; // Goes through every enemy in the Enemy List foreach (Enemy enemy in EnemyList) { // Save the enemy coordinate to a more convinient variable coordinate = enemy.Coordinates; // Set the enemy to not move enemy.CanMove = false; // Update the enemy sprite enemy.FirstSprite = firstSprite; // Increase the enemy Y enemy.YIncreasse(); // If the enemy Y coordinate is greater than the `TOP_START_ROW` if (coordinate.Y > TOP_START_ROW) { // Delete the top line of the enemy BufferEditor.Delete(coordinate.X, coordinate.Y, " "); // Update the enemy enemy.Update(); } } } }
/// <summary> /// Delete a string from the current buffer /// </summary> /// <param name="x">String x position</param> /// <param name="y">String Y position</param> /// <param name="str">The string</param> public static void Delete(int x, int y, string str) { // Goes through every char on the given string for (int i = 0; i < str.Length; i++) { // Set's the char for the current pixel currentPixel.pixelChar = str[i]; // Writes that pixel to the buffer buffer2D.current[x + i, y] = currentPixel; } // Write to the buffer BufferEditor.Write(x, y, str); }
/// <summary> /// Writes the enemy sprite to the buffer /// </summary> private void WriteToBuffer() { // Select the correct sprite to render currentSprite = FirstSprite ? sprite1 : sprite2; // Go through the array of strings for (int i = 0; i < currentSprite.Length; i++) { // Write a string to the buffer BufferEditor.WriteWithColor(0, coordinates.Y + i, " ", myColor); // Write each string to the buffer BufferEditor.Delete(coordinates.X, coordinates.Y + i, currentSprite[i]); } }
/// <summary> /// Writes the ovni to the buffer /// </summary> public void Write() { // If the current state is Flying if (currentState == State.FLYING) { // Loop the height of the ovni sprite for (int i = 0; i < OVNI_HEIGHT; i++) { // Set the color BufferEditor.WriteWithColor(0, coordinates.Y + i, " ", ConsoleColor.DarkMagenta); // Write the ovni to the buffer BufferEditor.Write(coordinates.X, coordinates.Y + i, sprite[i]); } } }
/// <summary> /// Writes the number to the buffer /// </summary> /// <param name="value"> /// The value it self (number of lifes, score, etc...)</param> /// <param name="xOffset"> /// Offset the number to the right depending on the number of numbers</param> /// <param name="x">The x value where the number will be displayed</param> private static void WriteNumber(int value, int xOffset, int x) { // Loops 3 times... for (int i = 0; i < NUMBER_ROWS; i++) { // Change the color depending on the row BufferEditor.WriteWithColor(0, i + 2, " ", i == 0 ? ConsoleColor.White : i == 1 ? ConsoleColor.Yellow : ConsoleColor.DarkYellow); // Write each string to the buffer BufferEditor.Write(x + xOffset, i + 2, digits[value][i]); } }
/// <summary> /// Get Ready method to animate the begining of the game or level /// </summary> private void GetReady() { // Create a new timer counter Timer counter = new Timer(160); // Create a new timer to blink the text Timer blinker = new Timer(9); // Create a new bool to display the text bool displayText = true; // While the counter is counting while (counter.IsCounting()) { // Updates the header DisplayHeader(); // Sets the `displayText` to true or false displayText = !blinker.IsCounting() ? !displayText : displayText; // If the displayText is true if (displayText) { // Set a color and write to the buffer BufferEditor.WriteWithColor(0, 50, " ", ConsoleColor.Yellow); BufferEditor.Write(45, 50, "Get Ready!"); } // Else... else { // Delete from the buffer BufferEditor.Delete(45, 50, " "); } // Move the enemies down enemies.MoveDown(); /// Render the frame /// BufferEditor.DisplayRender(); // Delay the loop Thread.Sleep(BASE_DELAY); } // Delete the text again BufferEditor.Write(45, 50, " "); }
/// <summary> /// Moves the enemies up /// </summary> public void MoveUp() { // If the moveSpeed timer is not counting and the move steps is counting if (!moveUpSpeed.IsCounting() && moveUpSteps.IsCounting()) { // Create a new int `min` int min = 60; // Create a new coordinate Vector2 coordinate; // Go through every enemy in the enemies list for (int i = 0; i < EnemyList.Count; i++) { // Save it's coordinates coordinate = EnemyList[i].Coordinates; // If the min is bigger than the coordinate Y if (min > coordinate.Y) { // Set the min to be equal to the coordinate Y min = coordinate.Y; } } // If min is bigger than the minimum Y value if (min > Y_MIN) { // Go through every enemy on the enemies list for (int i = 0; i < EnemyList.Count; i++) { // Save it's coordinates coordinate = EnemyList[i].Coordinates; // Delete the under part of the enemy BufferEditor.Delete(coordinate.X, coordinate.Y + 2, " "); // Decrease the enemy Y value EnemyList[i].DecreaseY(); } } } }
/// <summary> /// Deletes the number from the buffer /// </summary> /// <param name="format">Used to know how many numbers will represent the value</param> /// <param name="x">The X position to write the numbers</param> private static void Delete(int format, int x) { // Create a new int `xOffset` int xOffset = 0; // Loops the ammount of numbers for (int i = 0; i <= format; i++) { // Loops the ammount of rows for (int j = 0; j < NUMBER_ROWS; j++) { // Deletes the number from the buffer BufferEditor.Delete(x + xOffset, j + 2, " "); } // Increase the x offset by 2 xOffset += 2; } }
/// <summary> /// The player lost a life /// </summary> private void LifeLost() { // Int with the time ammount the blinking will last int timer = (lifes == 0) ? 50 : 150; // Create a new timer counter = new Timer(timer); // How long it takes for each blink blinkCounter = new Timer(8); // If we wan't to display the lifes bool displayLife = true; // Decrease the ammount of lifes lifes--; // Set the life lost to true ship.LifeLost = true; // Reset the Enemies Move Up enemies.ResetMoveUp(); // Set the ship destroyed true enemies.shipDestroyed = true; // Add an explosion explosions.Add(ship.Coordinates, ExplosionType.LARGE); // While the counter is counting while (counter.IsCounting()) { // Go through all objects in the collection for (int i = 0; i < objectsCollection.Count; i++) { // Update them (objectsCollection[i] as GameObject).Update(); } // Delay the thread by a certain ammout of time so the game can be precieved Thread.Sleep(BASE_DELAY); // If the blink counter has stopped counting if (!blinkCounter.IsCounting()) { // If display life is true if (displayLife) { // Set it to false displayLife = false; // Display the lifes NumberManager.WriteLifes(lifes + 1); BufferEditor.DisplayRender(); NumberManager.WriteLifes(lifes + 1); } // Else else { // Set it to true displayLife = true; // Delete the lifes NumberManager.DeleteLifes(); } } // Check for hits EnemyDestroyedCheck(); BarrierHitCheck(); /// Render the frame /// BufferEditor.DisplayRender(); } // Reset the ship values ship.Init(level); // Set the life lost to false ship.LifeLost = false; // Set the ship destroyed to false enemies.shipDestroyed = false; // Display the lifes NumberManager.WriteLifes(lifes); }
/// <summary> /// Is called when the level is completed to play animations and initialise the next level /// </summary> private void LevelCompleted() { // Create a new timer to be a counter for the animations duration Timer counter = new Timer(201); // Create a new timer for the blinking animation Timer blinker = new Timer(9); // Create a new bool bool displayScore = false; // Clears the buffer BufferEditor.ClearBuffer(); // Loops while the counter is counting while (counter.IsCounting()) { // Sets the display score to true or false depending on the counter displayScore = !blinker.IsCounting() ? !displayScore : displayScore; // If displayScore is true if (displayScore) { // Write an informational text saying that the level was completed BufferEditor.WriteWithColor(0, 22, " ", ConsoleColor.Yellow); BufferEditor.WriteWithColor(0, 24, " ", ConsoleColor.Yellow); BufferEditor.Delete(36, 22, "L E V E L C O M P L E T E D!"); BufferEditor.Delete(38, 24, "Level bonus 1000 points!"); NumberManager.WriteLevel(level); } else { // Delete the previously written text BufferEditor.Write(36, 22, " "); BufferEditor.Write(38, 24, " "); NumberManager.DeleteLevel(); } // Updates the score score += 5; NumberManager.WriteScore(score); // Updates the header DisplayHeader(); // Display the frame BufferEditor.DisplayRender(); // Wait for 20 miliseconds Thread.Sleep(20); } // Removes the text in the middle of the screen BufferEditor.Write(36, 22, " "); BufferEditor.Write(38, 24, " "); // Increases the level level++; // Updates the level number NumberManager.WriteLevel(level); // Delays for 800 miliseconds Thread.Sleep(800); // Initializes the next level InitNextLevel(); }
/// <summary> /// Delete the bullet from the buffer /// </summary> public void Delete() { // Write the bullet to the buffer BufferEditor.Delete(coordinates.X, coordinates.Y, " "); }
/// <summary> /// Game Loop Class /// </summary> public void Loop() { // Intantiate a new ship ship = new Ship(level); // Add a ship to the objects collection objectsCollection.Add(ship); // Add the enemies to the objects collection objectsCollection.Add(enemies); // Add the barriers to the objects collection objectsCollection.Add(barriers); // Add the ovni to the objects collection objectsCollection.Add(ovni); // Add the explosions to the objects collection objectsCollection.Add(explosions); // Clear the buffer from the menu render BufferEditor.ClearBuffer(); /// Call a global Start method /// for (int i = 0; i < objectsCollection.Count; i++) { objectsCollection[i].Start(); } // Create a new long start long start; // Create a new long timeToWait int timeToWait; // Calls the get ready animation before the game starts GetReady(); // Loops... do { // The time in ticks at the start of the frame start = DateTime.Now.Ticks / 10000; /// Call a global update method /// for (int i = 0; i < objectsCollection.Count; i++) { objectsCollection[i].Update(); } // Displays the header DisplayHeader(); // Check for hits EnemyDestroyedCheck(); OvniDestroyedCheck(); BarrierHitCheck(); // If there's no enemies left if (enemies.EnemyList.Count == 0) { // Call the level completed method LevelCompleted(); } // Check if the ship was hit if (ShipDestroyedCheck()) { // Call the life lost method LifeLost(); gameOver = lifes == 0; } /// Render the frame /// BufferEditor.DisplayRender(); // Get the delay needed timeToWait = (int)(start + FAME_TIME - DateTime.Now.Ticks / 10000); // Delay the thread by a certain ammout of time so the game can be precieved Thread.Sleep(timeToWait < 0 ? 0 : timeToWait); // While the game is not over } while (!gameOver); // Clears the buffer BufferEditor.ClearBuffer(); }
/// <summary> /// Renders all the sprites in the menu /// </summary> public void RenderMenu() { // Display the enemies in the menu // Enemy 1 for (int i = 0; i < Sprites.enemy1String.Length; i++) { BufferEditor.WriteWithColor(0, 10 + i, " ", ConsoleColor.Green); BufferEditor.Write(6, 10 + i, Sprites.enemy1String[i]); BufferEditor.Write(85, 10 + i, Sprites.enemy1String[i]); } // Enemy 2 for (int i = 0; i < Sprites.enemy2String.Length; i++) { BufferEditor.WriteWithColor(0, 17 + i, " ", ConsoleColor.Cyan); BufferEditor.Write(6, 17 + i, Sprites.enemy2String[i]); BufferEditor.Write(85, 17 + i, Sprites.enemy2String[i]); } // Enemy 3 for (int i = 0; i < Sprites.enemy3String.Length; i++) { BufferEditor.WriteWithColor(0, 24 + i, " ", ConsoleColor.Magenta); BufferEditor.Write(6, 24 + i, Sprites.enemy3String[i]); BufferEditor.Write(85, 24 + i, Sprites.enemy3String[i]); } // Enemy 4 for (int i = 0; i < Sprites.enemy4String.Length; i++) { BufferEditor.WriteWithColor(0, 31 + i, " ", ConsoleColor.Blue); BufferEditor.Write(6, 31 + i, Sprites.enemy4String[i]); BufferEditor.Write(85, 31 + i, Sprites.enemy4String[i]); } // Enemy 5 for (int i = 0; i < Sprites.enemy5String.Length; i++) { BufferEditor.WriteWithColor(0, 37 + i, " ", ConsoleColor.DarkGreen); BufferEditor.Write(6, 37 + i, Sprites.enemy5String[i]); BufferEditor.Write(85, 37 + i, Sprites.enemy5String[i]); } // Display Title for (int i = 0; i < Sprites.spaceString.Length; i++) { BufferEditor.WriteWithColor(0, 21 + i, " ", ConsoleColor.Yellow); BufferEditor.Write(21, 21 + i, Sprites.spaceString[i]); BufferEditor.Write(45, 21 + i, Sprites.invadersString[i]); } // Display Buttons for (int i = 0; i < Sprites.playString.Length; i++) { BufferEditor.WriteWithColor(0, 28 + i, " ", ConsoleColor.White); BufferEditor.Write(22, 28 + i, Sprites.playString[i]); BufferEditor.Write(63, 28 + i, Sprites.quitString[i]); } // Display Button Selector for (int i = 0; i < Sprites.selectionString.Length; i++) { BufferEditor.WriteWithColor(0, 33 + i, " ", ConsoleColor.White); // If we have the play button selected if (playSelected) { // Draw the ship bellow the play button BufferEditor.Write(PLAY_X_SELECTION, Y_SELECTION + i, Sprites.selectionString[i]); BufferEditor.Write(QUIT_X_SELECTION, Y_SELECTION + i, " "); } // If we have the quit button selected else if (quitSelected) { // Draw the ship bellow the quit button BufferEditor.Write(QUIT_X_SELECTION, Y_SELECTION + i, Sprites.selectionString[i]); BufferEditor.Write(PLAY_X_SELECTION, Y_SELECTION + i, " "); } } // Tells the double buffer to render the frame BufferEditor.DisplayRender(); // Gets the input from the user GetInput(); }
/// <summary> /// Deletes the Ovni from the buffer /// </summary> private void Delete() { // Delete the ovni from the buffer BufferEditor.Delete(coordinates.X, coordinates.Y, " "); BufferEditor.Delete(coordinates.X, coordinates.Y + 1, " "); }