"""Frame-rate independent movement: the same speed at 30, 60 or 144 FPS.""" import pygame WIDTH, HEIGHT = 640, 360 SPEED = 200 # pixels per second, not per frame pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() font = pygame.font.Font(None, 28) fps_options = [30, 60, 144] fps_index = 1 x = 0.0 running = True while running: dt = clock.tick(fps_options[fps_index]) / 1000 # seconds since last frame for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: fps_index = (fps_index + 1) % len(fps_options) # cycle the cap x += SPEED * dt if x > WIDTH: x -= WIDTH + 40 screen.fill((20, 26, 46)) pygame.draw.rect(screen, (244, 197, 49), (round(x), 160, 40, 40)) info = f"Cap {fps_options[fps_index]} FPS | actual {clock.get_fps():.0f} | Space changes the cap" screen.blit(font.render(info, True, (230, 236, 247)), (16, 16)) pygame.display.flip() pygame.quit()