在 Pygame 中,标准化不同 fps 值的游戏速度 [英] In Pygame, normalizing game-speed across different fps values

查看:54
本文介绍了在 Pygame 中,标准化不同 fps 值的游戏速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 Pygame,制作一些简单的游戏只是为了学习它.但是,我很难按照我想要的方式实现 fps.

I'm messing around with Pygame, making some simple games just to learn it. However, I'm having a hard time implementing fps the way that I want.

据我了解,fps 通常设置为:

From what I understand, fps is generally set with:

import pygame
...
clock = pygame.time.Clock()
while True:
    clock.tick(60)

然后在整个程序中,我确保每个循环/帧的写入时间为 1/60 秒,因此我可以,例如,让对象也以我想要的速度移动.将刻度增加到 120 fps 会使游戏运行过快,而减小它会使游戏运行过慢.

And then throughout the program, I make sure that every loop/frame is written to take 1/60th of a second, so I can, for example, make objects move at the speed I want them too. Increasing the tick to 120 fps will make the game run too fast, while decreasing it will make the game run too slow.

然而,这不是我熟悉的任何其他游戏的 fps 工作方式.在大多数游戏中,fps 可以随心所欲地变化(通常取决于系统运行应用程序的程度),但游戏将始终以相同的速度运行(例如,在屏幕上移动 100 个像素将花费 1 秒,无论那一秒发生了多少帧).

However, this is not how I'm familiar with fps working for any other game. In most games, fps can vary as much as you want (usually based on how well the system is running the application), but the game will always run at the same speed (e.g. moving 100 pixels across the screen will take 1 second no matter how many frames happened in that second).

我能想到的让它以我想要的方式工作的唯一方法是每帧获取当前的 fps,并将其纳入每个运动或基于时间的事件的计算中.但这似乎不必要地复杂,我想知道我是否完全遗漏了 pygame 功能的一部分,它为我解决了这个问题.

The only way I can think of getting it to work the way I want is to grab the current fps every frame, and factor that into the calculations of every movement or time based event. But that seems needlessly complicated, and I'm wondering if I'm completely missing a part of pygame functionality that figures that out for me.

推荐答案

游戏使用固定的物理时间步长,同时允许视频时间步长 (fps) 变化.这意味着您的 update(delta) 函数被调用时带有一个常量 delta 值.这样可以保持稳定性.

Games use a fixed-timestep for physics, while allowing the video timestep (fps) to vary. This means your update(delta) function gets called with a constant delta value. This maintains stability.

这意味着在实践中,update 可能会在每次 draw() 的单次调用中平均被调用多次,具体取决于经过的时间.

This means in practice, update may end up being called multiple times on average per single call of draw(), depending on how much time elapses.

详情参见:Gaffer 的修复你的时间步长"

一个更大的 (python) 示例位于 cookbook: ConstantGametime

A larger (python) example is at cookbook: ConstantGametime

这篇关于在 Pygame 中,标准化不同 fps 值的游戏速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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