Pygame clock.tick () versus frame rate in the main loop of the game

Each 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 api for get_ticks():

Returns the number of milliseconds since pygame.init () was called. Before pygame initialization, it will always be 0.

But clock.tick():

This method should be called once for each frame. He will calculate how much. milliseconds have passed since the previous call.

If you pass an optional argument to the frame rate, the function will be delayed so that the game continues more slowly than the specified ticks per second. This can be used to limit the speed of the game. By calling Clock.tick (40) once per frame, the program will never run at a speed of more than 40 frames per second.

, , clock.tick() , ?

, clock.tick(40) , "" 40 , while 40 ?

fps .

UPDATE: , get_ticks() - REAL time mls , fps tick() - 0.1 30 60.

, , clock.tick() , , , .

, .

+9
3

FPS, - , .
1/FPS - , .
- PyGame.

clock.tick(40) , 40 .

+8

fps - clock.tick(30) 60, , get_ticks() , pygame.init() !

, - FPS! , clock.tick(0.1) - aka 1 10 , get_ticks() ONCE PER 10 seconds! while fps = 0,1.

fps , -

.

+3

, , -,

import pygame

pygame.init()

gameDisplay = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()

crashed = False

counter = 1
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    pygame.display.update()
    print(counter)
    counter += 1
    clock.tick(1) # will be 10 in the next run 

, , , 1, 10, 10 . " ".

1 10 10 , 10 10 100 "duuh" "1 fps", counter 10 " ", 10- 100

So, in short, we could say that the loop controls the display of your game and clock.tick () indicates how fast you want to change the display of the game in other words, how fast the loop works

0
source

Source: https://habr.com/ru/post/1621002/


All Articles