7

I'm trying to inherit from Transformable and Drawable in SFML in order to make my objects... well, transformable and drawable. I'm making a simple breakout game, but perhaps I'm going about this the wrong way. Here's my code:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class Player : public sf::Transformable, public sf::Drawable {
    public:
        Player(int x, int y);
        ~Player() {};

        sf::RectangleShape p_rect;

        void doMovement(const sf::RenderWindow& window);
        sf::FloatRect getGlobalBounds() const;
    private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
            states.transform *= getTransform();
            target.draw(p_rect, states);
        }

};

class Ball : public sf::Transformable, public sf::Drawable {
    public:
        Ball(int r, int x, int y);
        ~Ball() {};

        sf::CircleShape b_circle;

        void doXMovement();
        void doYMovement();
        bool doXCollisions(const Player& player);
        bool doYCollisions(const Player& player);
        sf::FloatRect getGlobalBounds() const;
    private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
            states.transform *= getTransform();
            target.draw(b_circle, states);
        }

        bool right;
        bool up;
};

Player::Player(int x, int y) {
    p_rect = sf::RectangleShape(sf::Vector2f(x, y));
}

void Player::doMovement(const sf::RenderWindow& window) {
    setPosition(sf::Mouse::getPosition(window).x, 500);
    if (getPosition().x < 0)
        setPosition(0, 500);
    else if (getPosition().x > 720)
        setPosition(720, 500);
}

sf::FloatRect Player::getGlobalBounds() const {
    return getTransform().transformRect(p_rect.getGlobalBounds());
}

Ball::Ball(int r, int x, int y) {
    b_circle = sf::CircleShape(r);
    b_circle.setPosition(x, y);
    right = true;
    up = false;
}

void Ball::doXMovement() {
    if (right)
        move(1, 0);
    else
        move(-1, 0);
}

void Ball::doYMovement() {
    if (up)
        move(0, -1);
    else
        move(0, 1);
}

bool Ball::doXCollisions(const Player& player) {
    bool coll;
    if (getGlobalBounds().intersects(player.getGlobalBounds())) {
        right = !right;
        coll = true;
    } else
        coll = false;

    if (getPosition().x >= 800 - b_circle.getRadius())
        right = false;
    else if (getPosition().x <= 0)
        right = true;
    return coll;
}

bool Ball::doYCollisions(const Player& player) {
    bool coll;
    if (getGlobalBounds().intersects(player.getGlobalBounds())) {
        up = !up;
        coll = true;
    } else
        coll = false;
    if (getPosition().x <= 0)
        up = false;
    return coll;
}

sf::FloatRect Ball::getGlobalBounds() const {
    return getTransform().transformRect(b_circle.getGlobalBounds());
}

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Breakout");
    window.setMouseCursorVisible(false);
    Player player(80, 10);
    Ball ball(3, 100, 100);
    sf::Clock clock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        player.doMovement(window);
        if (clock.getElapsedTime().asMilliseconds() >= 3) {
            clock.restart();
            if (!ball.doYCollisions(player))
                ball.doXCollisions(player);
            ball.doYMovement();
            ball.doXMovement();
        }
        window.clear(sf::Color::Black);
        window.draw(player);
        window.draw(ball);
        window.display();
    }
    return 0;
}

Now moving and drawing work (nearly) as expected, however collisions are a bit wonky. First my collisions problems:

  1. Do I need to implement the getGlobalBounds function the way I did? Or is there a better way to do it with things included in Transformable and Drawable?
  2. Should I be performing transformations on the shapes directly, or should I pass the transformations to the draw function like I currently am?

Something strange is also happening with the drawing which is probably a quick fix. Right now the getPosition method returns incorrect values for my ball object. The area it returns seems to be shifted down and to the right a bit. Any reason that might be?

Thanks for any help you are able to give!

EDIT: Also any general C++ tips are welcome, I'm still a beginner.

4

1 回答 1

2

如果我是你,我会定义一个新类,TransformableAndDrawable如下所示:

class TransformableAndDrawable : public sf::Transformable, public sf::Drawable {
    // Your code here
}

在这个类中,您应该定义可转换和可绘制类通常需要的所有成员。此外,在此类中,您应该定义通常可以在可转换和可绘制类中实现的所有方法。然后,您的类应该继承自TransformableAndDrawable,如下所示:

class Player : TransformableAndDrawable {
    // Your code here
}

现在,第一个问题的答案是:TransformableAndDrawable如果给定的方法是通用方法,我会在类中实现它,所以所有继承的类TransformableAndDrawable都会有这个方法。

不要给这些成员命名,比如p_rectand p_circle,而是用相同的名字命名这些成员,比如p_shape,这样你就不会有命名问题。另外,我相信您可以将 p_shape 声明为祖先类或接口(我不知道您正在使用的库中定义了哪些类)并且仅在需要时指定形状的性质(无论它是圆形或矩形或其他东西)。

至于第二个问题:我喜欢你实现事物的方式,但你犯了两个错误:

  • 它不可扩展:我们想要一个通用的解决方案,一个可用于您现在和将来使用的任何形状的类,不是吗?
  • 它不够通用:当我想知道形状的全局边界时,我对形状的性质不感兴趣,我希望你的代码在我不知道的情况下处理形状的性质

简而言之,您应该执行以下操作:

  1. 创建一个包装类,它将继承自TransformableDrawable

  2. 在你的包装类中,对形状的性质不可知,尽可能通用,希望有一些类或接口是RectangleShape和的祖先CircleShape

  3. 从包装类继承所有可绘制和可转换类,因此您将在类之间拥有共享功能

  4. 如果包装类中的某些内容不适合从它继承的类,请覆盖该类中的方法。

编辑:

我更详细地查看了您正在使用的库,发现有一个名为 Shape 的类,它是 和 的CircleShape祖先RectangleShape。因此,而不是使用这些类Shape,您的代码将更加通用和可重用。

于 2013-07-28T17:17:23.397 回答