【发布时间】:2023-03-31 22:26:02
【问题描述】:
我作为我们的玩家做了一个圆圈并设置了重力,但它的工作速度非常快,我什至看不到。我认为它必须与 sf::time 或 sf::clock 做一些事情,但如何?我实际上并没有得到这两个,所以任何代码示例和解释都将不胜感激。这是我的代码:-
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::CircleShape shape(20);
shape.setFillColor(sf::Color::White);
shape.setPosition(380, 550);
shape.setOutlineColor(sf::Color::Green);
shape.setOutlineThickness(3);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
shape.move(0, -100);
}
if(event.KeyReleased)
{
if(event.key.code == sf::Keyboard::Up)
{
int x[] = { 10, 10, 10, 10, 10};
for (int y = 0; y <= 4; y++)
{
shape.move(0, x[y]);
}
}
}
}
window.clear(sf::Color::Black);
window.draw(shape);
window.display();
}
return 0;
}
【问题讨论】:
-
s = v * t;您缺乏任何时间概念,因此计算机正在尽可能快地移动对象。您必须测量每次迭代所花费的时间,然后将该(增量)时间乘以您的速度。
-
@Paranaix 是正确的。您的计算机正在尽可能快地检查 Up 按键。因此,您需要实现某种帧速率。我对 SFML 中的计时不太熟悉,但根据时钟的精度,您可以执行 if(clock() % 120) == 0) 之类的按键检查。这样一来,您只需每 120 个单位时间进行一次检查(我认为通常是毫秒?)
标签: c++ time clock sfml gravity