我在均匀移动 Sprite 时遇到问题;目前我正在使用 while 循环来移动它们,问题是计算机越快,循环越快, Sprite 移动得越快。我在 pygame 中尝试了计时器/时钟功能(等等?),它在等待时卡住了光标,因此使光标跳动。
多线程是答案吗?
这是我的问题的视频; http://www.youtube.com/watch?v=cFawkUJhf30
最佳答案
你依赖于帧率,帧率越快,你的运动就会越快。
通常,我们计算 2 帧/循环迭代之间的时间,我们称之为“增量时间”。然后我们将该增量时间乘以运动矢量。
这是一个循环示例:
clock = pygame.time.Clock()
while True:
# limit the framerate and get the delta time
dt = clock.tick(60)
# convert the delta to seconds (for easier calculation)
speed = 1 / float(dt)
# do all your stuff, calculate your heroes vector movement
# if heroes position is "px, py" and movement is "mx, my"
# then multiply with speed
px *= mx * speed
py *= my * speed
然后运动将遵循帧率:如果您的循环速度更快,则增量会更低,然后每帧运动更慢 => 无论帧率是多少,结果都将具有相同的速度。
您现在独立于帧率。
关于python - 如何让 Sprite 在 Pygame 和 Python 中的所有计算机上均匀移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9276790/