添加倒数计时器而不禁用用户输入

时间:2013-10-19 13:02:34

标签: c# .net console console-application game-engine

所以我只用一步就做了一个基本的Zork控制台应用程序游戏。门每次都是随机的(尚未完成),并且玩家必须猜测通过哪一扇门,只允许通过一扇门。然而,为了让用户输入更快,我想用倒数计时器给墙壁关闭它们的幻觉,如果他们没有在到达零之前输入任何东西,他们就会被“杀死”但是我无法弄清楚如何添加倒数计时器,但也让控制台检查用户关于移动的输入。这是我当前的代码,并且记住它尚未完成。

using System;
using System.Threading; 

namespace Prog4
{
    class program 
    {
        public static void Main(string[] args)
        {
           Random random = new Random();
           int north = random.Next(1,5);
           int south = random.Next(1,5);
           int east = random.Next(1,5);
           int west = random.Next(1,5);

           Console.WriteLine("You are in a room that is slowly closing in on you, it has only four exits." + Environment.NewLine +
                             "The four exits face: North, South, East and West. " + Environment.NewLine +
                             "Use n for North, s for South, e for East and w for West" + Environment.NewLine +
                             "Only one exit leads to outside, the rest lead to certain doooooooooooooom");
         }
    }
}

2 个答案:

答案 0 :(得分:1)

如需开始,请访问此主题:link 之后,您可以看到that

using System;
using System.Timers;

namespace Prog4
{
    class program
    {
        private static int _countDown = 30; // Seconds
        private static bool waySelected = false;

        static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (waySelected == false)
            {
                if (_countDown-- <= 0)
                    Console.WriteLine("You got crushed!");
                else
                {
                    Console.SetCursorPosition(0, 9);
                    Console.WriteLine(_countDown + " seconds until you are crushed");
                }
            }
        }

        static void Main(string[] args)
        {
            Timer aTimer = new Timer(1000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.Enabled = true;

            Random random = new Random();
            int north = random.Next(1, 5);
            int south = random.Next(1, 5);
            int east = random.Next(1, 5);
            int west = random.Next(1, 5);

            Console.WriteLine("You are in a room that is slowly closing in on you, it has only four exits.\n" +
                              "The four exits face: North, South, East and West. \n" +
                              "Press n for North, \n" +
                              "      s for South, \n" +
                              "      e for East, \n" +
                              "      w for West, \n" +
                              "Only one exit leads to outside, the rest lead to certain doooooooooooooom \n");

            Console.WriteLine("Press any key to continue...");

            ConsoleKeyInfo way = Console.ReadKey(true);
            waySelected = true;
            Console.Clear();

            switch (way.KeyChar)
            {
                case 'n':
                    Console.WriteLine("The Next room is looks like North ...");
                    break;
                case 's':
                    Console.WriteLine("The Next room is looks like South ...");
                    break;
                case 'e':
                    Console.WriteLine("The Next room is looks like East ...");
                    break;
                case 'w':
                    Console.WriteLine("The Next room is looks like West ...");
                    break;
                default:
                    Console.WriteLine("You choose wrong way, you got crushed!");
                    break;
            }

            Console.ReadKey(true);
        }
    }
}

它并不完美,但它有效:)

答案 1 :(得分:0)

对于实际的定时器逻辑,我建议使用Rx。在Visual Studio中添加Rx-Main NuGet包,然后这将为您提供一个每分钟调用的函数:

public static void UpdateTimer(long _)
{
    // do work to update the timer on the console
}

public static void Main()
{
    using (Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(UpdateTimer))
    {
        // code goes here that blocks while waiting for user input
        // when the using block is left, the UpdateTimer function will stop being called.
    }
}

关于放在UpdateTimer函数中的内容,这取决于您希望计时器的显示方式。我建议按照其他答案中的说明进行操作。