如何让精灵在Pygame和Python中的所有计算机上均匀移动

Jac*_*don 3 python multithreading pygame sprite python-multithreading

我在均匀移动精灵方面遇到了问题; 目前我正在使用while循环移动它们,问题是计算机越快,循环越快,精灵移动得越快.我已经尝试了pygame中的定时器/时钟功能(等待?),并且它在等待时冻结光标,因此将光标按到跳跃状态.

多线程的答案是什么?

这是我的问题视频; http://www.youtube.com/watch?v=cFawkUJhf30

tit*_*ito 9

你依赖于帧率,帧速率越快,你的移动速度就越快.

通常,我们计算2帧/循环迭代之间的时间,我们将其称为"增量时间".然后我们将该delta时间乘以运动矢量.

这是一个循环示例:

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
Run Code Online (Sandbox Code Playgroud)

然后移动遵循帧速率:如果你的循环更快,那么delta更低,然后每帧的移动速度更慢=>无论帧速率如何,结果都将具有相同的速度.

你现在独立于帧率.