gpt4 book ai didi

c# - 时间跨度逻辑错误

转载 作者:行者123 更新时间:2023-11-30 14:03:23 28 4
gpt4 key购买 nike

我的逻辑有误,我无法弄清楚它是什么。基本上,我连续计算游戏循环每次迭代的时间跨度,并将该持续时间添加到它之前的持续时间。我正在尝试计算玩游戏的总时间。当然,它不会产生正确的结果。我究竟做错了什么?非常感谢任何指导。

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

最佳答案

使用 StopWatch 而不是时间跨度计算耗时的类:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// Do stuff ...
stopWatch.Stop();

关于c# - 时间跨度逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4286421/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com