帧率影响游戏速度 [英] Framerate affect the speed of the game

查看:216
本文介绍了帧率影响游戏速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 pygame 上练习,我想知道如何才能使帧率不影响游戏的执行速度

I am practicing on pygame and I was wondering how can we do so that the framerate does not affect the speed of execution of the game

我希望 FPS 不被锁定并且游戏始终以相同的速度运行.

直到现在我使用了 pygame.time.Clock.tick 函数但是角色的速度正在改变取决于 FPS 的数量,我不想要.

Until now I used the pygame.time.Clock.tick function but the speed of the character was changing depending on the number of FPS, which I don't want.

推荐答案

您必须根据帧速率计算每帧的移动.

You have to calculate the movement per frame depending on the frame rate.

pygame.time.Clock.tick 返回自上次调用以来的毫秒数.当您在应用程序循环中调用它时,这是自上一帧以来经过的毫秒数.将物体速度乘以每帧经过的时间,无论 FPS 多少,都会获得恒定的运动.

pygame.time.Clock.tick returns the number of milliseconds since the last call. When you call it in the application loop, this is the number of milliseconds that have passed since the last frame. Multiply the objects speed by the elapsed time per frame to get constant movement regardless of FPS.

例如定义玩家每秒应该移动的像素数距离(move_per_second).然后计算应用程序循环中每帧的距离:

For instance define the distance in number of pixel, which the player should move per second (move_per_second). Then compute the distance per frame in the application loop:

move_per_second = 500
FPS = 60
run = True
clock = pygame.time.Clock() 
while run:
    ms_frame = clock .tick(FPS)
    move_per_frame = move_per_second * ms_frame / 1000  

    # [...]

这篇关于帧率影响游戏速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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