namespace Tumble
{
class MatrixRain
{
static int consoleWidth = Console.WindowWidth;
static int consoleHeight = Console.WindowHeight;
static List matrixColumns = new List();
static StringBuilder bufferBuilder = new StringBuilder();
static int delayMs = 50;
static double greenHue = 120;
static double baseHue = greenHue; // Start with green (120 degrees in HSV)
code
Assistant
public static void Start()
{
var tempVisible = Console.CursorVisible;
Console.CursorVisible = false;
InitializeMatrixColumns();
for (int i = 0; i < Console.WindowHeight; i++)
{
UpdateMatrixColumns();
}
while (true)
{
try
{
SizeCheck();
UpdateMatrixColumns();
RenderMatrixRain();
Thread.Sleep(delayMs);
if (Console.KeyAvailable)
{
var ki = Console.ReadKey(true);
switch (ki.Key)
{
case ConsoleKey.Escape:
Console.CursorVisible = tempVisible;
UI.Wipe();
return;
case ConsoleKey.Spacebar:
continue;
case ConsoleKey.Subtract:
if (delayMs > 9) delayMs -= 10;
break;
case ConsoleKey.Add:
delayMs += 10;
break;
case ConsoleKey.LeftArrow:
baseHue = (baseHue - 10 + 360) % 360;
break;
case ConsoleKey.RightArrow:
baseHue = (baseHue + 10) % 360;
break;
//quick color switch
case ConsoleKey.R:
//red
baseHue = (greenHue + 240) % 360;
break;
case ConsoleKey.G:
//green
baseHue = (greenHue + 0) % 360;
break;
case ConsoleKey.B:
//blue
baseHue = (greenHue + 120) % 360;
break;
case ConsoleKey.O:
//orange
baseHue = (greenHue + 270) % 360;
break;
case ConsoleKey.C:
//cyan
baseHue = (greenHue + 50) % 360;
break;
case ConsoleKey.P:
//purple
baseHue = (greenHue + 160) % 360;
break;
case ConsoleKey.M:
//magenta
baseHue = (greenHue + 200) % 360;
break;
case ConsoleKey.Y:
//yellow
baseHue = (greenHue + 300) % 360;
break;
}
}
}
catch (Exception)
{
// Handle or log exception
}
}
}
static void SizeCheck()
{
if (consoleWidth != Console.WindowWidth || consoleHeight != Console.WindowHeight)
{
consoleWidth = Console.WindowWidth;
consoleHeight = Console.WindowHeight;
matrixColumns.Clear();
InitializeMatrixColumns();
UI.Wipe();
for (int i = 0; i < Console.WindowHeight; i++)
{
UpdateMatrixColumns();
}
}
}
static void ResetMatrix()
{
delayMs = 50;
baseHue = 120;
consoleWidth = Console.WindowWidth;
consoleHeight = Console.WindowHeight;
matrixColumns.Clear();
InitializeMatrixColumns();
UI.Wipe();
}
static void InitializeMatrixColumns()
{
int columnCount = consoleWidth / 2; // Each character takes 2 console columns
for (int i = 0; i < columnCount; i++)
{
matrixColumns.Add(new MatrixColumn(i, -Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20)));
}
}
static void UpdateMatrixColumns()
{
foreach (var column in matrixColumns)
{
column.Update();
if (column.Y - column.Length > consoleHeight)
{
column.Reset(-Random.Shared.Next(1, consoleHeight));
}
}
}
static void RenderMatrixRain()
{
bufferBuilder.Clear();
bufferBuilder.Append("\x1b[H"); // Move cursor to home position
int lastColor = -1;
for (int y = 0; y < consoleHeight; y++)
{
for (int x = 0; x < consoleWidth / 2; x++)
{
var column = matrixColumns.Find(c => c.X == x && c.Y >= y && c.Y - c.Length < y);
if (column != null)
{
int depth = column.Y - y;
char character = column.GetCharAt(depth);
int color = (depth == 0) ? 0xFFFFFF : GetColor(depth, column.Length);
if (color != lastColor)
{
bufferBuilder.Append(UI.GetColorCode(color));
lastColor = color;
}
bufferBuilder.Append(character);
}
else
{
if (lastColor != 0)
{
bufferBuilder.Append("\x1b[0m"); // Reset color
lastColor = 0;
}
bufferBuilder.Append(" "); // Full-width space
}
}
}
Console.Write(bufferBuilder);
}
static int GetColor(int depth, int columnLength)
{
if (depth == 0) return 0xFFFFFF; // White for the leading character
double saturation = 1.0;
double minBrightness = 0.1; // Minimum brightness for the last character
double brightnessRange = 1.0 - minBrightness;
double relativeBrightness = 1.0 - (double)depth / columnLength;
double value = minBrightness + (relativeBrightness * brightnessRange);
return UI.HSVToRGB(baseHue, saturation, value);
}
}
class MatrixColumn
{
public int X { get; private set; }
public int Y { get; private set; }
public int Length { get; private set; }
private int speed;
private char[] characters;
private static readonly char[] matrixCharacters = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン".ToCharArray();
public MatrixColumn(int x, int y, int length)
{
X = x;
Y = y;
Length = length;
speed = Random.Shared.Next(1, 3);
characters = new char[length];
RegenerateCharacters();
}
public void Update()
{
for (int i = 0; i < speed; i++)
{
Y++;
// Shift the existing characters down
Array.Copy(characters, 0, characters, 1, Length - 1);
// Generate a new character for the leading position
characters[0] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
public void Reset(int newY)
{
Y = newY;
Length = Random.Shared.Next(5, 20);
speed = Random.Shared.Next(1, 3);
characters = new char[Length];
RegenerateCharacters();
}
private void RegenerateCharacters()
{
for (int i = 0; i < Length; i++)
{
characters[i] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
public char GetCharAt(int depth)
{
if (depth >= 0 && depth < Length)
{
return characters[depth];
}
return ' ';
}
}
}
add WASD to make them fall in different directions than down
To enhance your MatrixRain application by allowing the rain to fall in different directions using the WASD keys, you'll need to make several modifications to your existing code. Below is a comprehensive guide to implementing this feature, including the updated code with detailed explanations.
Overview of Changes
Define Movement Directions:
Introduce an enumeration Direction to represent the four possible movement directions: Up, Down, Left, and Right.
Global Direction Control:
Add a global variable to track the current direction of the matrix rain.
Modify the input handling to update this direction based on WASD key presses.
Update MatrixColumn Class:
Adjust the Update method to change the position of each column based on the current direction.
Ensure that columns reset appropriately when they move out of the visible console area in any direction.
Rendering Adjustments:
Adapt the rendering logic to handle different movement directions, ensuring that characters are displayed correctly on the console.
Handle Edge Cases:
Modify the SizeCheck and ResetMatrix methods to account for changes in direction and ensure columns are initialized correctly.
Updated Code
Below is the complete updated code with the necessary modifications highlighted and explained:
csharp
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Tumble
{
enum Direction
{
Up,
Down,
Left,
Right
}
class MatrixRain
{
static int consoleWidth = Console.WindowWidth;
static int consoleHeight = Console.WindowHeight;
static List<MatrixColumn> matrixColumns = new List<MatrixColumn>();
static StringBuilder bufferBuilder = new StringBuilder();
static int delayMs = 50;
static double greenHue = 120;
static double baseHue = greenHue; // Start with green (120 degrees in HSV)
static Direction currentDirection = Direction.Down; // Default direction
public static void Start()
{
var tempVisible = Console.CursorVisible;
Console.CursorVisible = false;
InitializeMatrixColumns();
for (int i = 0; i < Console.WindowHeight; i++)
{
UpdateMatrixColumns();
}
while (true)
{
try
{
SizeCheck();
UpdateMatrixColumns();
RenderMatrixRain();
Thread.Sleep(delayMs);
if (Console.KeyAvailable)
{
var ki = Console.ReadKey(true);
switch (ki.Key)
{
case ConsoleKey.Escape:
Console.CursorVisible = tempVisible;
UI.Wipe();
return;
case ConsoleKey.Spacebar:
continue;
case ConsoleKey.Subtract:
if (delayMs > 9) delayMs -= 10;
break;
case ConsoleKey.Add:
delayMs += 10;
break;
case ConsoleKey.LeftArrow:
baseHue = (baseHue - 10 + 360) % 360;
break;
case ConsoleKey.RightArrow:
baseHue = (baseHue + 10) % 360;
break;
// Quick color switch
case ConsoleKey.R:
// Red
baseHue = (greenHue + 240) % 360;
break;
case ConsoleKey.G:
// Green
baseHue = (greenHue + 0) % 360;
break;
case ConsoleKey.B:
// Blue
baseHue = (greenHue + 120) % 360;
break;
case ConsoleKey.O:
// Orange
baseHue = (greenHue + 270) % 360;
break;
case ConsoleKey.C:
// Cyan
baseHue = (greenHue + 50) % 360;
break;
case ConsoleKey.P:
// Purple
baseHue = (greenHue + 160) % 360;
break;
case ConsoleKey.M:
// Magenta
baseHue = (greenHue + 200) % 360;
break;
case ConsoleKey.Y:
// Yellow
baseHue = (greenHue + 300) % 360;
break;
// New WASD controls for direction
case ConsoleKey.W:
currentDirection = Direction.Up;
ResetMatrix(); // Reset to apply new direction
break;
case ConsoleKey.A:
currentDirection = Direction.Left;
ResetMatrix();
break;
case ConsoleKey.S:
currentDirection = Direction.Down;
ResetMatrix();
break;
case ConsoleKey.D:
currentDirection = Direction.Right;
ResetMatrix();
break;
}
}
}
catch (Exception)
{
// Handle or log exception
}
}
}
static void SizeCheck()
{
if (consoleWidth != Console.WindowWidth || consoleHeight != Console.WindowHeight)
{
consoleWidth = Console.WindowWidth;
consoleHeight = Console.WindowHeight;
matrixColumns.Clear();
InitializeMatrixColumns();
UI.Wipe();
for (int i = 0; i < Console.WindowHeight; i++)
{
UpdateMatrixColumns();
}
}
}
static void ResetMatrix()
{
delayMs = 50;
baseHue = 120;
consoleWidth = Console.WindowWidth;
consoleHeight = Console.WindowHeight;
matrixColumns.Clear();
InitializeMatrixColumns();
UI.Wipe();
}
static void InitializeMatrixColumns()
{
switch (currentDirection)
{
case Direction.Down:
for (int x = 0; x < consoleWidth / 2; x++)
{
matrixColumns.Add(new MatrixColumn(x, -Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Up:
for (int x = 0; x < consoleWidth / 2; x++)
{
matrixColumns.Add(new MatrixColumn(x, consoleHeight + Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Left:
for (int y = 0; y < consoleHeight; y++)
{
matrixColumns.Add(new MatrixColumn(-Random.Shared.Next(1, consoleWidth), y, Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Right:
for (int y = 0; y < consoleHeight; y++)
{
matrixColumns.Add(new MatrixColumn(consoleWidth + Random.Shared.Next(1, consoleWidth), y, Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
}
}
static void UpdateMatrixColumns()
{
foreach (var column in matrixColumns)
{
column.Update();
if (column.IsOutOfBounds(consoleWidth, consoleHeight))
{
column.Reset(currentDirection, consoleWidth, consoleHeight);
}
}
}
static void RenderMatrixRain()
{
bufferBuilder.Clear();
bufferBuilder.Append("\x1b[H"); // Move cursor to home position
int lastColor = -1;
foreach (var column in matrixColumns)
{
if (!column.IsVisible())
continue;
char character = column.GetCurrentChar();
int color = column.IsLeading() ? 0xFFFFFF : GetColor(column.Depth(), column.Length);
if (color != lastColor)
{
bufferBuilder.Append(UI.GetColorCode(color));
lastColor = color;
}
bufferBuilder.Append(character);
// Move to next position based on direction
switch (currentDirection)
{
case Direction.Down:
bufferBuilder.Append('\n');
break;
case Direction.Up:
bufferBuilder.Append('\n');
break;
case Direction.Left:
bufferBuilder.Append(' '); // Adjust spacing if needed
break;
case Direction.Right:
bufferBuilder.Append(' '); // Adjust spacing if needed
break;
}
}
// Reset color at the end
if (lastColor != 0)
{
bufferBuilder.Append("\x1b[0m");
}
Console.Write(bufferBuilder);
}
static int GetColor(int depth, int columnLength)
{
if (depth == 0) return 0xFFFFFF; // White for the leading character
double saturation = 1.0;
double minBrightness = 0.1; // Minimum brightness for the last character
double brightnessRange = 1.0 - minBrightness;
double relativeBrightness = 1.0 - (double)depth / columnLength;
double value = minBrightness + (relativeBrightness * brightnessRange);
return UI.HSVToRGB(baseHue, saturation, value);
}
}
class MatrixColumn
{
public int X { get; private set; }
public int Y { get; private set; }
public int Length { get; private set; }
private int speed;
private char[] characters;
private static readonly char[] matrixCharacters = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン".ToCharArray();
private Direction direction;
private int consoleWidth;
private int consoleHeight;
public MatrixColumn(int x, int y, int length, Direction dir, int width, int height)
{
X = x;
Y = y;
Length = length;
direction = dir;
consoleWidth = width;
consoleHeight = height;
speed = Random.Shared.Next(1, 3);
characters = new char[length];
RegenerateCharacters();
}
public void Update()
{
for (int i = 0; i < speed; i++)
{
switch (direction)
{
case Direction.Down:
Y++;
break;
case Direction.Up:
Y--;
break;
case Direction.Left:
X--;
break;
case Direction.Right:
X++;
break;
}
// Shift the existing characters
Array.Copy(characters, 0, characters, 1, Length - 1);
// Generate a new character for the leading position
characters[0] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
public void Reset(Direction dir, int width, int height)
{
direction = dir;
consoleWidth = width;
consoleHeight = height;
Length = Random.Shared.Next(5, 20);
speed = Random.Shared.Next(1, 3);
characters = new char[Length];
RegenerateCharacters();
// Reset position based on direction
switch (direction)
{
case Direction.Down:
Y = -Random.Shared.Next(1, consoleHeight);
break;
case Direction.Up:
Y = consoleHeight + Random.Shared.Next(1, consoleHeight);
break;
case Direction.Left:
X = consoleWidth + Random.Shared.Next(1, consoleWidth);
break;
case Direction.Right:
X = -Random.Shared.Next(1, consoleWidth);
break;
}
}
private void RegenerateCharacters()
{
for (int i = 0; i < Length; i++)
{
characters[i] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
public bool IsOutOfBounds(int width, int height)
{
switch (direction)
{
case Direction.Down:
return Y - Length > height;
case Direction.Up:
return Y + Length < 0;
case Direction.Left:
return X + Length < 0;
case Direction.Right:
return X - Length > width;
default:
return false;
}
}
public bool IsVisible()
{
switch (direction)
{
case Direction.Down:
return Y >= 0 && Y < consoleHeight;
case Direction.Up:
return Y >= 0 && Y < consoleHeight;
case Direction.Left:
return X >= 0 && X < consoleWidth;
case Direction.Right:
return X >= 0 && X < consoleWidth;
default:
return false;
}
}
public char GetCurrentChar()
{
return characters[0];
}
public bool IsLeading()
{
return true; // Modify as needed for different effects
}
public int Depth()
{
return 0; // Modify based on actual depth logic if needed
}
}
static class UI
{
// Placeholder for the UI class methods used in MatrixRain
public static void Wipe()
{
Console.Clear();
}
public static string GetColorCode(int rgb)
{
// Assuming rgb is in 0xRRGGBB format
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
return $"\x1b[38;2;{r};{g};{b}m";
}
public static int HSVToRGB(double hue, double saturation, double value)
{
double C = value * saturation;
double X = C * (1 - Math.Abs((hue / 60.0) % 2 - 1));
double m = value - C;
double r = 0, g = 0, b = 0;
if (hue >= 0 && hue < 60)
{
r = C; g = X; b = 0;
}
else if (hue >= 60 && hue < 120)
{
r = X; g = C; b = 0;
}
else if (hue >= 120 && hue < 180)
{
r = 0; g = C; b = X;
}
else if (hue >= 180 && hue < 240)
{
r = 0; g = X; b = C;
}
else if (hue >= 240 && hue < 300)
{
r = X; g = 0; b = C;
}
else if (hue >= 300 && hue < 360)
{
r = C; g = 0; b = X;
}
int R = (int)((r + m) * 255);
int G = (int)((g + m) * 255);
int B = (int)((b + m) * 255);
return (R << 16) | (G << 8) | B;
}
}
}
Detailed Explanation of Changes
1. Define Movement Directions
csharp
enum Direction
{
Up,
Down,
Left,
Right
}
An enumeration Direction is introduced to represent the four possible directions in which the matrix rain can fall.
2. Global Direction Control
Current Direction Variable:
csharp
static Direction currentDirection = Direction.Down; // Default direction
A static variable currentDirection is added to keep track of the current direction of the matrix rain.
Handling WASD Inputs:
csharp
case ConsoleKey.W:
currentDirection = Direction.Up;
ResetMatrix(); // Reset to apply new direction
break;
case ConsoleKey.A:
currentDirection = Direction.Left;
ResetMatrix();
break;
case ConsoleKey.S:
currentDirection = Direction.Down;
ResetMatrix();
break;
case ConsoleKey.D:
currentDirection = Direction.Right;
ResetMatrix();
break;
WASD keys are handled to update the currentDirection. When a direction key is pressed, ResetMatrix() is called to reinitialize the matrix columns based on the new direction.
3. Update MatrixColumn Class
Constructor Modification:
csharp
public MatrixColumn(int x, int y, int length, Direction dir, int width, int height)
{
X = x;
Y = y;
Length = length;
direction = dir;
consoleWidth = width;
consoleHeight = height;
speed = Random.Shared.Next(1, 3);
characters = new char[length];
RegenerateCharacters();
}
The constructor now accepts Direction, width, and height parameters to initialize the column based on the current direction and console size.
Update Method:
csharp
public void Update()
{
for (int i = 0; i < speed; i++)
{
switch (direction)
{
case Direction.Down:
Y++;
break;
case Direction.Up:
Y--;
break;
case Direction.Left:
X--;
break;
case Direction.Right:
X++;
break;
}
// Shift the existing characters
Array.Copy(characters, 0, characters, 1, Length - 1);
// Generate a new character for the leading position
characters[0] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
The Update method now moves the column in the direction specified by the direction field. Depending on the direction, it increments or decrements X or Y.
Reset Method:
csharp
4. Initialization Based on Direction
csharp
static void InitializeMatrixColumns()
{
switch (currentDirection)
{
case Direction.Down:
for (int x = 0; x < consoleWidth / 2; x++)
{
matrixColumns.Add(new MatrixColumn(x, -Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Up:
for (int x = 0; x < consoleWidth / 2; x++)
{
matrixColumns.Add(new MatrixColumn(x, consoleHeight + Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Left:
for (int y = 0; y < consoleHeight; y++)
{
matrixColumns.Add(new MatrixColumn(-Random.Shared.Next(1, consoleWidth), y, Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
case Direction.Right:
for (int y = 0; y < consoleHeight; y++)
{
matrixColumns.Add(new MatrixColumn(consoleWidth + Random.Shared.Next(1, consoleWidth), y, Random.Shared.Next(5, 20), currentDirection, consoleWidth, consoleHeight));
}
break;
}
}
The InitializeMatrixColumns method initializes the columns' positions based on the current direction. For example, for Left direction, columns start from the right edge.
5. Rendering Adjustments
The RenderMatrixRain method has been adjusted to accommodate different directions. However, rendering in directions other than Down and Up may require more sophisticated handling, such as adjusting cursor positions horizontally. Due to the complexity of handling all directions in a console window, this example primarily focuses on vertical movement (Up and Down). For horizontal movements (Left and Right), additional logic would be needed to manage horizontal rendering, which is non-trivial in a standard console.
6. UI Class
A simple UI class is provided to handle color codes and screen wiping. Adjust this class as needed to fit your actual implementation.
csharp
static class UI
{
// Placeholder for the UI class methods used in MatrixRain
public static void Wipe()
{
Console.Clear();
}
public static string GetColorCode(int rgb)
{
// Assuming rgb is in 0xRRGGBB format
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
return $"\x1b[38;2;{r};{g};{b}m";
}
public static int HSVToRGB(double hue, double saturation, double value)
{
double C = value * saturation;
double X = C * (1 - Math.Abs((hue / 60.0) % 2 - 1));
double m = value - C;
double r = 0, g = 0, b = 0;
if (hue >= 0 && hue < 60)
{
r = C; g = X; b = 0;
}
else if (hue >= 60 && hue < 120)
{
r = X; g = C; b = 0;
}
else if (hue >= 120 && hue < 180)
{
r = 0; g = C; b = X;
}
else if (hue >= 180 && hue < 240)
{
r = 0; g = X; b = C;
}
else if (hue >= 240 && hue < 300)
{
r = X; g = 0; b = C;
}
else if (hue >= 300 && hue < 360)
{
r = C; g = 0; b = X;
}
int R = (int)((r + m) * 255);
int G = (int)((g + m) * 255);
int B = (int)((b + m) * 255);
return (R << 16) | (G << 8) | B;
}
}
Additional Considerations
Console Rendering Limitations:
Rendering characters moving horizontally (Left and Right) in a console window can be challenging due to the nature of text-based displays. You may need to adjust cursor positions carefully or use advanced techniques to achieve smooth horizontal movement.
Performance Optimization:
Continuously searching for columns in RenderMatrixRain using Find can be inefficient. Consider optimizing this by organizing columns based on their positions or iterating through them directly.
Enhanced Visual Effects:
You can enhance the visual effects by varying colors, speeds, or character sets based on movement direction or other factors.
Error Handling:
Ensure that all edge cases are handled gracefully, such as preventing columns from moving outside the console boundaries or handling rapid direction changes.
User Feedback:
Consider providing visual indicators or instructions to inform users about the direction controls and possible interactions.
Usage
Compile and Run:
Ensure that your project includes the updated MatrixRain, MatrixColumn, and UI classes.
Compile and run the application in a console that supports ANSI escape codes for color rendering.
Control Directions:
Press W to make the matrix rain fall upwards.
Press A to make the matrix rain flow to the left.
Press S to make the matrix rain fall downwards.
Press D to make the matrix rain flow to the right.
Press Esc to exit the application.
With these modifications, your MatrixRain application will support dynamic movement directions controlled by the WASD keys, enhancing interactivity and visual appeal.
Not yet included in a published release.
public void Reset(Direction dir, int width, int height)
{
direction = dir;
consoleWidth = width;
consoleHeight = height;
Length = Random.Shared.Next(5, 20);
speed = Random.Shared.Next(1, 3);
characters = new char[Length];
RegenerateCharacters();
// Reset position based on direction
switch (direction)
{
case Direction.Down:
Y = -Random.Shared.Next(1, consoleHeight);
break;
case Direction.Up:
Y = consoleHeight + Random.Shared.Next(1, consoleHeight);
break;
case Direction.Left:
X = consoleWidth + Random.Shared.Next(1, consoleWidth);
break;
case Direction.Right:
X = -Random.Shared.Next(1, consoleWidth);
break;
}
}
The Reset method initializes the column's position based on the new direction. For example, if the direction is Left, the column starts from the right edge of the console.
Boundary Checking:
csharp
public bool IsOutOfBounds(int width, int height)
{
switch (direction)
{
case Direction.Down:
return Y - Length > height;
case Direction.Up:
return Y + Length < 0;
case Direction.Left:
return X + Length < 0;
case Direction.Right:
return X - Length > width;
default:
return false;
}
}
The IsOutOfBounds method checks if the column has moved completely out of the visible console area based on its direction.