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 ' ';
}
}
}
when i press W, make them fall upwards with the white part up and tail following below.
To modify your MatrixRain program so that pressing the W key makes the rain fall upwards—with the leading (white) character at the top and the tail following below—you'll need to introduce direction control into your existing implementation. Below is the updated code with detailed explanations of the changes made to achieve this functionality.
Overview of Changes
Direction Tracking: Introduced a global variable to track the current direction of the rain (downwards or upwards).
Key Handling: Added a case for the W key to toggle the direction.
MatrixColumn Adjustments: Modified the MatrixColumn class to handle movement based on the current direction.
Rendering Adjustments: Updated the rendering logic to display the leading character appropriately based on the direction.
Initialization and Reset Logic: Adjusted the initialization and reset logic to position columns correctly depending on the direction.
Updated Code
csharp
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Tumble
{
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)
// New variable to track the direction of the rain
static bool isFallingUpwards = false;
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 case for W key to toggle direction
case ConsoleKey.W:
isFallingUpwards = !isFallingUpwards;
ResetMatrix(); // Reset to apply the new direction
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++)
{
if (isFallingUpwards)
{
// Start below the visible window if falling upwards
matrixColumns.Add(new MatrixColumn(i, consoleHeight + Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), isFallingUpwards));
}
else
{
// Start above the visible window if falling downwards
matrixColumns.Add(new MatrixColumn(i, -Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), isFallingUpwards));
}
}
}
static void UpdateMatrixColumns()
{
foreach (var column in matrixColumns)
{
column.Update();
if (isFallingUpwards)
{
// Check if the column has moved above the visible window
if (column.Y + column.Length < 0)
{
// Reset below the window
column.Reset(consoleHeight + Random.Shared.Next(1, consoleHeight));
}
}
else
{
// Check if the column has moved below the visible window
if (column.Y - column.Length > consoleHeight)
{
// Reset above the window
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 && (
(isFallingUpwards ? c.Y <= y && c.Y + c.Length > y :
c.Y >= y && c.Y - c.Length < y)
));
if (column != null)
{
int depth;
if (isFallingUpwards)
{
depth = y - column.Y;
}
else
{
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
}
}
bufferBuilder.Append(Environment.NewLine);
}
if (lastColor != 0)
{
bufferBuilder.Append("\x1b[0m"); // Reset at end
}
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 bool isFallingUpwards;
private static readonly char[] matrixCharacters = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン".ToCharArray();
public MatrixColumn(int x, int y, int length, bool isFallingUpwards)
{
X = x;
Y = y;
Length = length;
this.isFallingUpwards = isFallingUpwards;
speed = Random.Shared.Next(1, 3);
characters = new char[length];
RegenerateCharacters();
}
public void Update()
{
for (int i = 0; i < speed; i++)
{
if (isFallingUpwards)
{
Y--;
}
else
{
Y++;
}
if (isFallingUpwards)
{
// Shift characters up
Array.Copy(characters, 1, characters, 0, Length - 1);
// Generate new character at the end
characters[Length - 1] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
else
{
// Shift characters down
Array.Copy(characters, 0, characters, 1, Length - 1);
// Generate new character at the top
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 ' ';
}
}
// Mock UI class based on context
static class UI
{
public static void Wipe()
{
Console.Clear();
}
// Converts HSV values to RGB integer
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 rPrime, gPrime, bPrime;
if (hue >= 0 && hue < 60)
{
rPrime = C;
gPrime = X;
bPrime = 0;
}
else if (hue >= 60 && hue < 120)
{
rPrime = X;
gPrime = C;
bPrime = 0;
}
else if (hue >= 120 && hue < 180)
{
rPrime = 0;
gPrime = C;
bPrime = X;
}
else if (hue >= 180 && hue < 240)
{
rPrime = 0;
gPrime = X;
bPrime = C;
}
else if (hue >= 240 && hue < 300)
{
rPrime = X;
gPrime = 0;
bPrime = C;
}
else // hue >= 300 && hue < 360
{
rPrime = C;
gPrime = 0;
bPrime = X;
}
int r = (int)((rPrime + m) * 255);
int g = (int)((gPrime + m) * 255);
int b = (int)((bPrime + m) * 255);
return (r << 16) | (g << 8) | b;
}
// Converts RGB integer to ANSI escape code for coloring text
public static string GetColorCode(int color)
{
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
return $"\x1b[38;2;{r};{g};{b}m"; // Set foreground RGB
}
}
}
Detailed Explanation of the Changes
Direction Tracking (isFallingUpwards):
Added a static boolean isFallingUpwards to the MatrixRain class to keep track of the current direction of the rain.
Key Handling for W Key:
In the Start method, within the key handling switch statement, added a new case for ConsoleKey.W that toggles the isFallingUpwards flag and resets the matrix to apply the new direction.
csharp
case ConsoleKey.W:
isFallingUpwards = !isFallingUpwards;
ResetMatrix(); // Reset to apply the new direction
break;
Initialization (InitializeMatrixColumns):
Modified the InitializeMatrixColumns method to position the columns based on the current direction.
If isFallingUpwards is true, columns start below the visible window; otherwise, they start above.
csharp
if (isFallingUpwards)
{
// Start below the visible window if falling upwards
matrixColumns.Add(new MatrixColumn(i, consoleHeight + Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), isFallingUpwards));
}
else
{
// Start above the visible window if falling downwards
matrixColumns.Add(new MatrixColumn(i, -Random.Shared.Next(1, consoleHeight), Random.Shared.Next(5, 20), isFallingUpwards));
}
Updating Columns (UpdateMatrixColumns):
Adjusted the logic to reset columns based on the current direction.
If falling upwards and a column moves above the window, it resets below, and vice versa.
csharp
Usage
Toggle Direction: Press the W key while the program is running to toggle the direction of the rain. Each press of W will switch the rain between falling downwards and upwards.
Exit: Press the Escape key to exit the program.
Adjust Speed: Use the + and - keys to adjust the speed of the rain.
Change Colors: Utilize the existing color keys (R, G, B, etc.) to change the color hues of the rain.
Additional Considerations
Smooth Transition: Currently, toggling the direction resets all columns. For a smoother transition, you might want to implement a way to gradually change the direction without resetting.
Extensibility: You can extend this functionality by allowing more directional controls or adding more interactive features based on user input.
Performance: Rendering a large number of columns with frequent updates can be CPU-intensive. Optimize as needed based on performance observations.
This enhanced implementation provides a dynamic and interactive Matrix rain effect, allowing users to control the direction of the rain in real-time by pressing the W key. Feel free to further customize and optimize the code to better fit your specific requirements.
Not yet included in a published release.
if (isFallingUpwards)
{
// Check if the column has moved above the visible window
if (column.Y + column.Length < 0)
{
// Reset below the window
column.Reset(consoleHeight + Random.Shared.Next(1, consoleHeight));
}
}
else
{
// Check if the column has moved below the visible window
if (column.Y - column.Length > consoleHeight)
{
// Reset above the window
column.Reset(-Random.Shared.Next(1, consoleHeight));
}
}
MatrixColumn Class Adjustments:
Modified the MatrixColumn constructor to accept a bool isFallingUpwards parameter.
Updated the Update method to move the column in the appropriate direction and shift characters accordingly.
csharp
public MatrixColumn(int x, int y, int length, bool isFallingUpwards)
{
X = x;
Y = y;
Length = length;
this.isFallingUpwards = isFallingUpwards;
speed = Random.Shared.Next(1, 3);
characters = new char[length];
RegenerateCharacters();
}
public void Update()
{
for (int i = 0; i < speed; i++)
{
if (isFallingUpwards)
{
Y--;
}
else
{
Y++;
}
if (isFallingUpwards)
{
// Shift characters up
Array.Copy(characters, 1, characters, 0, Length - 1);
// Generate new character at the end
characters[Length - 1] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
else
{
// Shift characters down
Array.Copy(characters, 0, characters, 1, Length - 1);
// Generate new character at the top
characters[0] = matrixCharacters[Random.Shared.Next(matrixCharacters.Length)];
}
}
}
Rendering Adjustments (RenderMatrixRain):
Updated the rendering logic to account for the direction of the rain.
Adjusted how depth is calculated based on whether the rain is falling upwards or downwards.
csharp
int depth;
if (isFallingUpwards)
{
depth = y - column.Y;
}
else
{
depth = column.Y - y;
}
char character = column.GetCharAt(depth);
int color = (depth == 0) ? 0xFFFFFF : GetColor(depth, column.Length);
UI Class Enhancements:
Provided implementations for the UI.HSVToRGB and UI.GetColorCode methods, assuming they were part of your original codebase.
These methods convert HSV color values to RGB and then to ANSI escape codes for colored console output.
csharp
static class UI
{
public static void Wipe()
{
Console.Clear();
}
// Converts HSV values to RGB integer
public static int HSVToRGB(double hue, double saturation, double value)
{
// [Existing HSV to RGB conversion logic]
}
// Converts RGB integer to ANSI escape code for coloring text
public static string GetColorCode(int color)
{
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
return $"\x1b[38;2;{r};{g};{b}m"; // Set foreground RGB
}
}