我如何让精灵在Pygame&Python中的所有电脑上均匀移动
我在均匀移动精灵时遇到了问题;目前我正在使用while循环来移动它们,问题在于计算机速度越快,循环越快,精灵移动的速度越快。我已经尝试了pygame中的定时器/时钟函数(等待?),它在等待时冻结了光标,因此使光标跳动。我如何让精灵在Pygame&Python中的所有电脑上均匀移动
是多线程的答案?
这里是我的问题的视频; 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
然后移动跟随帧率:如果你的循环速度更快,那么增量更低,然后将移动每帧=>结果越慢无论帧率如何,速度都相同。
你现在独立于帧率。
作为一个方面认为,将'上限'放在'dt'上是明智的,以防发生负载峰值。 – badp 2012-02-14 12:35:10
mx是什么定义的? – 2012-02-14 12:56:34
没关系,它现在没有'px * = mx *速度 py * =我*速度'位 ** Thankyou这么多** – 2012-02-14 12:59:34
我发现一个线程处理这个问题here:
尝试以下操作:
clock = pygame.time.Clock()
while True:
if clock.tock(60): #Limit to 60fps
... #Update game display here
else:
cursor.update()
+1的视频! – badp 2012-02-14 12:36:34