clock.tick() in the Pygame documentation:
tick()
update the clock
tick(framerate=0) -> milliseconds
This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.
If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling
Clock.tick(40)once per frame, the program will never run at more than 40 frames per second.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.
So the clock.tick() returns the time since the last call to clock.tick(), use that value to multiply all your speeds with it when you move.
Example
dt = clock.tick(60)
player.position.x += player.xSpeed * dt
player.position.y += player.ySpeed * dt
This way your player will always move at the same speed regardless of what you put into the clock.tick() function
clock.tick() in the Pygame documentation:
tick()
update the clock
tick(framerate=0) -> milliseconds
This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.
If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling
Clock.tick(40)once per frame, the program will never run at more than 40 frames per second.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.
So the clock.tick() returns the time since the last call to clock.tick(), use that value to multiply all your speeds with it when you move.
Example
dt = clock.tick(60)
player.position.x += player.xSpeed * dt
player.position.y += player.ySpeed * dt
This way your player will always move at the same speed regardless of what you put into the clock.tick() function
I think you would need multi threading.
But you might run into issues if the computer doesn't have times to compute everything within 1/500s.
python - How fps limiting works in pygame (specific example) - Game Development Stack Exchange
performance - Setting a fixed frame rate in Pygame, Python 3 - Stack Overflow
python - Show FPS in Pygame - Stack Overflow
How to prevent game speed from being controlled by FPS?
get_fps() only gives a correct result, when you call tick() once per frame:
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
clock.tick()
print(clock.get_fps())
# [...]
See the documentation of get_fps()
get_fps()compute the clock framerate
get_fps() -> floatCompute your game's framerate (in frames per second). It is computed by averaging the last ten calls to
Clock.tick().
Initialize your clock
clock = pygame.time.Clock()
Add your font
font = pygame.font.SysFont("Arial" , 18 , bold = True)
And your fps function
def fps_counter():
fps = str(int(clock.get_fps()))
fps_t = font.render(fps , 1, pygame.Color("RED"))
window.blit(fps_t,(0,0))
Call your function in loop
fps_counter()
#dont forget to add tick(60) to run 60 frame per sec
In most games, the speed of the game (things like movement speed) are the same regardless of whether you have 200 FPS, or just 30 FPS. The problem is that every pygame game I've made has had game-speed controlled directly by the clock. If I double the clock.tick argument, the game-speed doubles. This is something I would like to prevent.
I have tried putting the pygame.display.flip function on a separate thread to my main game loop, so that changes in FPS change how frequently the display updates without changing the rate at which the game is processed, however this leads to a huge stuttery mess.
Is there a simple method to having game-speed and FPS separate?