Python pygame clock.tick() 与游戏主循环中的帧率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34383559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:55:01  来源:igfitidea点击:

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

pythonpygameframe-ratepygame-clockpygame-tick

提问by ERJAN

Every pygame has a game loop that looks like this:

每个 pygame 都有一个游戏循环,如下所示:

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():

根据 api for get_ticks()

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

返回自调用 pygame.init() 以来的毫秒数。在 pygame 初始化之前,这将始终为 0。

But clock.tick():

但是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 slowerthan 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.

此方法应每帧调用一次。它将计算多少 . 自上次调用以来已经过去了几毫秒。

如果您传递可选的帧速率参数,该函数将延迟以保持游戏运行速度低于每秒给定的滴答数。这可用于帮助限制游戏的运行速度。通过每帧调用一次 Clock.tick(40),程序将永远不会以超过 40 帧/秒的速度运行。

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

我有点糊涂了,是不是clock.tick()直接影响到游戏开始后已经过去了多少毫秒?

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

那么clock.tick(40)意味着我每秒“发出”40 帧,而 while 循环每秒运行 40 次?

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

我没有看到 fps 和刻度之间的关系。

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

更新:我其实只是测试它,并get_ticks()在大联盟仍返回实时不管fps的你给tick() - 0.13060

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

所以它似乎clock.tick()只是设置了游戏应该运行的速度或者while 循环应该多久更新一次,运行自己

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

但是我仍然有点困惑,欢迎其他答案。

采纳答案by ModoUnreal

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

FPS每秒帧数,是每单位时间显示的帧数。
1 / FPS是每帧之间应该经过的时间量。
Tick只是 PyGame 中的时间度量。

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

clock.tick(40)意味着每秒最多应该通过 40 帧。

回答by ERJAN

I set up high fps - clock.tick(30) or 60, and game runs fast, and get_ticks() prints out elapsed time very fast however actual runtime from pygame.init() did not change!

我设置了高 fps - clock.tick(30) 或 60,游戏运行速度很快,get_ticks() 打印出经过的时间非常快,但是 pygame.init() 的实际运行时间没有改变!

I thought time runs faster because of high FPS! It does not, I tried clock.tick(0.1) - aka 1 frame per 10 seconds, and get_ticks() printed out its elapsed time only ONCE PER 10 seconds!Because the while loop was running through itself at fps = 0.1.

我认为时间过得更快,因为 FPS 高!它没有,我尝试了 clock.tick(0.1) - 也就是每 10 秒 1 帧,并且 get_ticks() 每 10 秒只打印一次它的经过时间因为 while 循环以 fps = 0.1 的速度运行。

But if fps was higher, the update rate would be higher-not the total elapsed time

但是如果 fps 更高,则更新率会更高——而不是总经过时间

Now I figured it out.

现在我想通了。

回答by Sam

I know it is already answered but I wanted to explain something I tired

我知道已经有人回答了,但我想解释一些我厌倦的事情

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 

so what we will do is we will make two runs one with frame per second equals 1 and the other with 10, and we will run the code for 10 seconds " I used my phone stopwatch to do this".

所以我们要做的是进行两次运行,一次每秒帧数为 1,另一次为 10,我们将运行代码 10 秒“我用我的手机秒表来做到这一点”。

so mathematically 1 fps for 10sec is 10 right and 10 fps in 10sec is 100 "duuh" so what you should get running the first run "1 fps" is the counter variable should be around 10 "depends on your timing" and the second run at the end of the 10 second counter variable in your console should be around 100

所以数学上 10 秒的 1 fps 是 10 对,10 秒内的 10 fps 是 100“duuh”所以你应该运行第一次运行“1 fps”是计数器变量应该在 10 左右“取决于你的时间”和第二次运行在控制台中的 10 秒计数器变量结束时应该在 100 左右

so, in brief, we could say the loop is controlling your game display and clock.tick() specifies how fast you want to change the game display in other words how fast the loop runs

因此,简而言之,我们可以说循环正在控制您的游戏显示,而 clock.tick() 指定您想要更改游戏显示的速度,即循环运行的速度