Home Game Development python – How fps limiting works in pygame (particular instance)

python – How fps limiting works in pygame (particular instance)

0
python – How fps limiting works in pygame (particular instance)

[ad_1]

I not too long ago wrote a program the place I wanted to restrict the fps. After some looking out I discovered a approach through pygame built-in strategies pygame.time.Clock() and clock.tick(fps) however a fair higher, extra correct approach by the time module. But now comes the tough half and that’s delta time and its use on this instance. As far as I do know it’s used to measure the time between frames to have the ability to calculate the motion of an object in actual time. So my query is, how does dt assist me restrict fps and what’s the which means of the sleep_time variable on this instance?

I’ll be thankful for any clarification and try to assist with this matter

A fast be aware: I’m additionally fairly new to python and I’m nonetheless making an attempt to grasp it, however not programming generally and I already grasp the fundamentals. What I do not perceive is the algorithm behind the scenes. So that is principally all I need assistance with. Thank you.

# Modules ---------------------------------------- #

import pygame as pg, time

# Initialization --------------------------------- #

pg.init()
pg.show.set_mode((720, 400))

# Variables -------------------------------------- #

prev_time = time.time()
FPS = 60
working = True

# Main-loop -------------------------------------- #
whereas working:
    current_time = time.time()
    dt = prev_time - current_time
    prev_time = current_time
    sleep_time = 1./FPS - dt
    if sleep_time > 0:
        time.sleep(sleep_time)
    for occasion in pg.occasion.get():
        if occasion.kind == pg.QUIT:
            working = False

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here