尝试为简单的球弹跳实验创建 class,我哪里出错了?

Trying to create a class for a simple ball bounce experiment, where am I going wrong?

我对 c++ 编程还很陌生,我正在尝试使用 SFML 创建一个简单的程序,在该程序中可以创建会弹跳的球。然而,我什至在进入物理模拟之前就被卡住了。我似乎无法让我的球 class 工作。我想用它来存储创建的每个球的变量以及更新和绘制函数。但每次我都会遇到某种错误,而我却找不到任何关于看似如此简单的事情的帮助。

#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();
    }
}

任何帮助或批评将不胜感激,在此先感谢!

试试这个片段:

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();
    }
}