In Pygame, normalizing game-speed across different fps values

后端 未结 4 1158
迷失自我
迷失自我 2021-01-05 12:05

I\'m messing around with Pygame, making some simple games just to learn it. However, I\'m having a hard time implementing fps the way that I want.

From what I unders

4条回答
  •  伪装坚强ぢ
    2021-01-05 12:16

    If you want to move an object at a fixed speed at every FPS you can indicate speed not with an absolute value, but with a value relative to the movement to be done in a second, then divide that by the number of frames per second.

    For example, if you want your object to move at a speed of 2 pixels per frame, let's say at 30 FPS, and you want this speed to be the same at every FPS, you can indicate it as the pixel movement in a second, which would be 60. In the game loop, when you need to update the speed, you'll use this speed divided by FPS, thus resulting in the relative speed used in every frame update. In this way, at 30 FPS you'll get a 2 pixel movement for each frame, while at 60 FPS you'll get a 1 pixel movement, but after 1 second you will have the same amount of pixel movement, which is 60.

提交回复
热议问题