In Pygame, normalizing game-speed across different fps values

后端 未结 4 1162
迷失自我
迷失自我 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:31

    You can define the speed of your objects in pixels/second, then use the returned value from tick to know how much every object should move.

    Example

    import pygame
    ...
    player_speed = (20, 0)   # (x,y) pixels/second
    ...
    pygame.time.Clock() = clock
    elapsed = 0.
    while True:
        seconds = elapsed/1000.0
        update_player(seconds)
        ...
        elapsed = clock.tick(60)
    
    def update_player(seconds):
        player.position = (player.position[0] + int(player_speed[0]*seconds), 
                           player.position[1] + int(player_speed[1]*seconds))
    

提交回复
热议问题