gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:00 25 4
gpt4 key购买 nike

我正在尝试制作我的第一款游戏,一款控制台俄罗斯方 block 。我有一个 Block 类,它包含 x 和 y 整数。然后我有一节课Piece : List<Block> , 和一个类 Pieces : List<Piece> .

我已经可以随机生成碎片,并让它们每秒掉落一行。我仍然没有进行碰撞检测,但我想我已经知道以后如何解决它了。问题是我不知道如何控制棋子。我已经阅读了一些关于键盘 Hook 的内容并检查了一些俄罗斯方 block 教程,但其中大部分是针对 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/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;
}
}

关于C# 控制台俄罗斯方 block 编程。让棋子动起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com