SFML Pong, but object oriented

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ////////////////////////////////////////////////////////////
  2. // Headers
  3. ////////////////////////////////////////////////////////////
  4. #include <SFML/Graphics.hpp>
  5. #include <SFML/Audio.hpp>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <cstdlib>
  9. #include "Classes.h"
  10. #ifdef SFML_SYSTEM_IOS
  11. #include <SFML/Main.hpp>
  12. #endif
  13. using namespace std;
  14. ////////////////////////////////////////////////////////////
  15. /// Entry point of application
  16. ///
  17. /// \return Application exit code
  18. ///
  19. ////////////////////////////////////////////////////////////
  20. int main()
  21. {
  22. srand(static_cast<unsigned int>(time(NULL)));
  23. // Define some constants
  24. const float pi = 3.1415;
  25. // Create the window of the application
  26. sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
  27. sf::Style::Titlebar | sf::Style::Close);
  28. window.setVerticalSyncEnabled(true);
  29. // Load the sounds used in the game
  30. // Create the left paddle
  31. Paddle leftPaddle;
  32. // Create the right paddle
  33. Paddle rightPaddle;
  34. // Create the ball
  35. Ball ball;
  36. // Load the text font
  37. sf::Font font;
  38. if (!font.loadFromFile("resources/sansation.ttf"))
  39. return EXIT_FAILURE;
  40. // Initialize the pause message
  41. sf::Text pauseMessage;
  42. pauseMessage.setFont(font);
  43. pauseMessage.setCharacterSize(40);
  44. pauseMessage.setPosition(170, 150);
  45. //pauseMessage.setFillColor(sf::Color::White);
  46. #ifdef SFML_SYSTEM_IOS
  47. pauseMessage.setString("Welcome to SFML pong!\nTouch the screen to start the game");
  48. #else
  49. pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
  50. #endif
  51. // Define the paddles properties
  52. sf::Clock AITimer;
  53. const sf::Time AITime = sf::seconds(0.1f);
  54. const float paddleSpeed = 400.f;
  55. float rightPaddleSpeed = 0.f; // to be changed later
  56. sf::Clock clock;
  57. bool isPlaying = false;
  58. while (window.isOpen())
  59. {
  60. // Handle events
  61. sf::Event event;
  62. while (window.pollEvent(event))
  63. {
  64. // Window closed or escape key pressed: exit
  65. if ((event.type == sf::Event::Closed) ||
  66. ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
  67. {
  68. window.close();
  69. break;
  70. }
  71. // Space key pressed: play
  72. if (((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space)) ||
  73. (event.type == sf::Event::TouchBegan))
  74. {
  75. if (!isPlaying)
  76. {
  77. // (re)start the game
  78. isPlaying = true;
  79. clock.restart();
  80. // Reset the position of the paddles and ball
  81. leftPaddle.setPosition(10 + leftPaddle.getSize().x / 2, gameHeight / 2);
  82. rightPaddle.setPosition(gameWidth - 10 - leftPaddle.getSize().x / 2, gameHeight / 2);
  83. ball.setPosition(gameWidth / 2, gameHeight / 2);
  84. // Reset the ball angle
  85. do
  86. {
  87. // Make sure the ball initial angle is not too much vertical
  88. ball.setAngle((rand() % 360) * 2 * pi / 360);
  89. }
  90. while (abs(cos(ball.getAngle())) < 0.7f);
  91. }
  92. }
  93. // Window size changed, adjust view appropriately
  94. if (event.type == sf::Event::Resized)
  95. {
  96. sf::View view;
  97. view.setSize(gameWidth, gameHeight);
  98. view.setCenter(gameWidth/2.f, gameHeight/2.f);
  99. window.setView(view);
  100. }
  101. }
  102. if (isPlaying)
  103. {
  104. float deltaTime = clock.restart().asSeconds();
  105. // Move the player's paddle
  106. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
  107. (leftPaddle.getY() - leftPaddle.getSize().y / 2 > 5))
  108. {
  109. leftPaddle.move(0.f, -paddleSpeed * deltaTime);
  110. }
  111. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) &&
  112. (leftPaddle.getY() + leftPaddle.getSize().y / 2 < gameHeight - 5))
  113. {
  114. leftPaddle.move(0.f, paddleSpeed * deltaTime);
  115. }
  116. if (sf::Touch::isDown(0))
  117. {
  118. sf::Vector2i pos = sf::Touch::getPosition(0);
  119. sf::Vector2f mappedPos = window.mapPixelToCoords(pos);
  120. leftPaddle.setPosition(leftPaddle.getX(), mappedPos.y);
  121. }
  122. // Move the computer's paddle
  123. if (((rightPaddleSpeed < 0.f) && (rightPaddle.getY() - leftPaddle.getSize().y / 2 > 5.f)) ||
  124. ((rightPaddleSpeed > 0.f) && (rightPaddle.getY() + leftPaddle.getSize().y / 2 < gameHeight - 5.f)))
  125. {
  126. rightPaddle.move(0.f, rightPaddleSpeed * deltaTime);
  127. }
  128. // Update the computer's paddle direction according to the ball position
  129. if (AITimer.getElapsedTime() > AITime)
  130. {
  131. AITimer.restart();
  132. if (ball.getY() + ball.getRadius() > rightPaddle.getY() + leftPaddle.getSize().y / 2)
  133. rightPaddleSpeed = paddleSpeed;
  134. else if (ball.getY() - ball.getRadius() < rightPaddle.getY() - leftPaddle.getSize().y / 2)
  135. rightPaddleSpeed = -paddleSpeed;
  136. else
  137. rightPaddleSpeed = 0.f;
  138. }
  139. // Move the ball
  140. float factor = ball.getSpeed() * deltaTime;
  141. ball.move(cos(ball.getAngle()) * factor, sin(ball.getAngle()) * factor);
  142. #ifdef SFML_SYSTEM_IOS
  143. const string inputString = "Touch the screen to restart";
  144. #else
  145. const string inputString = "Press space to restart or\nescape to exit";
  146. #endif
  147. // Check collisions between the ball and the screen
  148. if (ball.getX() - ball.getRadius() < 0)
  149. {
  150. isPlaying = false;
  151. pauseMessage.setString("You Lost!\n" + inputString);
  152. }
  153. if (ball.getX() + ball.getRadius() > gameWidth)
  154. {
  155. isPlaying = false;
  156. pauseMessage.setString("You Won!\n" + inputString);
  157. }
  158. ball.checkCollisions();
  159. // Check the collisions between the ball and the paddles
  160. // Left Paddle
  161. ball.checkLeftPaddle(leftPaddle);
  162. // Right Paddle
  163. ball.checkRightPaddle(rightPaddle);
  164. }
  165. // Clear the window
  166. window.clear(sf::Color(50, 200, 50));
  167. if (isPlaying)
  168. {
  169. // Draw the paddles and the ball
  170. window.draw(leftPaddle);
  171. window.draw(rightPaddle);
  172. window.draw(ball);
  173. }
  174. else
  175. {
  176. // Draw the pause message
  177. window.draw(pauseMessage);
  178. }
  179. // Display things on screen
  180. window.display();
  181. }
  182. return EXIT_SUCCESS;
  183. }