简体   繁体   English

C#-控制台如何获取每一帧的输入

[英]C# - Console How to get input every frame

So, I was bored and decided to write an ASCII game in C# for the fun of it, and I have drawing, clearing, updating, etc.. Although, I'm stuck at one part, input. 因此,我很无聊,因此决定以C#编写一个ASCII游戏,并且我要进行绘图,清除,更新等工作。尽管我只停留在输入的一部分。 I want to get input every frame, without the player pressing enter, So far the player has to press enter, but it doesn't do anything. 我想在每一帧都获得输入,而无需玩家按Enter键,到目前为止,玩家必须按Enter键,但是它什么也没做。

Here is my code: 这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace ASCII
{
    public static class Game
    {
        static string map = File.ReadAllText("Map.txt");
        public static void Draw()
        {
            Console.CursorVisible = false;
            Console.WriteLine(map);
        }

        public static void Update()
        {
            Clear();
            Input();
        }

        public static void Input()
        {
            string input = Console.ReadLine();

            switch (input)
            {
                case "a":
                    //Do something
                    break;
            }
        }

        public static void Clear()
        {
            Console.Clear();
            Draw();
        }
    }
}

As you can see in the Input() void, it gets input every frame, but I only want to get it once, the do a move method or something that I will implement later. 正如您在Input() void中看到的那样,它在每一帧都获取输入,但是我只想获取一次,执行move方法或稍后将实现的操作。

BTW Map.txt displays this: BTW Map.txt显示如下:

###################

# #

# @ # #

######## #

# #

# #

# #

# #

# #

# #

# #

###################

Console.ReadLine will wait for the enter-key to continue making the application kind of modal. Console.ReadLine将等待enter键继续使应用程序变为模态。 What you want instead is handle keyboard-events on the concole. 相反,您想要的是在Condel上处理键盘事件。 So you could use ReadKey instead: 因此,您可以改用ReadKey

var input = Console.ReadKey(true);

switch (input.Key)
{
    case ConsoleKey.A:
        //Do something
        break;
}

To hold on moving you may implement this in a loop. 要继续前进,您可以循环执行此操作。 The key here is to remember your current action until the next key-event passes 关键是要记住您当前的操作,直到下一个关键事件过去为止

int action = 0;
while(!exit) 
{
    // handle the action
    myPlayer.X += action; // move player left or right depending on the previously pressed key (A or D)

    if(!Console.KeyAvailable) continue;
    var input = Console.ReadKey(true);

    switch (input.Key)
    {
        case ConsoleKey.A:
            action = -1
            break;
        case ConsoleKey.D:
            action = 1
            break;
    }    
}

I'm not sure I understood the question right, but if I did, you can use Console.KeyAvailable and Console.ReadKey to check if a key is available before reading it. 我不确定我是否理解正确的问题,但是如果知道,可以在读取之前使用Console.KeyAvailableConsole.ReadKey检查密钥是否可用。

So something like: 所以像这样:

public static void Input()
{
   if(!Console.KeyAvailable) return;
   ConsoleKeyInfo key = Console.ReadKey(true);

   switch (key.Key)
   {
      case ConsoleKey.A:
         //Do something
          break;
   }
}

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

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