Pygame程序中的奇怪延迟

2024-03-28 18:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pygame中运行一个小游戏,在主循环的每次迭代之间需要一个恒定的延迟。虽然我把FPS数锁定在60pygame.Clock.tick\u busy\u循环(),我观察到延迟的变化(每个循环大约4毫秒)。我的一个函数应该被窃听,但我很难找到哪一个。你知道吗

我在Ipython的主循环中使用了一个%prun,结果如下:

         2264 function calls in 5.439 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      322    4.401    0.014    4.401    0.014 {method 'tick' of 'Clock' objects}
      322    0.730    0.002    0.730    0.002 {pygame.display.flip}
        1    0.228    0.228    0.228    0.228 {pygame.base.quit}
        1    0.034    0.034    0.034    0.034 {method 'blit' of 'pygame.Surface' objects}
        1    0.028    0.028    5.439    5.439 main.py:9(main)
      323    0.014    0.000    0.014    0.000 {pygame.event.get}
      323    0.002    0.000    0.016    0.000 lib.py:26(getInput)
      322    0.001    0.000    0.036    0.000 lib.py:80(checkEventType)
      322    0.001    0.000    0.035    0.000 lib.py:91(instructions)
      322    0.000    0.000    0.000    0.000 {method 'get_time' of 'Clock' objects}
        1    0.000    0.000    5.439    5.439 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {pygame.mixer.stop}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {ord}

这里有什么可以解释每次迭代之间延迟2-5毫秒的变化吗?你知道吗


Tags: ofpygetobjectstimemainlibfunction
2条回答

你的程序不是电脑上唯一的程序。系统必须同时运行其他程序。有时做得快,有时慢。2-5毫秒没什么好担心的。你知道吗

你可能还想研究一下游戏时间时钟.tick()而不是pygame.Clock.tick\u busy\u循环():

http://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick

Note that this function uses SDL_Delay function which is not accurate on every platform, but does not use much cpu. Use tick_busy_loop if you want an accurate timer, and don’t mind chewing cpu.

当然,除非你严格要求帧速率保持在60。你知道吗

相关问题 更多 >