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

查看:1077
本文介绍了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.13060设置什么fps,get_ticks()仍会返回以mls为单位的实时时间.

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 是每帧之间应该经过的时间.
刻度只是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天全站免登陆