I am trying to learn c# by making a tetris console application. I have a gameBoard class ("gb" in the code) and a block class ("bl" in the code.) The code below is what I have so far to move a block left and right, but I can't wrap my head around how to make the block fall at the same time I'm accepting user input.
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
switch (keyInfo.Key)
{
case ConsoleKey.LeftArrow:
currCol = bl.getCol();
if (currCol - 1 >= 0)
{
gb.removeBlock(bl.getCol(), bl.getRow());
bl.setCol(currCol - 1);
gb.putBlock(bl.getCol(), bl.getRow());
Console.Clear();
Console.WriteLine(gb.makeGrid());
}
break;
case ConsoleKey.RightArrow:
currCol = bl.getCol();
if (currCol + 1 <= 9)
{
gb.removeBlock(bl.getCol(), bl.getRow());
bl.setCol(currCol + 1);
gb.putBlock(bl.getCol(), bl.getRow());
Console.Clear();
Console.WriteLine(gb.makeGrid());
}
break;
}
}
I am assuming that a Timer is probably the way to do this, but I don't know how I can pass my instances to the ElapsedEventHandler's OnTimedEvent function
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 5 seconds.
aTimer.Interval=5000;
aTimer.Enabled=true;
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
Are timers the way to go, or should I be using something else? If timers are what I am supposed to be using, where should I start learning about how to use them?
Thanks!