zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

- Game loop #118

Closed zzz6519003 closed 4 years ago

zzz6519003 commented 5 years ago
var interval;  // 声明 interval,用来标识触发器

void main()
{
  init();
  interval = setInterval(update, 1/30);  // 设定 1/30 秒触发一次,执行 update
}
void update()
{
  if(KeyDown("Escape")) {
    clearInterval(interval);  // 退出的话,停止事件跟踪
    shutdown();
  }
  player.x += 1;

  draw();
}
void main()
{
  float totaltime = GetTime();
  float lasttime = 0;
  float deltatime = 0;

  init();
  while(running)
  {
    lasttime = totaltime;
    totaltime = GetTime();
    deltatime = totaltime - lasttime;

    update(deltatime); 
    draw();

    if(KeyDown("Escape"))
      running = false;
  }
  shutdown();
}
void update(float deltatime)
{
  player.x += deltatime
}
void main()
{
  bool running = true;

  init(); // 初始化整个体系,框架、图形、声音等等

  while(running)
  {
    update(); // 执行游戏逻辑
    draw(); // 绘制游戏

    if(KeyDown("Escape"))
      running = false;
  }

  exit(); // 退出,关闭各种接口等
}