我试图让我的第一场比赛,控制台俄罗斯方块。 我有一个类模块,它包含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.
                }
            }
        }
有帮助吗?

解决方案

什么你正在寻找的是非阻塞的控制台输入。

下面是一个例子:

的http:// WWW .dutton.me.uk / 2009/02/24 /非阻塞键盘输入式-C /

基本上,则需要检查 Console.KeyAvailable 在while循环,然后根据按下了哪个键移动片。


            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;
                }
            }

其他提示

您可以使用低级键盘挂钩如图此处

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top