1

所以我正在一步一步制作一个基本的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");
         }
    }
}
4

2 回答 2

2

要开始,请访问此线程:链接 之后您可以看到.

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);
        }
    }
}

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

于 2013-10-19T13:24:54.487 回答
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 函数中放入什么,这取决于您希望计时器如何出现。我建议按照其他答案中的说明进行操作。

于 2013-10-20T00:18:35.200 回答