SFML Pong, but object oriented

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "Classes.h"
  2. #include "Game.h"
  3. #include <cmath>
  4. #include <iostream>
  5. Ball::Ball() {
  6. setRadius(radius);
  7. setOutlineThickness(3);
  8. setOutlineColor(sf::Color::Black);
  9. setFillColor(sf::Color::White);
  10. setOrigin(radius / 2, radius / 2);
  11. }
  12. Ball::Ball(int r) {
  13. setRadius(r);
  14. setOutlineThickness(3);
  15. setOutlineColor(sf::Color::Black);
  16. setFillColor(sf::Color::White);
  17. setOrigin(r / 2, r / 2);
  18. radius = r;
  19. }
  20. float Ball::getX() const {
  21. return getPosition().x;
  22. }
  23. float Ball::getY() const {
  24. return getPosition().y;
  25. }
  26. void Ball::setPosition(float x, float y) {
  27. sf::Transformable::setPosition(x, y);
  28. }
  29. float Ball::getSpeed() const {
  30. return ballSpeed;
  31. }
  32. void Ball::move(float x, float y) {
  33. sf::Transformable::move(x, y);
  34. }
  35. float Ball::getRadius() const {
  36. return radius;
  37. }
  38. float Ball::getAngle() const {
  39. return angle;
  40. }
  41. void Ball::setAngle(float a) {
  42. angle = a;
  43. }
  44. void Ball::checkLeftPaddle(Paddle leftPaddle, sf::Sound ballSound) {
  45. if (getPosition().x - radius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
  46. getPosition().x - radius > leftPaddle.getX() &&
  47. getPosition().y + radius <= leftPaddle.getY() + leftPaddle.getSize().y / 2 &&
  48. getPosition().y - radius >= leftPaddle.getY() - leftPaddle.getSize().y / 2)
  49. {
  50. if (getPosition().y > leftPaddle.getY())
  51. angle = M_PI - angle + (rand() % 20) * M_PI / 180;
  52. else
  53. angle = M_PI - angle - (rand() % 20) * M_PI / 180;
  54. ballSound.play();
  55. setPosition(leftPaddle.getX() + radius + leftPaddle.getSize().x / 2 + 0.1f, getPosition().y);
  56. }
  57. }
  58. void Ball::checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound) {
  59. if (getPosition().x + radius > rightPaddle.getX() - rightPaddle.getSize().x / 2 &&
  60. getPosition().x + radius < rightPaddle.getX() &&
  61. getPosition().y + radius >= rightPaddle.getY() - rightPaddle.getSize().y / 2 &&
  62. getPosition().y - radius <= rightPaddle.getY() + rightPaddle.getSize().y / 2)
  63. {
  64. if (getPosition().y > rightPaddle.getY())
  65. angle = radius - angle + (rand() % 20) * radius / 180;
  66. else
  67. angle = radius - angle - (rand() % 20) * M_PI / 180;
  68. ballSound.play();
  69. setPosition(rightPaddle.getX() - radius - rightPaddle.getSize().x / 2 - 0.1, getPosition().y);
  70. }
  71. }
  72. void Ball::checkCollisions(sf::Sound ballSound) {
  73. if (getY() - radius < 0.f)
  74. {
  75. ballSound.play();
  76. angle = -angle;
  77. setPosition(getX(), radius + 0.1);
  78. }
  79. if (getY() + radius > gameHeight)
  80. {
  81. ballSound.play();
  82. angle = -angle;
  83. setPosition(getX(), gameHeight - radius - 0.1);
  84. }
  85. }
  86. Paddle::Paddle() {
  87. size.x = 25;
  88. size.y = 100;
  89. setSize(sf::Vector2f(25, 100) - sf::Vector2f(3, 3));
  90. setOutlineThickness(3);
  91. setOutlineColor(sf::Color::Black);
  92. setFillColor(sf::Color(200, 200, 200));
  93. setOrigin(sf::Vector2f(25, 100) / 2.f);
  94. }
  95. float Paddle::getY() const {
  96. return getPosition().y;
  97. }
  98. float Paddle::getX() const {
  99. return getPosition().x;
  100. }
  101. void Paddle::setPosition(float x, float y) {
  102. sf::Transformable::setPosition(x, y);
  103. }
  104. sf::Vector2f Paddle::getSize() const {
  105. return size;
  106. }
  107. void Paddle::move(float x, float y) {
  108. sf::Transformable::move(x, y);
  109. }
  110. Game::Game() {
  111. isPlaying = false;
  112. AITime = sf::seconds(0.1);
  113. paddleSpeed = 400;
  114. rightPaddleSpeed = 0;
  115. ballAngle = 0;
  116. }