Back to index

libsfml  1.6+dfsg2
Pong.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Graphics.hpp>
00006 #include <SFML/Audio.hpp>
00007 #include <cmath>
00008 
00009 
00016 int main()
00017 {
00018        // Defines PI
00019        const float PI = 3.14159f;
00020 
00021     // Create the window of the application
00022     sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Pong");
00023 
00024     // Load the sounds used in the game
00025     sf::SoundBuffer BallSoundBuffer;
00026     if (!BallSoundBuffer.LoadFromFile("datas/pong/ball.wav"))
00027     {
00028         return EXIT_FAILURE;
00029     }
00030     sf::Sound BallSound(BallSoundBuffer);
00031 
00032     // Load the images used in the game
00033     sf::Image BackgroundImage, LeftPaddleImage, RightPaddleImage, BallImage;
00034     if (!BackgroundImage.LoadFromFile("datas/pong/background.jpg")    ||
00035         !LeftPaddleImage.LoadFromFile("datas/pong/paddle_left.png")   ||
00036         !RightPaddleImage.LoadFromFile("datas/pong/paddle_right.png") ||
00037         !BallImage.LoadFromFile("datas/pong/ball.png"))
00038     {
00039         return EXIT_FAILURE;
00040     }
00041 
00042     // Load the text font
00043     sf::Font Cheeseburger;
00044     if (!Cheeseburger.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
00045         return EXIT_FAILURE;
00046 
00047        // Initialize the end text
00048        sf::String End;
00049     End.SetFont(Cheeseburger);
00050        End.SetSize(60.f);
00051     End.Move(150.f, 200.f);
00052     End.SetColor(sf::Color(50, 50, 250));
00053 
00054     // Create the sprites of the background, the paddles and the ball
00055     sf::Sprite Background(BackgroundImage);
00056     sf::Sprite LeftPaddle(LeftPaddleImage);
00057     sf::Sprite RightPaddle(RightPaddleImage);
00058     sf::Sprite Ball(BallImage);
00059 
00060     LeftPaddle.Move(10, (App.GetView().GetRect().GetHeight() - LeftPaddle.GetSize().y) / 2);
00061     RightPaddle.Move(App.GetView().GetRect().GetWidth() - RightPaddle.GetSize().x - 10, (App.GetView().GetRect().GetHeight() - RightPaddle.GetSize().y) / 2);
00062     Ball.Move((App.GetView().GetRect().GetWidth() - Ball.GetSize().x) / 2, (App.GetView().GetRect().GetHeight() - Ball.GetSize().y) / 2);
00063 
00064     // Define the paddles properties
00065     sf::Clock AITimer;
00066        const float AITime     = 0.1f;
00067     float LeftPaddleSpeed  = 400.f;
00068     float RightPaddleSpeed = 400.f;
00069 
00070     // Define the ball properties
00071     float BallSpeed = 400.f;
00072     float BallAngle;
00073     do
00074     {
00075         // Make sure the ball initial angle is not too much vertical
00076         BallAngle = sf::Randomizer::Random(0.f, 2 * PI);
00077     } while (std::abs(std::cos(BallAngle)) < 0.7f);
00078 
00079        bool IsPlaying = true;
00080     while (App.IsOpened())
00081     {
00082         // Handle events
00083         sf::Event Event;
00084         while (App.GetEvent(Event))
00085         {
00086             // Window closed or escape key pressed : exit
00087             if ((Event.Type == sf::Event::Closed) || 
00088                ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)))
00089             {
00090                 App.Close();
00091                 break;
00092             }
00093         }
00094 
00095         if (IsPlaying)
00096               {
00097                      // Move the player's paddle
00098             if (App.GetInput().IsKeyDown(sf::Key::Up) && (LeftPaddle.GetPosition().y > 5.f))
00099                             LeftPaddle.Move(0.f, -LeftPaddleSpeed * App.GetFrameTime());
00100             if (App.GetInput().IsKeyDown(sf::Key::Down) && (LeftPaddle.GetPosition().y < App.GetView().GetRect().GetHeight() - LeftPaddle.GetSize().y - 5.f))
00101                             LeftPaddle.Move(0.f, LeftPaddleSpeed * App.GetFrameTime());
00102 
00103                      // Move the computer's paddle
00104             if (((RightPaddleSpeed < 0.f) && (RightPaddle.GetPosition().y > 5.f)) ||
00105                 ((RightPaddleSpeed > 0.f) && (RightPaddle.GetPosition().y < App.GetView().GetRect().GetHeight() - RightPaddle.GetSize().y - 5.f)))
00106             {
00107                 RightPaddle.Move(0.f, RightPaddleSpeed * App.GetFrameTime());
00108             }
00109 
00110             // Update the computer's paddle direction according to the ball position
00111             if (AITimer.GetElapsedTime() > AITime)
00112             {
00113                 AITimer.Reset();
00114                 if ((RightPaddleSpeed < 0) && (Ball.GetPosition().y + Ball.GetSize().y > RightPaddle.GetPosition().y + RightPaddle.GetSize().y))
00115                     RightPaddleSpeed = -RightPaddleSpeed;
00116                 if ((RightPaddleSpeed > 0) && (Ball.GetPosition().y < RightPaddle.GetPosition().y))
00117                     RightPaddleSpeed = -RightPaddleSpeed;
00118             }
00119 
00120                      // Move the ball
00121                      float Factor = BallSpeed * App.GetFrameTime();
00122             Ball.Move(std::cos(BallAngle) * Factor, std::sin(BallAngle) * Factor);
00123 
00124                      // Check collisions between the ball and the screen
00125                      if (Ball.GetPosition().x < 0.f)
00126                      {
00127                             IsPlaying = false;
00128                             End.SetText("You lost !\n(press escape to exit)");
00129                      }
00130                      if (Ball.GetPosition().x + Ball.GetSize().x > App.GetView().GetRect().GetWidth())
00131                      {
00132                             IsPlaying = false;
00133                             End.SetText("You won !\n(press escape to exit)");
00134                      }
00135                      if (Ball.GetPosition().y < 0.f)
00136                      {
00137                             BallSound.Play();
00138                             BallAngle = -BallAngle;
00139                             Ball.SetY(0.1f);
00140                      }
00141                      if (Ball.GetPosition().y + Ball.GetSize().y > App.GetView().GetRect().GetHeight())
00142                      {
00143                             BallSound.Play();
00144                             BallAngle = -BallAngle;
00145                             Ball.SetY(App.GetView().GetRect().GetHeight() - Ball.GetSize().y - 0.1f);
00146                      }
00147 
00148                      // Check the collisions between the ball and the paddles
00149                      // Left Paddle
00150                      if (Ball.GetPosition().x < LeftPaddle.GetPosition().x + LeftPaddle.GetSize().x && 
00151                             Ball.GetPosition().x > LeftPaddle.GetPosition().x + (LeftPaddle.GetSize().x / 2.0f) &&
00152                             Ball.GetPosition().y + Ball.GetSize().y >= LeftPaddle.GetPosition().y &&
00153                             Ball.GetPosition().y <= LeftPaddle.GetPosition().y + LeftPaddle.GetSize().y)
00154                      {
00155                             BallSound.Play();
00156                             BallAngle = PI - BallAngle;
00157                             Ball.SetX(LeftPaddle.GetPosition().x + LeftPaddle.GetSize().x + 0.1f);
00158                      }
00159 
00160                      // Right Paddle
00161                      if (Ball.GetPosition().x + Ball.GetSize().x > RightPaddle.GetPosition().x &&
00162                             Ball.GetPosition().x + Ball.GetSize().x < RightPaddle.GetPosition().x + (RightPaddle.GetSize().x / 2.0f) &&
00163                             Ball.GetPosition().y + Ball.GetSize().y >= RightPaddle.GetPosition().y &&
00164                             Ball.GetPosition().y <= RightPaddle.GetPosition().y + RightPaddle.GetSize().y)
00165                      {
00166                             BallSound.Play();
00167                             BallAngle = PI - BallAngle;
00168                             Ball.SetX(RightPaddle.GetPosition().x - Ball.GetSize().x - 0.1f);
00169                      }
00170               }
00171 
00172         // Clear the window
00173         App.Clear();
00174 
00175         // Draw the background, paddles and ball sprites
00176         App.Draw(Background);
00177         App.Draw(LeftPaddle);
00178         App.Draw(RightPaddle);
00179         App.Draw(Ball);
00180 
00181         // If the game is over, display the end message
00182         if (!IsPlaying)
00183             App.Draw(End);
00184 
00185         // Display things on screen
00186         App.Display();
00187     }
00188 
00189     return EXIT_SUCCESS;
00190 }