2013-10-19 32 views
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"); 
     } 
    } 
} 
+2

玩我的答案[这里](http://stackoverflow.com/a/16973768/2330053)和[这里](http://stackoverflow.com/a/16907575/2330053)的一些想法如何做这个。 –

回答

0

为了启动,请访问这个线程: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); 
     } 
    } 
} 

它不是完美的,但它的工作原理:)

+0

如果我想这样做,那么每秒钟之后打勾的数字呢?感谢您的帮助顺便说一句:) – Bacon

+0

请进一步阅读本文,Idle_Mind的回答是enugh作为我thnik。您可以在右上角看到经过的时间。链接(http://stackoverflow.com/questions/16970277/issue-with-multi-threading/16973768#16973768)。这就是你需要的 – Krekkon

+0

这是我的代码,我相信我已经正确实现它,但它不会显示代码?也不,如果我做Console.WriteLine(_countDown)将显示一个递减的数字。我目前的代码:http://pastebin.com/YidDk527 – Bacon

0

对于实际的定时器逻辑我会建议使用的Rx。在Visual Studio中添加RX-主要的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功能,这取决于你想如何计时器出现。我建议按照其他答案中的说明进行操作。

相关问题