#include "Classes.h" #include "Game.h" #include #include Ball::Ball() { setRadius(radius); setOutlineThickness(3); setOutlineColor(sf::Color::Black); setFillColor(sf::Color::White); setOrigin(radius / 2, radius / 2); } Ball::Ball(int r) { setRadius(r); setOutlineThickness(3); setOutlineColor(sf::Color::Black); setFillColor(sf::Color::White); setOrigin(r / 2, r / 2); radius = r; } float Ball::getX() const { return getPosition().x; } float Ball::getY() const { return getPosition().y; } void Ball::setPosition(float x, float y) { sf::Transformable::setPosition(x, y); } float Ball::getSpeed() const { return ballSpeed; } void Ball::move(float x, float y) { sf::Transformable::move(x, y); } float Ball::getRadius() const { return radius; } float Ball::getAngle() const { return angle; } void Ball::setAngle(float a) { angle = a; } void Ball::checkLeftPaddle(Paddle leftPaddle, sf::Sound ballSound) { if (getPosition().x - radius < leftPaddle.getX() + leftPaddle.getSize().x / 2 && getPosition().x - radius > leftPaddle.getX() && getPosition().y + radius <= leftPaddle.getY() + leftPaddle.getSize().y / 2 && getPosition().y - radius >= leftPaddle.getY() - leftPaddle.getSize().y / 2) { if (getPosition().y > leftPaddle.getY()) angle = M_PI - angle + (rand() % 20) * M_PI / 180; else angle = M_PI - angle - (rand() % 20) * M_PI / 180; ballSound.play(); setPosition(leftPaddle.getX() + radius + leftPaddle.getSize().x / 2 + 0.1f, getPosition().y); } } void Ball::checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound) { if (getPosition().x + radius > rightPaddle.getX() - rightPaddle.getSize().x / 2 && getPosition().x + radius < rightPaddle.getX() && getPosition().y + radius >= rightPaddle.getY() - rightPaddle.getSize().y / 2 && getPosition().y - radius <= rightPaddle.getY() + rightPaddle.getSize().y / 2) { if (getPosition().y > rightPaddle.getY()) angle = radius - angle + (rand() % 20) * radius / 180; else angle = radius - angle - (rand() % 20) * M_PI / 180; ballSound.play(); setPosition(rightPaddle.getX() - radius - rightPaddle.getSize().x / 2 - 0.1, getPosition().y); } } Paddle::Paddle() { size.x = 25; size.y = 100; setSize(sf::Vector2f(25, 100) - sf::Vector2f(3, 3)); setOutlineThickness(3); setOutlineColor(sf::Color::Black); setFillColor(sf::Color(200, 200, 200)); setOrigin(sf::Vector2f(25, 100) / 2.f); } float Paddle::getY() const { return getPosition().y; } float Paddle::getX() const { return getPosition().x; } void Paddle::setPosition(float x, float y) { sf::Transformable::setPosition(x, y); } sf::Vector2f Paddle::getSize() const { return size; } void Paddle::move(float x, float y) { sf::Transformable::move(x, y); } Game::Game() { isPlaying = false; AITime = sf::seconds(0.1); paddleSpeed = 400; rightPaddleSpeed = 0; ballAngle = 0; }