C# 控制台俄罗斯方块编程。让棋子动起来?
我正在尝试制作我的第一个游戏,控制台俄罗斯方块。 我有一个类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是非阻塞控制台输入。
这是一个示例:
http:// /www.dutton.me.uk/2009/02/24/non-blocking-keyboard-input-in-c/
基本上,您会检查 Console.KeyAvailable 在 while 循环中,然后根据按下的键移动该部分。
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.
您可以使用低级键盘挂钩,如此处
You could use a Low-Level Keyboard Hook as shown here