游戏倒计时

本文关键字:倒计时 游戏 | 更新日期: 2023-09-27 17:59:02

我需要一个(可能)简单的代码来在游戏中进行倒计时。我不能使用锁定线程的方法,例如

Thread.Sleep();

特别是,我想为玩家创建一个时间限制,必须按下一系列键。如果玩家没有在10秒内完成,它就输了!

游戏倒计时

摘自MSDN-http://msdn.microsoft.com/en-us/library/ms149618.aspx?cs-保存lang=1&cs lang=csharp#code-snippet-1

public void StartTimer(int dueTime)
{
    Timer t = new Timer(new TimerCallback(TimerProc));
    t.Change(dueTime, 0);
}
private void TimerProc(object state)
{
    // The state object is the Timer object.
    Timer t = (Timer) state;
    t.Dispose();
    Console.WriteLine("The timer callback executes.");
}

试试这个:

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=10 * 1000;//ten seconds
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
     //do whatever you want 
}