我正在尝试从 SFML 中的 Transformable 和 Drawable 继承,以使我的对象......好吧,可转换和可绘制。我正在制作一个简单的突破游戏,但也许我的做法是错误的。这是我的代码:
#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;
}
现在移动和绘制工作(几乎)符合预期,但是碰撞有点不稳定。首先是我的碰撞问题:
- 我需要像以前那样实现 getGlobalBounds 函数吗?或者是否有更好的方法来处理 Transformable 和 Drawable 中包含的内容?
- 我应该直接对形状执行转换,还是应该像现在一样将转换传递给绘图函数?
绘图也发生了一些奇怪的事情,这可能是一个快速修复。现在 getPosition 方法为我的球对象返回了不正确的值。它返回的区域似乎向下和向右移动了一点。有什么可能的原因吗?
感谢您提供的任何帮助!
编辑:也欢迎任何通用的 C++ 提示,我仍然是初学者。
最佳答案
如果我是你,我会像这样定义一个名为 TransformableAndDrawable
的新类:
class TransformableAndDrawable : public sf::Transformable, public sf::Drawable {
// Your code here
}
在这个类中,您应该定义您的可转换类和可绘制类通常需要的所有成员。此外,在此类中,您应该定义所有通常可以在可转换和可绘制类中实现的方法。然后,您的类应该继承自 TransformableAndDrawable
,如下所示:
class Player : TransformableAndDrawable {
// Your code here
}
现在,第一个问题的答案是:如果给定方法是通用方法,我将在 TransformableAndDrawable
类中实现它,所以所有类都继承自 TransformableAndDrawable
会有这个方法。
不要给这些成员起不同的名字,比如 p_rect
和 p_circle
,而是用相同的名字来命名这些成员,比如 p_shape
,这样你就会有没有命名问题。此外,我相信您可以将 p_shape 声明为祖先类或接口(interface)(我不知道您正在使用的库中定义了哪些类)并且仅在需要时指定形状的性质(无论是圆形或矩形或其他东西)。
关于第二个问题:我喜欢你实现的方式,但是你犯了两个错误:
- 它不可扩展:我们想要一个通用的解决方案,一个可用于您现在和将来使用的任何形状的类,不是吗?
- 不够通用:当我想知道形状的全局边界时,我对形状的性质不感兴趣,我更希望你的代码在我不知道的情况下处理形状的性质
简而言之,您应该执行以下操作:
创建一个包装器类,它将继承自
Transformable
和Drawable
在你的包装器类中,对形状的性质是不可知的,尽可能通用,希望有一些类或接口(interface)是
RectangleShape
和的祖先圆形
。从包装类继承所有可绘制和可转换类,因此您将在类之间共享功能
如果您的包装类中的某些内容不适用于从它继承的类,请覆盖该类中的方法。
编辑:
我更详细地查看了您正在使用的库,发现有一个名为 Shape 的类,它是 CircleShape
和 RectangleShape
的祖先。因此,使用 Shape
而不是这些类,您的代码将更加通用和可重用。
关于c++ - 继承 SFML 中的 Transformable 和 Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17910297/