在Pygame中,将游戏速度标准化为不同的fps值(In Pygame, normalizing game

编程入门 行业动态 更新时间:2024-10-16 18:24:38
在Pygame中,将游戏速度标准化为不同的fps值(In Pygame, normalizing game-speed across different fps values)

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

根据我的理解,fps通常设置为:

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

然后在整个程序中,我确保每个循环/帧被写入需要1/60秒,所以我可以,例如,让对象以我想要的速度移动。 将刻度线增加到120 fps将使游戏运行得太快,而减少它会使游戏运行得太慢。

然而,这并不是我熟悉fps为任何其他游戏工作的方式。 在大多数游戏中,fps可以根据需要变化(通常基于系统运行应用程序的程度),但游戏总是以相同的速度运行(例如,在屏幕上移动100个像素将花费1秒钟,无论如何在那一秒发生了多少帧)。

我能想到让它按照我想要的方式工作的唯一方法是每帧捕获当前的fps,并将其计入每个基于运动或时间的事件的计算中。 但这似乎是不必要的复杂,我想知道我是否完全错过了pygame功能的一部分,这对我来说是有用的。

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.

From what I understand, fps is generally set with:

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

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.

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).

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)变化。 这意味着您将使用常量delta值调用update(delta)函数。 这保持了稳定性。

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

有关详细信息,请参阅: Gaffer的“修复您的时间步长”

一个更大的(python)示例是在cookbook:ConstantGametime

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.

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.

For details see: Gaffer's "fix your timestep"

A larger (python) example is at cookbook: ConstantGametime

更多推荐

本文发布于:2023-08-05 04:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   标准   游戏   Pygame   normalizing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!