Brak opisu

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