简体   繁体   English

c#控制台-执行某些操作,直到按下键

[英]c# Console-Do something until key pressed

Recently I am trying to make basic games in Console-Application, like to move the cursor with the arrows (maybe as a very basic start of snake and games that you move something all over the Console screen), and I wanted to ask if it's possible to make that the cursor won't stop to move, and just change the direction when I press on some arrow? 最近,我试图在Console-Application中制作基本游戏,例如用箭头移动光标(也许是蛇的非常基本的起点,并且您在Console屏幕上移动了一些东西),我想问一下是否是否有可能使光标不会停止移动,而只是在按下某些箭头时更改方向? Is it something with events? 有事吗?

Thanks. 谢谢。

I would suggest something like the following: 我建议类似以下内容:

bool isRight, isUp; //Used to determine direction
while (gamePlaying)
{
     if(Console.KeyAvailable)
     {
       Console.ReadKey(); //Do your thing in here
     }
}

So you check if a key is being pressed, and if it is you read it. 因此,您检查是否按下了一个键,是否已按下它。

Also, bear in mind if you're making a console game you'll want to double-buffer your screen and use the Win32 API to minimise write calls if possible or your screen with flicker horribly. 另外,请记住,如果您要制作控制台游戏,则希望对屏幕进行双重缓冲,并尽可能使用Win32 API最小化写入调用,否则屏幕将出现可怕的闪烁。

I do not believe there is anyway to control the cursor position of the console window. 我认为无论如何都无法控制控制台窗口的光标位置。 You can read or write text, but that's about it. 您可以阅读或编写文本,仅此而已。 So if you want to make a console game, it will need to be text-based. 因此,如果您想制作一个主机游戏,它将需要基于文本。 You may be able to make an game ASCII art based where new text is printed to the console fast enough that it appears to be animated, but I don't think the console can go fast enough to make this work well. 您也许可以基于游戏ASCII艺术作品,将新文本足够快地打印到控制台上,使其看起来好像是动画的,但是我认为控制台不能足够快地完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM