Cześć!
Od pewnego czasu uczę się biblioteki SFML. W ramach nauki piszę grę typu Arkanoid.
Zrobiłem już piłkę, paletkę, lecz nie wiem jak zrobić kolizję między nimi. Oto mój kod:
using namespace sf;
int main()
{
Map map;
map.screen();
return 0;
}
class Map
{
public:
void Map::screen()
{
Ball::setBall();
Paddle::setPaddle();
Map collision;
RenderWindow window{ VideoMode(1280, 720), "Pong" };
window.setFramerateLimit(60);
Event event1;
for (;;)
{
checkCollision::collisionRight();
checkCollision::collisionTop();
checkCollision::collisionLeft();
checkCollision::collisionBottom();
ifCollision();
window.clear(Color::Yellow);
window.pollEvent(event1);
if (event1.type == Event::Closed)
{
window.close();
break;
}
Ball::show();
Paddle::update();
window.draw(Ball::circle);
window.draw(Paddle::rocket);
window.display();
}
}
};
class Paddle
{
public:
RectangleShape rocket;
const float width{ 200.0f };
const float height{ 22.0f };
float a;
float b;
Vector2f paddleSpeed{ a, b };
void set_size()
{
rocket.setPosition(650, 640);
rocket.setSize(Vector2f(this->width, this->height));
rocket.setFillColor(Color::Cyan);
rocket.setOrigin(width / 2.f, height / 2.f);
}
void draw(RenderTarget& target, RenderStates state)const
{
target.draw(this->rocket, state);
}
void update()
{
paddleX = 640;
paddleY = 650;
this->rocket.move(this->paddleSpeed);
if (Keyboard::isKeyPressed(Keyboard::Key::Left) && this->Left() > 0)
this-> paddleSpeed.x = -12.0;
else if (Keyboard::isKeyPressed(Keyboard::Key::Right) && this->Right() < 1280)
this-> paddleSpeed.x = 12.0;
else
this-> paddleSpeed.x = 0;
}
float Left()
{
return this->rocket.getPosition().x - rocket.getSize().x/2.f;
}
float Right()
{
return this->rocket.getPosition().x + rocket.getSize().x/2.f;
}
float Top()
{
return this->rocket.getPosition().y - rocket.getSize().y / 2.f;
}
float Bottom()
{
return this->rocket.getPosition().y + rocket.getSize().y / 2.f;
}
};
class Ball
{
public:
float ballX;
float ballY;
CircleShape circle;
const float r{ 20.0f };
const float ballSpeed{ 3.3f };
Vector2f speed{ ballSpeed, ballSpeed };
void Ball::setBall()
{
ballX = 640.0;
ballY = 360.0;
circle.setPosition(ballX, ballY);
circle.setRadius(r);
circle.setFillColor(Color::Red);
circle.setOrigin(this->r, this->r);
}
void Ball::draw(RenderTarget& target, RenderStates state)const
{
target.draw(this->circle, state);
}
void show()
{
circle.move(speed);
if (this->Left() < 0)
speed.x = 20;
else if (this->Right() > 1280)
{
speed.x = -20;
}
if (this->Top() < 0)
{
speed.y = 20;
}
else if (this->Bottom() > 720)
{
speed.y = -20;
}
}
float Left()
{
return this->circle.getPosition().x - circle.getRadius();
}
float Right()
{
return this->circle.getPosition().x + circle.getRadius();
}
float Top()
{
return this->circle.getPosition().y - circle.getRadius();
}
float Bottom()
{
return this->circle.getPosition().y + circle.getRadius();
}
};
Jak to zrobić?
To mój pierwszy projekt w SFML, więc mogą być błędy.
Pozdrawiam.