我在邏輯中遇到錯誤,無法弄清楚它是什麼。基本上,我不斷計算遊戲循環的每次迭代的時間跨度,並將該持續時間添加到之前的持續時間。我正在計算遊戲的總時間。當然,它不會產生正確的結果。我究竟做錯了什麼?任何指導非常感謝。時間範圍邏輯錯誤
private TimeSpan totalDuration = TimeSpan.FromSeconds(1);
private int score = 0;
public void Stop()
{
IsGameOver = true;
//MessageBox.Show(String.Format("Game Over\n\nScore = {0}", score));
MessageBox.Show(String.Format("Game Over\n\nScore = {0}\n\nTime
Duration={1}", score, totalDuration));
Application.Exit();
}
public void Start()
{
score = 0;
IsGameOver = false;
currentRedLightX = 0;
currentRedLightY = 0;
currentGreenLightX = width/2;
currentGreenLightY = height/2;
double minIterationDuration = SPEED; // 50 frames/sec
//game loop
while (!IsGameOver)
{
if (IsCollision())
{
score += 10;
}
DateTime startIterationTime = System.DateTime.UtcNow;
UpdateGameState();
Render();
DateTime endIterationTime = System.DateTime.UtcNow;
TimeSpan iterationDuration = endIterationTime - startIterationTime;
totalDuration += iterationDuration;
//totalDuration += iterationDuration.Duration();
if (iterationDuration.TotalMilliseconds < minIterationDuration)
Thread.Sleep(Convert.ToInt32(minIterationDuration -
iterationDuration.TotalMilliseconds));
Application.DoEvents();
}
聽起來不錯,但我該怎麼做? – Mike 2010-11-26 15:28:47