Uri*_*Uri 1 c# keyboard tetris
我正在尝试制作我的第一款游戏,一款控制台俄罗斯方块.我有一个类Block,它包含x和y整数.然后我有一个班级Piece : List<Block>和一个班级Pieces : List<Piece>.
我已经可以随机生成碎片,并使它们每秒掉落一行.我仍然没有进行碰撞检测,但我认为我已经知道如何解决它.问题是我不知道如何控制碎片.我已经阅读了一些关于键盘挂钩并检查了一些俄罗斯方块教程,但其中大多数是用于Windows窗体,这真正简化了事件处理等等.
那么......你能指点一下在控制台上控制棋子的路径的开头吗?谢谢!
public class Program
{
static void Main(string[] args)
{
const int limite = 60;
Piezas listaDePiezas = new Piezas(); //list of pieces
bool gameOver = false;
Pieza pieza; //piece
Console.CursorVisible = false;
while (gameOver != true)
{
pieza = CrearPieza(); //Cretes a piece
if (HayColision(listaDePiezas, pieza) == true) //if there's a collition
{
gameOver = true;
break;
}
else
listaDePiezas.Add(pieza); //The piece is added to the list of pieces
while (true) //This is where the piece falls. I know that I shouldn't use a sleep. I'll take care of that later
{
Thread.Sleep(1000);
pieza.Bajar(); //Drop the piece one row.
Dibujar(listaDePiezas); //Redraws the gameplay enviroment.
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 12
您正在寻找的是非阻塞控制台输入.
这是一个例子:
http://www.dutton.me.uk/2009/02/24/non-blocking-keyboard-input-in-c/
基本上,您可以在while循环中检查Console.KeyAvailable,然后根据按下的键移动该块.
if (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.UpArrow:
// not used in tetris game?
break;
case ConsoleKey.DownArrow:
// drop piece
break;
case ConsoleKey.LeftArrow:
// move piece left
break;
case ConsoleKey.RightArrow:
// move piece right
break;
}
}Run Code Online (Sandbox Code Playgroud)