class Program
{
struct Rain
{
public int x;
public int y;
public char[] c;
}
static void Main(string[] args)
{
char[,] board = new char[10, 50];
int playerY = 25;
List<Rain> rain = new List<Rain>();
Random rnd = new Random(DateTime.Now.Millisecond);
bool continueGame = true;
do
{
// 1/2 chance of making rain this key-press
if (rnd.Next(1, 10) > 5)
rain.Add(new Rain() { x = -1, y = rnd.Next(0, board.GetLength(1)), c = new char[0x3EFFFFF] });
// Move the rain down, destroy the rain, and check for player collision.
for (int n = 0; n < rain.Count; n++)
{
Rain temp = rain[n];
if (temp.x < board.GetLength(0) - 1)
temp.x++;
else if (temp.x == board.GetLength(0) - 1)
{
if (temp.y == playerY)
{
Console.Clear();
Console.WriteLine("You died!");
Console.ReadKey();
return;
}
rain.RemoveAt(n);
continue;
}
rain[n] = temp;
}
// Draw the board & player to a buffer
char[,] buffer = new char[board.GetLength(0), board.GetLength(1)];
for (int x = 0; x < board.GetLength(0); x++)
{
for (int y = 0; y < board.GetLength(1); y++)
{
if (x == board.GetLength(0) - 1 && y == playerY)
buffer[x, y] = '@';
else
buffer[x, y] = ' ';
}
}
// Add enemies to the board
foreach (Rain rt in rain)
{
buffer[rt.x, rt.y] = '*';
}
// Convert our char board to a string (StringBuilder is faster due to pre-allocation)
StringBuilder output = new StringBuilder();
for (int x = 0; x < board.GetLength(0); x++)
{
for (int y = 0; y < board.GetLength(1); y++)
{
output.Append(buffer[x, y]);
}
output.Append(Environment.NewLine);
}
// Add "ground" under the player for pretty
for (int y = 0; y < board.GetLength(1); y++)
{
output.Append('-');
}
output.Append(Environment.NewLine);
output.Append("Press Q to quit. A or Left and D or Right to avoid the 'rain'.");
output.Append(Environment.NewLine);
Console.Clear();
Console.Write(output);
switch (Console.ReadKey().Key)
{
case ConsoleKey.Q:
continueGame = false;
break;
case ConsoleKey.LeftArrow:
case ConsoleKey.A:
if (playerY > 1)
playerY--;
break;
case ConsoleKey.RightArrow:
case ConsoleKey.D:
if (playerY < board.GetLength(1) - 1)
playerY++;
break;
}
}
while (continueGame);
}
}
PS - Random.Next(x, y) is bugged. Technically it should be rnd.Next(0, board.GetLength(1) - 1) but since Random.Next() never returns MaxValue it is safe to call it in this way. It is Microsoft's bug, not mine...
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.