C# 控制台俄罗斯方块编程。让棋子动起来?

发布于 2024-08-16 09:35:12 字数 1445 浏览 1 评论 0原文

我正在尝试制作我的第一个游戏,控制台俄罗斯方块。 我有一个类 Block,其中包含 x 和 y 整数。然后我有一个类 Piece : List 和一个类 Pieces : List

我已经可以随机生成碎片,并让它们每秒掉落一排。我仍然没有进行碰撞检测,但我想我已经知道如何解决它了。 问题是我不知道如何控制这些碎片。我读过一些关于键盘挂钩的内容,并检查了一些俄罗斯方块教程,但其中大多数都是针对 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.
                }
            }
        }

I'm trying to make my first game, a console tetris.
I have a class Block, that contains x and y integers. Then I have a class Piece : List<Block>, and a class Pieces : List<Piece>.

I can already randomly generate pieces, and make them fall one row per second. I still didn't get to collisions detection, but I think that I already know how to work it out later.
The problem is that I don't know how to control the pieces. I've read a bit about keyboard hooking and checked some tetris tutorials, but most of them are for windows forms, which really simplifies events handling and the such.

So... Could you please point me the beginning of the path to controlling the pieces on a console? Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2024-08-23 09:35:13

您正在寻找的是非阻塞控制台输入。

这是一个示例:

http:// /www.dutton.me.uk/2009/02/24/non-blocking-keyboard-input-in-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;
                }
            }

What you are looking for is non-blocking console input.

Here is an example:

http://www.dutton.me.uk/2009/02/24/non-blocking-keyboard-input-in-c/

Basically, you would check Console.KeyAvailable in your while loop and then move the piece according to what key was pressed.


            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;
                }
            }
权谋诡计 2024-08-23 09:35:13

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

You could use a Low-Level Keyboard Hook as shown here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文