c++ - 在控制台上调整 2 个不同对象的速度
问题描述
这是一个非常蹩脚的问题,我很抱歉。
解决方案
你不想使用sleep_for
. 它可以睡更长时间,并且不提供时间保证。你想要它一个固定的更新游戏循环。您可以查看流行的游戏引擎以了解它是如何实现的。
但归结为以下伪代码:
//mainloop
// get a number of frame based on time.
// take a number of frame higher than expected frame rate. say 10 ms.
int frame = now() / frameDuration;
while(true)
{
int frameNow = now() / frameDuration;
while(frame != frameNow)
{
Update();
frame++;
}
render();
}
具有以下更新功能:(再次伪代码)
// there, you advance you object for a fixing duration, say 10 ms.
// just use different speed for each
Update()
{
obstacle.position += obstacleSpeed * frameDuration;
character.position += characterSpeed * frameDuration;
}
推荐阅读
- laravel - 查看未找到 laravel , Heroku
- curl - 在 Jenkins 管道中的 for 循环中执行的发布请求会引发错误 org.apache.commons.collections.map.ListOrderedMap$ListOrderedMapEntry
- python - 将 Modbus TCP 数据转换为可读格式
- javascript - 如何让谷歌地图标记的信息窗口工作?
- swift - Swift 5 转换:FRadio 代码。“没有更多上下文,表达的类型是模棱两可的”
- python-sphinx - 为序列化参数接口创建使用文档
- angular - 在不模拟的情况下从 Angular 测试 REST 服务器
- javascript - javascript中的谷歌图表高度
- python - 如何使用 urllib2 调用网站?
- java - 导出 Java 项目时,我的图形不会显示