试着为一个简单的球反弹实验创建一个类,我错在哪里? 我对C++编程非常陌生,我正在尝试用SFML创建一个简单的程序,在这个程序中,可以创建一个可以弹跳的球。然而,在我进入物理模拟之前,我就被卡住了。我似乎无法让我的球类课程正常运转。我想用它来存储创建的每个球的变量,以及更新和绘制函数。但每次我最终都会犯一些错误,我在一些显然很简单的事情上找不到任何帮助 #include <iostream> #include <SFML\Audio.hpp> #include <SFML\Graphics.hpp> #include <SFML\Network.hpp> #include <SFML\System.hpp> #include <SFML\Window.hpp> class Ball { public: // vector for position sf::Vector2 pos(100,100); // vector for velocity sf::Vector2 vel(0,0); void update() { // factors influence velocity // update position based on velocity pos.x += vel.x; pos.y += vel.y; } void draw() { // draw ball to the window using position vector sf::circleShape circle(10); circle.setPosition(pos.x,pos.y); circle.setFillColor(sf::Color::White); window.draw(circle); } }; int main() { /*create window settings*/ sf::ContextSettings settings; settings.antialiasingLevel = 8; // set the antialiasing level /*create window*/ sf::RenderWindow window; window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings); /*create ball(s)*/ Ball ball01; /*Main loop*/ while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { // "close requested" event: close the window if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::White); // call ball.update(); and ball.draw(); ball01.update(); ball01.draw(); window.display(); } } #包括 #包括 #包括 #包括 #包括 #包括 班级舞会 { 公众: //位置向量 sf::矢量2位置(100100); //速度矢量 向量2级(0,0); 无效更新() { //影响速度的因素 //基于速度更新位置 位置x+=水平x; 位置y+=水平y; } 作废提款() { //使用位置向量将球绘制到窗口 sf::圆形(10); 圆圈设定位置(位置x、位置y); circle.setFillColor(sf::Color::White); 窗。画(圆); } }; int main() { /*创建窗口设置*/ sf::上下文设置; settings.antialasinglevel=8;//设置抗锯齿级别 /*创建窗口*/ sf::RenderWindow窗口; 创建(sf::VideoMode(800600),“简单物理”,sf::Style::Default,设置); /*创建球(s)*/ Ball-ball01; /*主回路*/ while(window.isOpen()) { sf::事件; while(window.pollEvent(事件)) { //“请求关闭”事件:关闭窗口 如果(event.type==sf::event::Closed) window.close(); } 窗口。清除(sf::颜色::白色); //调用ball.update();和ball.draw(); ball01.update(); ball01.draw(); window.display(); } }

试着为一个简单的球反弹实验创建一个类,我错在哪里? 我对C++编程非常陌生,我正在尝试用SFML创建一个简单的程序,在这个程序中,可以创建一个可以弹跳的球。然而,在我进入物理模拟之前,我就被卡住了。我似乎无法让我的球类课程正常运转。我想用它来存储创建的每个球的变量,以及更新和绘制函数。但每次我最终都会犯一些错误,我在一些显然很简单的事情上找不到任何帮助 #include <iostream> #include <SFML\Audio.hpp> #include <SFML\Graphics.hpp> #include <SFML\Network.hpp> #include <SFML\System.hpp> #include <SFML\Window.hpp> class Ball { public: // vector for position sf::Vector2 pos(100,100); // vector for velocity sf::Vector2 vel(0,0); void update() { // factors influence velocity // update position based on velocity pos.x += vel.x; pos.y += vel.y; } void draw() { // draw ball to the window using position vector sf::circleShape circle(10); circle.setPosition(pos.x,pos.y); circle.setFillColor(sf::Color::White); window.draw(circle); } }; int main() { /*create window settings*/ sf::ContextSettings settings; settings.antialiasingLevel = 8; // set the antialiasing level /*create window*/ sf::RenderWindow window; window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings); /*create ball(s)*/ Ball ball01; /*Main loop*/ while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { // "close requested" event: close the window if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::White); // call ball.update(); and ball.draw(); ball01.update(); ball01.draw(); window.display(); } } #包括 #包括 #包括 #包括 #包括 #包括 班级舞会 { 公众: //位置向量 sf::矢量2位置(100100); //速度矢量 向量2级(0,0); 无效更新() { //影响速度的因素 //基于速度更新位置 位置x+=水平x; 位置y+=水平y; } 作废提款() { //使用位置向量将球绘制到窗口 sf::圆形(10); 圆圈设定位置(位置x、位置y); circle.setFillColor(sf::Color::White); 窗。画(圆); } }; int main() { /*创建窗口设置*/ sf::上下文设置; settings.antialasinglevel=8;//设置抗锯齿级别 /*创建窗口*/ sf::RenderWindow窗口; 创建(sf::VideoMode(800600),“简单物理”,sf::Style::Default,设置); /*创建球(s)*/ Ball-ball01; /*主回路*/ while(window.isOpen()) { sf::事件; while(window.pollEvent(事件)) { //“请求关闭”事件:关闭窗口 如果(event.type==sf::event::Closed) window.close(); } 窗口。清除(sf::颜色::白色); //调用ball.update();和ball.draw(); ball01.update(); ball01.draw(); window.display(); } },c++,class,sfml,C++,Class,Sfml,任何帮助或批评都将不胜感激,提前感谢 试试这个片段: 1) 您的背景色应该与球的颜色不同 2) 使用window.setFramerateLimit()减慢动画速度 3) 使用边界条件,使球不会消失在视野之外 4) Draw()也需要更正 class Ball { public: // vector for position sf::Vector2f pos{ 100, 100 }; // vector for velocity sf::Vector2f ve

任何帮助或批评都将不胜感激,提前感谢

试试这个片段:

1) 您的背景色应该与球的颜色不同

2) 使用window.setFramerateLimit()减慢动画速度

3) 使用边界条件,使球不会消失在视野之外

4) Draw()也需要更正

class Ball
{
public:

    // vector for position
    sf::Vector2f pos{ 100, 100 };

    // vector for velocity
    sf::Vector2f vel{ 1, 1 };

    void update()
    {
        // factors influence velocity
        // update position based on velocity
        pos.x += vel.x;
        pos.y += vel.y;

        if (pos.x > 800 || pos.x < 0) vel.x = -vel.x; //boundary cond
        if (pos.y > 600 || pos.y < 0) vel.y = -vel.y; //boundary cond
    }

    void draw(sf::RenderWindow& window)
    {
        // draw ball to the window using position vector 
        sf::CircleShape circle(10);
        circle.setPosition(pos.x, pos.y);
        circle.setFillColor(sf::Color::White);

        window.draw(circle);
    }
};

int main()
{
    /*create window settings*/
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8; // set the antialiasing level

                                    /*create window*/
    sf::RenderWindow window;
    window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings);

    /*create ball(s)*/
    Ball ball01;

    window.setFramerateLimit(60); //slow down speed
    /*Main loop*/
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::Black); // ball is white so make backgnd black

        // call ball.update(); and ball.draw();
        ball01.update();
        ball01.draw(window);
        window.display();
    }
}
班级舞会
{
公众:
//位置向量
sf::Vector2f pos{100100};
//速度矢量
向量2f层{1,1};
无效更新()
{
//影响速度的因素
//基于速度更新位置
位置x+=水平x;
位置y+=水平y;
如果(pos.x>800 | | pos.x<0)vel.x=-vel.x;//边界条件
如果(pos.y>600 | | pos.y<0)vel.y=-vel.y;//边界条件
}
空心绘制(sf::渲染窗口和窗口)
{
//使用位置向量将球绘制到窗口
sf::圆形(10);
圆圈设定位置(位置x、位置y);
circle.setFillColor(sf::Color::White);
窗。画(圆);
}
};
int main()
{
/*创建窗口设置*/
sf::上下文设置;
settings.antialasinglevel=8;//设置抗锯齿级别
/*创建窗口*/
sf::RenderWindow窗口;
创建(sf::VideoMode(800600),“简单物理”,sf::Style::Default,设置);
/*创建球(s)*/
Ball-ball01;
window.setFramerateLimit(60);//减慢速度
/*主回路*/
while(window.isOpen())
{
sf::事件;
while(window.pollEvent(事件))
{
//“请求关闭”事件:关闭窗口
如果(event.type==sf::event::Closed)
window.close();
}
window.clear(sf::Color::Black);//球是白色的,所以将背景设为黑色
//调用ball.update();和ball.draw();
ball01.update();
ball01.绘制(窗口);
window.display();
}
}

我鼓励您非常具体;您希望Ball做什么?您试图实现什么?
Ball
应该如何更新和渲染,以及您想出了什么?但是为了让球滚动,可以说,您可以查看sfml的
sf::Vector2f
来存储位置和速度,并注意
Ball::draw()
需要访问
窗口才能进行任何绘图。你也可以考虑使用<代码> STD::Vector 来存储许多球,每个球都需要更新和绘制。嗨,我刚刚更新了我尝试过的一件事,我知道大部分都可能是错的,但我无法从收到的错误中得出结论:/我该如何授予draw函数对窗口的访问权限?您正在引用
main
window
对象,它肯定不在
Ball::draw()的范围内。考虑将函数的签名更改为<代码>球::绘制(SF:ReNeWindow和Window);
然后从
main
传入
window
,我知道,是
voiddraw(sf::RenderWindow&window)
而不是
voiddraw()
?顺便说一下,非常感谢你的帮助!