Framerate affect the speed of the game

自古美人都是妖i 提交于 2020-12-18 05:00:10

问题


I am practicing on pygame and I was wondering how can we do so that the framerate does not affect the speed of execution of the game

I would like FPS to not be locked and the game to always run at the same speed.

Until now I used the pygame.time.Clock.tick function but the speed of the character was changing depending on the number of FPS, which I don't want.


回答1:


pygame.time.Clock.tick returns the number of milliseconds passed since the previous call. If you call it in the application loop, then this is the number of milliseconds passed since the last frame.
Multiply the velocity of the player by the passed time per frame, to get a constant movement independent on the FPS.

For instance define the distance in number of pixel, which the player should move per second (move_per_second). Then compute the distance per frame in the application loop:

move_per_second = 500
FPS = 60
fun = True
clock = pygame.time.Clock() 
while run:
    ms_frame = clock .tick(FPS)
    move_per_frame = move_per_second * ms_frame / 1000  

    # [...]


来源:https://stackoverflow.com/questions/61352366/framerate-affect-the-speed-of-the-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!