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.
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