Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
883 views
in Technique[技术] by (71.8m points)

python - pygame clock.tick() vs framerate in game main loop

Every pygame has a game loop that looks like this:

while running:
    for event in pygame.event.get():        
        if event.type == pygame.QUIT:
            running = False   
    pygame.display.flip()
    print("tick " + str(pygame.time.get_ticks()))
    clock.tick(1)

According to the api forget_ticks():

Returns the number of millisconds since pygame.init() was called. Before pygame is initialized this will always be 0.

But clock.tick() :

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.

I'm a bit confused, does it mean that clock.tick() directly affects how many milliseconds have passed since the start of the game?

So clock.tick(40) means I "issue" 40 frames per second and the while loop runs 40 times per second?

I don't see the relation between fps and ticks.

UPDATE: I actually just tested it and get_ticks() still returns REAL time in mls no matter what fps you give to tick() - 0.1 or 30 or 60.

So it seems clock.tick() just sets up how fast game should run or how often while loop should update itself, run through itself.

However I m still a bit confused, other answers are welcome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

FPS, Frames Per Second, is the number of frames shown per unit of time.
1 / FPS is the amount of time should pass between each frame.
Tick is just a measure of time in PyGame.

clock.tick(40) means that for every second at most 40 frames should pass.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...