我正在創建一個遊戲...我想添加一個計數器作爲一個時鐘完全一樣的時間計數器在微軟solitair遊戲&我希望以後當遊戲結束時間停止和時間達到被保存在一個變量中,但是我想用這個數字來生成一個分數表。狀態條和時間
thanx提前.... 我將張貼了整場比賽時,其100%這樣做,你們可以享受:-)
我正在創建一個遊戲...我想添加一個計數器作爲一個時鐘完全一樣的時間計數器在微軟solitair遊戲&我希望以後當遊戲結束時間停止和時間達到被保存在一個變量中,但是我想用這個數字來生成一個分數表。狀態條和時間
thanx提前.... 我將張貼了整場比賽時,其100%這樣做,你們可以享受:-)
你可以使用一個秒錶對象。
下面是關於如何使用它的鏈接: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
要啓動秒錶做這樣的:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
要停止:
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed; // gets elapsed time
要顯示的耗用時間:
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10);
現在,您可以在條上顯示'elapsedtime'字符串。
編輯:
要查看移動時間,把下面的按鈕單擊處理程序啓動時間:
// global variable
string timeelepse = string.empty;
// in button click handler
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
System.Threading.Thread t = new System.Threading.Thread(delegate()
{
while (true)
{
TimeSpan ts = sw.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10);
timeelepse = elapsedTime;
UpdateLabel();
}
});
t.Start();
現在加上這兩個功能&委託給你的表單類:
public delegate void doupdate();
public void UpdateLabel()
{
doupdate db = new doupdate(DoUpdateLabel);
this.Invoke(db);
}
public void DoUpdateLabel()
{
toolStripStatusLabel1.Text = timeelepse;
}
什麼是你的問題? – albertein 2010-01-03 17:54:34
如何創建這個時間,並在遊戲結束時讓它停止並節省時間 – Tony 2010-01-03 17:56:28
請不要在這裏發佈整個遊戲。不要使用StackOverflow作爲您的個人博客。 – 2010-01-03 18:01:13