I have the code and need to implement the following features:
- Modify the existing Move method to support an optional parameter
- If enabled, the optional parameter should detect nondirectional key input
- If nondirectional input is detected, allow the game to terminate
This is my code of Move() method so far:
void Move()
{
int lastX = playerX;
int lastY = playerY;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
playerY--;
break;
case ConsoleKey.DownArrow:
playerY++;
break;
case ConsoleKey.LeftArrow:
playerX--;
break;
case ConsoleKey.RightArrow:
playerX++;
break;
case ConsoleKey.Escape:
shouldExit = true;
break;
}
// Clear the characters at the previous position
Console.SetCursorPosition(lastX, lastY);
for (int i = 0; i < player.Length; i++)
{
Console.Write(" ");
}
// Keep player position within the bounds of the Terminal window
playerX = (playerX < 0) ? 0 : (playerX >= width ? width : playerX);
playerY = (playerY < 0) ? 0 : (playerY >= height ? height : playerY);
// Draw the player at the new location
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
}
And this is my main game loop:
while (!shouldExit)
{
if (TerminalResized())
{
Console.Clear();
Console.WriteLine("Console was resized. Program exiting");
}
Move();
}
I have already implemented the Terminate On Resize feature, which is:
- Determine if the terminal was resized before allowing the game to continue
- Clear the Console and end the game if the terminal was resized
- Display the following message before ending the program:
Console was resized. Program exiting.