简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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