C# 如何在c中读取按钮mashing#

C# 如何在c中读取按钮mashing#,c#,console-application,C#,Console Application,我一直在尝试在我的RPG中使用一个按钮混合的小游戏,但是我还没有成功地让它工作。这是我创建的代码: using System; namespace CounterAttack { class Program { public static int n = 0; public static int buttonmash = 0; public static string buttonpressed; public static void CounterAttack

我一直在尝试在我的RPG中使用一个按钮混合的小游戏,但是我还没有成功地让它工作。这是我创建的代码:

using System;

namespace CounterAttack
{
class Program
{
    public static int n = 0;
    public static int buttonmash = 0;
    public static string buttonpressed;
    public static void CounterAttack()
    {
        n = 0;
        while (n < 4000)
        {
            buttonpressed = Console.ReadKey().ToString();
            System.Threading.Thread.Sleep(1);
            n = n + 1;
            if (buttonpressed == " ")
            {
                buttonmash = buttonmash + 1;
                buttonpressed = "a";
            }
            else
            {
                buttonpressed = "a";
            }
        }
        Console.WriteLine($"You pressed spacebar {buttonmash} times ");
    }
    static void Main(string[] args)
    {
        CounterAttack();
        System.Threading.Thread.Sleep(1000);
        Console.ReadKey(); 
    }
}
使用系统;
命名空间反击
{
班级计划
{
公共静态int n=0;
公共静态int按钮mash=0;
按下公共静态字符串按钮;
公共静态无效反击()
{
n=0;
而(n<4000)
{
buttonpressed=Console.ReadKey().ToString();
系统线程线程睡眠(1);
n=n+1;
如果(按钮按下==“”)
{
buttonmash=buttonmash+1;
按钮按下=“a”;
}
其他的
{
按钮按下=“a”;
}
}
WriteLine($“您按了空格键{buttonmash}次”);
}
静态void Main(字符串[]参数)
{
反击();
系统线程线程睡眠(1000);
Console.ReadKey();
}
}
}

由于我不知道的原因,程序将不会退出while循环。这意味着我无法检查代码的其他部分是否可以工作

因此,我的问题是:如何修改此代码以读取空格键在4秒内被按下的次数?

“我一直在尝试在我的RPG中运行一个按钮混合小游戏,但我还没有成功使其运行。”

您正在控制台应用程序中执行此操作。因此,答案从“不容易”到“完全不可能”不等。我希望其他人能沿着这个轴给你更精确的答案

游戏开发和GUI技术在一开始就没有很好的结合。那个古老的纸牌游戏几乎是游戏的上限。控制台与它的匹配比这还要少

所有严肃的游戏开发都是在游戏循环的任何环境中完成的。XNA有点过时,但对于.NET Core来说,有一些选择:

“我一直在尝试让一个按钮混搭的小游戏在我的RPG中工作,但我还没有成功让它工作。”

您正在控制台应用程序中执行此操作。因此,答案从“不容易”到“完全不可能”不等。我希望其他人能沿着这个轴给你更精确的答案

游戏开发和GUI技术在一开始就没有很好的结合。那个古老的纸牌游戏几乎是游戏的上限。控制台与它的匹配比这还要少


所有严肃的游戏开发都是在游戏循环的任何环境中完成的。XNA有点过时了,但对于.NET Core来说,有一些选项:

这种方法可以工作,只要在4秒计时器启动后按空格键

    static void Main(string[] args)
    {
        var startTime = DateTime.Now;
        var n = 0;
        while (DateTime.Now < startTime.AddSeconds(4))
        {
            var key = Console.ReadKey();
            System.Threading.Thread.Sleep(1);
            if (key.Key == ConsoleKey.Spacebar) n = n + 1;
        }
        Console.WriteLine($"You pressed spacebar {n} times ");
    }
static void Main(字符串[]args)
{
var startTime=DateTime.Now;
var n=0;
while(DateTime.Now
只要在4秒计时器启动后按空格键,这种方法就行了

    static void Main(string[] args)
    {
        var startTime = DateTime.Now;
        var n = 0;
        while (DateTime.Now < startTime.AddSeconds(4))
        {
            var key = Console.ReadKey();
            System.Threading.Thread.Sleep(1);
            if (key.Key == ConsoleKey.Spacebar) n = n + 1;
        }
        Console.WriteLine($"You pressed spacebar {n} times ");
    }
static void Main(字符串[]args)
{
var startTime=DateTime.Now;
var n=0;
while(DateTime.Now
试试这个:

class Program
    {
        private static int numSpaces = 0;

        static void Main(string[] args)
        {
            Thread th = new Thread(CheckSpace);

            th.Start();
            th.Join();

            Console.WriteLine($"You pressed space {numSpaces} times");
            Console.Read();
        }

        private static void CheckSpace()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    if (Console.ReadKey().Key == ConsoleKey.Spacebar)
                    {
                        numSpaces++;
                    }
                }
                if (sw.ElapsedMilliseconds>= 4000)
                {

                    break;
                }

                Thread.Sleep(10);
            }
        }
    }
试试这个:

class Program
    {
        private static int numSpaces = 0;

        static void Main(string[] args)
        {
            Thread th = new Thread(CheckSpace);

            th.Start();
            th.Join();

            Console.WriteLine($"You pressed space {numSpaces} times");
            Console.Read();
        }

        private static void CheckSpace()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    if (Console.ReadKey().Key == ConsoleKey.Spacebar)
                    {
                        numSpaces++;
                    }
                }
                if (sw.ElapsedMilliseconds>= 4000)
                {

                    break;
                }

                Thread.Sleep(10);
            }
        }
    }

@auberg的解决方案很好,但实际上不需要第二个线程。是的,在主线程上调用
Thread.Sleep()
是一个坏主意,但是这么短的时间是不明显的

试试这个:

 public static void CountKeyPresses()
 {
     Console.WriteLine("OK, start pressing the space bar");
     var numPresses = 0;
     var stopWatch = new Stopwatch();
     stopWatch.Start();
     while (stopWatch.ElapsedMilliseconds < 4000)
     {
         if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Spacebar)
         {
             ++numPresses;
         }
         System.Threading.Thread.Sleep(10);
     }
     Console.WriteLine($"\r\nYou pressed the spacebar {numPresses} times");
 }
publicstaticvoidcountkeypresss()
{
Console.WriteLine(“好,开始按空格键”);
var numpress=0;
var stopWatch=新秒表();
秒表。开始();
同时(秒表。延时毫秒<4000)
{
if(Console.KeyAvailable&&Console.ReadKey().Key==ConsoleKey.Spacebar)
{
++乳房;
}
系统线程线程睡眠(10);
}
WriteLine($“\r\n您按空格键{numPresses}次”);
}

@auberg的解决方案很好,但您确实不需要第二个线程。是的,在主线程上调用
Thread.Sleep()
是一个坏主意,但是这么短的时间是不明显的

试试这个:

 public static void CountKeyPresses()
 {
     Console.WriteLine("OK, start pressing the space bar");
     var numPresses = 0;
     var stopWatch = new Stopwatch();
     stopWatch.Start();
     while (stopWatch.ElapsedMilliseconds < 4000)
     {
         if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Spacebar)
         {
             ++numPresses;
         }
         System.Threading.Thread.Sleep(10);
     }
     Console.WriteLine($"\r\nYou pressed the spacebar {numPresses} times");
 }
publicstaticvoidcountkeypresss()
{
Console.WriteLine(“好,开始按空格键”);
var numpress=0;
var stopWatch=新秒表();
秒表。开始();
同时(秒表。延时毫秒<4000)
{
if(Console.KeyAvailable&&Console.ReadKey().Key==ConsoleKey.Spacebar)
{
++乳房;
}
系统线程线程睡眠(10);
}
WriteLine($“\r\n您按空格键{numPresses}次”);
}

按钮按下=Console.ReadKey().ToString()
将阻止倒计时
系统线程线程睡眠(1)
将阻止空格键被按下(也是以毫秒为单位,而不是秒为单位,应该是
Thread.Sleep(1000)
,要退出循环,您需要按空格键并等待1秒4000次。在退出该循环之前,您需要按一个键4000次。您可能希望查看
控制台.KeyAvailable
,如果有要读取的键,则只执行
控制台.ReadKey
。在windows应用程序中尝试执行此操作,然后倒计时(或KeyUp)事件。控制台只需等待按键按下
按钮=console.ReadKey().ToString();
将阻止倒计时
System.Threading.Thread.S