3

我遇到了均匀移动精灵的问题;目前我正在使用while循环来移动它们,问题是计算机越快,循环越快,精灵移动得越快。我在 pygame 中尝试了计时器/时钟功能(等待?),它在等待时冻结光标,因此使光标跳动。

多线程是答案吗?

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

4

2 回答 2

9

你依赖于帧率,帧率越快,你的运动就会越快。

通常,我们计算 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

然后运动跟随帧率:如果你的循环更快,那么增量更低,然后每帧的运动更慢=>无论帧率是多少,结果都将具有相同的速度。

你现在是独立的帧率。

于 2012-02-14T12:31:50.837 回答
0

我在这里找到了一个处理这个问题的线程:

尝试以下操作:

clock = pygame.time.Clock()
while True:
    if clock.tock(60): #Limit to 60fps
        ... #Update game display here
    else:
        cursor.update()
于 2012-02-14T12:32:14.907 回答