我正在为学校做一个基本的俄罗斯方块游戏。我不知道如何开发一个游戏循环,因为这是我第一次做游戏。
我正在用opengl来绘制这幅图。我是否做了一个主循环,等待一定的时间才能重新绘制场景?
发布于 2013-11-05 22:03:17
下面是一个基本的(非常高级别的伪码)游戏循环:
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,这样它们的值就可以以单位每秒的单位为单位。
abstract class Unit
{
public:
abstract void update(float deltaTime);
}
FallingBlockUnit::update(float deltaTime)
{
moveDown(fallSpeed * deltaTime);
}抽签经理将负责管理绘图缓冲区(我建议双缓冲以防止屏幕闪烁)
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
https://stackoverflow.com/questions/19799722
复制相似问题