gpt4 book ai didi

c++ - 在 C++ 的 SFML 中居中​​形状/对象

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:00 25 4
gpt4 key购买 nike

所以最近,我开始使用 SFML 在 Visual Studio 中制作游戏。设置完所有内容并编写了一些示例代码后,我设计了这个:

#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "Move the Shape");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}

window.clear();
window.draw(shape);
window.display();
}

return 0;
}

程序产生以下结果: Move the Shape UNCENTERED

如何将圆放在中心?我想设置一些代码,让用户可以使用键盘的箭头键移动圆圈,所以我需要圆圈在中心。

最佳答案

你需要用shape.setPosition(x, y)设置shape的位置.你知道窗口的宽度和高度(每个方向 600px),你知道圆的半径(100px),所以你可以计算出圆需要移动到中心的 x 和 y。我会把它留给你作为练习。

您可能还想考虑设置圆的原点,以便您可以通过圆心定位圆(参见 setOrigin)。

关于c++ - 在 C++ 的 SFML 中居中​​形状/对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com