pygame clock.tick() 与游戏主循环中的帧率 [英] pygame clock.tick() vs framerate in game main loop

查看:48
本文介绍了pygame clock.tick() 与游戏主循环中的帧率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

根据get_ticks()的api:

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

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

但是 clock.tick() :

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

This method should be called once per frame. It will compute how many . milliseconds have passed since the previous call.

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

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.

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

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(40) 意味着我每秒发出"40 帧,while 循环每秒运行 40 次?

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

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

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

更新:我实际上只是对其进行了测试,无论您给 tick() - 0.130 什么 fps,get_ticks() 仍然以 mls 为单位返回实时时间或 60.

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.

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

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.

推荐答案

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

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) 表示每秒最多 40 帧应该通过.

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

这篇关于pygame clock.tick() 与游戏主循环中的帧率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆