如何使精灵在 Pygame & 中的所有计算机上均匀移动Python
我在均匀移动精灵时遇到问题;目前我正在使用 while 循环来移动它们,问题是计算机越快,循环越快,精灵移动得越快。我在 pygame 中尝试过计时器/时钟功能(等待?),它在等待时冻结光标,因此使光标跳动。
多线程是答案吗?
这是我的问题的视频; http://www.youtube.com/watch?v=cFawkUJhf30
I'm having a problem with evenly moving sprites; Currently I'm using a while loop to move them, the problem with this being the faster the computer, the faster the loop goes and the faster the sprites move. I have tried the timer / clock function (wait?) in pygame and it freezes the cursor while it waits, therefore maiking the cursor jumpy.
Is multi-threading the answer?
Here's a video of my problem;
http://www.youtube.com/watch?v=cFawkUJhf30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你依赖于帧速率,帧速率越快,你的移动就会越快。
通常,我们计算 2 帧/循环迭代之间的时间,我们将其称为“增量时间”。然后我们将该增量时间乘以运动向量。
这是一个循环示例:
然后运动遵循帧速率:如果循环更快,则增量更低,然后每帧的运动更慢 =>无论帧速率是多少,结果都将具有相同的速度。
您现在与帧速率无关。
You're dependent of the framerate, faster the framerate is, faster your movement will be.
Usually, we are calculating the time between 2 frames/loop iteration, and we called it the "delta time". Then we multiply that delta time to the movement vector.
Here is a loop sample:
Then the movement are following the framerate: if your loop is faster, then the delta is lower, and then the movement are slower per frame => the result will have the same speed whatever the framerate is.
You're now framerate independent.
我在此处找到了一个处理此问题的线程:
尝试以下操作:
I found a thread dealing with this problem here:
Try the following: