Fixed syntax error in the Pong sample

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1184 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-07-21 21:35:37 +00:00
parent aa3769b3c6
commit 7a54cb78a3

View File

@ -15,8 +15,8 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
int main() int main()
{ {
// Defines PI // Defines PI
const float PI = 3.14159f; const float PI = 3.14159f;
// Create the window of the application // Create the window of the application
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML Pong"); sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML Pong");
@ -41,13 +41,13 @@ int main()
// Load the text font // Load the text font
sf::Font font; sf::Font font;
if (!ballImage.LoadFromFile("datas/post-fx/cheeseburger.ttf")) if (!font.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
return EXIT_FAILURE; return EXIT_FAILURE;
// Initialize the end text // Initialize the end text
sf::String end; sf::String end;
end.SetFont(font); end.SetFont(font);
end.SetSize(60.f); end.SetSize(60.f);
end.Move(150.f, 200.f); end.Move(150.f, 200.f);
end.SetColor(sf::Color(50, 50, 250)); end.SetColor(sf::Color(50, 50, 250));
@ -63,7 +63,7 @@ int main()
// Define the paddles properties // Define the paddles properties
sf::Clock AITimer; sf::Clock AITimer;
const float AITime = 0.1f; const float AITime = 0.1f;
float leftPaddleSpeed = 400.f; float leftPaddleSpeed = 400.f;
float rightPaddleSpeed = 400.f; float rightPaddleSpeed = 400.f;
@ -76,7 +76,7 @@ int main()
ballAngle = sf::Randomizer::Random(0.f, 2 * PI); ballAngle = sf::Randomizer::Random(0.f, 2 * PI);
} while (std::abs(std::cos(ballAngle)) < 0.7f); } while (std::abs(std::cos(ballAngle)) < 0.7f);
bool isPlaying = true; bool isPlaying = true;
while (window.IsOpened()) while (window.IsOpened())
{ {
// Handle events // Handle events
@ -93,14 +93,14 @@ int main()
} }
if (isPlaying) if (isPlaying)
{ {
// Move the player's paddle // Move the player's paddle
if (window.GetInput().IsKeyDown(sf::Key::Up) && (leftPaddle.GetPosition().y > 5.f)) if (window.GetInput().IsKeyDown(sf::Key::Up) && (leftPaddle.GetPosition().y > 5.f))
leftPaddle.Move(0.f, -leftPaddleSpeed * window.GetFrameTime()); leftPaddle.Move(0.f, -leftPaddleSpeed * window.GetFrameTime());
if (window.GetInput().IsKeyDown(sf::Key::Down) && (leftPaddle.GetPosition().y < window.GetView().GetSize().y - leftPaddle.GetSize().y - 5.f)) if (window.GetInput().IsKeyDown(sf::Key::Down) && (leftPaddle.GetPosition().y < window.GetView().GetSize().y - leftPaddle.GetSize().y - 5.f))
leftPaddle.Move(0.f, leftPaddleSpeed * window.GetFrameTime()); leftPaddle.Move(0.f, leftPaddleSpeed * window.GetFrameTime());
// Move the computer's paddle // Move the computer's paddle
if (((rightPaddleSpeed < 0.f) && (rightPaddle.GetPosition().y > 5.f)) || if (((rightPaddleSpeed < 0.f) && (rightPaddle.GetPosition().y > 5.f)) ||
((rightPaddleSpeed > 0.f) && (rightPaddle.GetPosition().y < window.GetView().GetSize().y - rightPaddle.GetSize().y - 5.f))) ((rightPaddleSpeed > 0.f) && (rightPaddle.GetPosition().y < window.GetView().GetSize().y - rightPaddle.GetSize().y - 5.f)))
{ {
@ -117,57 +117,57 @@ int main()
rightPaddleSpeed = -rightPaddleSpeed; rightPaddleSpeed = -rightPaddleSpeed;
} }
// Move the ball // Move the ball
float factor = ballSpeed * window.GetFrameTime(); float factor = ballSpeed * window.GetFrameTime();
ball.Move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor); ball.Move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
// Check collisions between the ball and the screen // Check collisions between the ball and the screen
if (ball.GetPosition().x < 0.f) if (ball.GetPosition().x < 0.f)
{ {
isPlaying = false; isPlaying = false;
end.SetText("You lost !\n(press escape to exit)"); end.SetText("You lost !\n(press escape to exit)");
} }
if (ball.GetPosition().x + ball.GetSize().x > window.GetView().GetSize().x) if (ball.GetPosition().x + ball.GetSize().x > window.GetView().GetSize().x)
{ {
isPlaying = false; isPlaying = false;
end.SetText("You won !\n(press escape to exit)"); end.SetText("You won !\n(press escape to exit)");
} }
if (ball.GetPosition().y < 0.f) if (ball.GetPosition().y < 0.f)
{ {
ballSound.Play(); ballSound.Play();
ballAngle = -ballAngle; ballAngle = -ballAngle;
ball.SetY(0.1f); ball.SetY(0.1f);
} }
if (ball.GetPosition().y + ball.GetSize().y > window.GetView().GetSize().y) if (ball.GetPosition().y + ball.GetSize().y > window.GetView().GetSize().y)
{ {
ballSound.Play(); ballSound.Play();
ballAngle = -ballAngle; ballAngle = -ballAngle;
ball.SetY(window.GetView().GetSize().y - ball.GetSize().y - 0.1f); ball.SetY(window.GetView().GetSize().y - ball.GetSize().y - 0.1f);
} }
// Check the collisions between the ball and the paddles // Check the collisions between the ball and the paddles
// Left Paddle // Left Paddle
if (ball.GetPosition().x < leftPaddle.GetPosition().x + leftPaddle.GetSize().x && if (ball.GetPosition().x < leftPaddle.GetPosition().x + leftPaddle.GetSize().x &&
ball.GetPosition().x > leftPaddle.GetPosition().x + (leftPaddle.GetSize().x / 2.0f) && ball.GetPosition().x > leftPaddle.GetPosition().x + (leftPaddle.GetSize().x / 2.0f) &&
ball.GetPosition().y + ball.GetSize().y >= leftPaddle.GetPosition().y && ball.GetPosition().y + ball.GetSize().y >= leftPaddle.GetPosition().y &&
ball.GetPosition().y <= leftPaddle.GetPosition().y + leftPaddle.GetSize().y) ball.GetPosition().y <= leftPaddle.GetPosition().y + leftPaddle.GetSize().y)
{ {
ballSound.Play(); ballSound.Play();
ballAngle = PI - ballAngle; ballAngle = PI - ballAngle;
ball.SetX(leftPaddle.GetPosition().x + leftPaddle.GetSize().x + 0.1f); ball.SetX(leftPaddle.GetPosition().x + leftPaddle.GetSize().x + 0.1f);
} }
// Right Paddle // Right Paddle
if (ball.GetPosition().x + ball.GetSize().x > rightPaddle.GetPosition().x && if (ball.GetPosition().x + ball.GetSize().x > rightPaddle.GetPosition().x &&
ball.GetPosition().x + ball.GetSize().x < rightPaddle.GetPosition().x + (rightPaddle.GetSize().x / 2.0f) && ball.GetPosition().x + ball.GetSize().x < rightPaddle.GetPosition().x + (rightPaddle.GetSize().x / 2.0f) &&
ball.GetPosition().y + ball.GetSize().y >= rightPaddle.GetPosition().y && ball.GetPosition().y + ball.GetSize().y >= rightPaddle.GetPosition().y &&
ball.GetPosition().y <= rightPaddle.GetPosition().y + rightPaddle.GetSize().y) ball.GetPosition().y <= rightPaddle.GetPosition().y + rightPaddle.GetSize().y)
{ {
ballSound.Play(); ballSound.Play();
ballAngle = PI - ballAngle; ballAngle = PI - ballAngle;
ball.SetX(rightPaddle.GetPosition().x - ball.GetSize().x - 0.1f); ball.SetX(rightPaddle.GetPosition().x - ball.GetSize().x - 0.1f);
} }
} }
// Clear the window // Clear the window
window.Clear(); window.Clear();