首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Opengl:基于时间的俄罗斯方块游戏是如何进行的?

Opengl:基于时间的俄罗斯方块游戏是如何进行的?
EN

Stack Overflow用户
提问于 2013-11-05 21:49:34
回答 1查看 1K关注 0票数 0

我正在为学校做一个基本的俄罗斯方块游戏。我不知道如何开发一个游戏循环,因为这是我第一次做游戏。

我正在用opengl来绘制这幅图。我是否做了一个主循环,等待一定的时间才能重新绘制场景?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-05 22:03:17

下面是一个基本的(非常高级别的伪码)游戏循环:

代码语言:javascript
复制
void RunGameLoop()
{
  // record the frame time and calculate the time since the last frame
  float timeTemp = time();
  float deltaTime = timeTemp - lastFrameTime;
  lastFrameTime = timeTemp;

  // iterate through all units and let them update
  unitManager->update(deltaTime);

  // iterate through all units and let them draw
  drawManager->draw();
}

将deltaTime (从秒内的最后一个帧起)传递给unitManager->update()的目的是,当单元正在更新时,它们可以将它们的移动乘以deltaTime,这样它们的值就可以以单位每秒的单位为单位。

代码语言:javascript
复制
abstract class Unit
{
  public:
  abstract void update(float deltaTime);
}

FallingBlockUnit::update(float deltaTime)
{
  moveDown(fallSpeed * deltaTime);
}

抽签经理将负责管理绘图缓冲区(我建议双缓冲以防止屏幕闪烁)

代码语言:javascript
复制
DrawManager::draw()
{
  // set the back buffer to a blank color
  backBuffer->clear();

  // draw all units here

  // limit the frame rate by sleeping until the next frame should be drawn
  // const float frameDuration = 1.0f / framesPerSecond;
  float sleepTime = lastDrawTime + frameDuration - time();
  sleep(sleepTime);
  lastDrawTime = time();

  // swap the back buffer to the front
  frontBuffer->draw(backBuffer);
}

为了进一步研究,我的游戏编程教授写了一本关于2d游戏编程的书。http://www.amazon.com/Graphics-Programming-Games-John-Pile/dp/1466501898

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19799722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档