且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何制作“循环"?对于游戏,每(数)毫秒运行一次?

更新时间:2023-02-25 15:13:32

如果你想让它运行 100 毫秒,你可以这样做

If you want it to run for 100 ms you would do this

System.Timers.Timer timer = new System.Timers.Timer(100);
public void Initialize(object sender, EventArgs e)
{
  timer.Elapsed+=Elapsed;
}
public void Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
  //do stuff
}

你可以做一些其他的事情,但我认为这个没有那么有效

Something else you can do, however I don't think this one is as efficient

        using System.Threading;

        Thread th = new Thread(new ThreadStart(delegate
        {
            while (true)
            {
                Thread.Sleep(100);
                //do stuff
            }
        }));

或者,你可以下载monogame,用c#制作更精致的游戏

Or, you can download monogame to make more elaborate games in c#

http://www.monogame.net/

它有自己的游戏时间控制.

It has its own gameTime control.