Changed the naming convention for public member variables/functions and free functions (using lowerCase instead of UpperCase)
This commit is contained in:
parent
ff5b69d312
commit
14ac411542
200 changed files with 4302 additions and 4320 deletions
|
@ -28,54 +28,54 @@ int main()
|
|||
|
||||
// Create the window of the application
|
||||
sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong");
|
||||
window.SetVerticalSyncEnabled(true);
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
// Load the sounds used in the game
|
||||
sf::SoundBuffer ballSoundBuffer;
|
||||
if (!ballSoundBuffer.LoadFromFile("resources/ball.wav"))
|
||||
if (!ballSoundBuffer.loadFromFile("resources/ball.wav"))
|
||||
return EXIT_FAILURE;
|
||||
sf::Sound ballSound(ballSoundBuffer);
|
||||
|
||||
// Create the left paddle
|
||||
sf::RectangleShape leftPaddle;
|
||||
leftPaddle.SetSize(paddleSize - sf::Vector2f(3, 3));
|
||||
leftPaddle.SetOutlineThickness(3);
|
||||
leftPaddle.SetOutlineColor(sf::Color::Black);
|
||||
leftPaddle.SetFillColor(sf::Color(100, 100, 200));
|
||||
leftPaddle.SetOrigin(paddleSize / 2.f);
|
||||
leftPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
|
||||
leftPaddle.setOutlineThickness(3);
|
||||
leftPaddle.setOutlineColor(sf::Color::Black);
|
||||
leftPaddle.setFillColor(sf::Color(100, 100, 200));
|
||||
leftPaddle.setOrigin(paddleSize / 2.f);
|
||||
|
||||
// Create the right paddle
|
||||
sf::RectangleShape rightPaddle;
|
||||
rightPaddle.SetSize(paddleSize - sf::Vector2f(3, 3));
|
||||
rightPaddle.SetOutlineThickness(3);
|
||||
rightPaddle.SetOutlineColor(sf::Color::Black);
|
||||
rightPaddle.SetFillColor(sf::Color(200, 100, 100));
|
||||
rightPaddle.SetOrigin(paddleSize / 2.f);
|
||||
rightPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
|
||||
rightPaddle.setOutlineThickness(3);
|
||||
rightPaddle.setOutlineColor(sf::Color::Black);
|
||||
rightPaddle.setFillColor(sf::Color(200, 100, 100));
|
||||
rightPaddle.setOrigin(paddleSize / 2.f);
|
||||
|
||||
// Create the ball
|
||||
sf::CircleShape ball;
|
||||
ball.SetRadius(ballRadius - 3);
|
||||
ball.SetOutlineThickness(3);
|
||||
ball.SetOutlineColor(sf::Color::Black);
|
||||
ball.SetFillColor(sf::Color::White);
|
||||
ball.SetOrigin(ballRadius / 2, ballRadius / 2);
|
||||
ball.setRadius(ballRadius - 3);
|
||||
ball.setOutlineThickness(3);
|
||||
ball.setOutlineColor(sf::Color::Black);
|
||||
ball.setFillColor(sf::Color::White);
|
||||
ball.setOrigin(ballRadius / 2, ballRadius / 2);
|
||||
|
||||
// Load the text font
|
||||
sf::Font font;
|
||||
if (!font.LoadFromFile("resources/sansation.ttf"))
|
||||
if (!font.loadFromFile("resources/sansation.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Initialize the pause message
|
||||
sf::Text pauseMessage;
|
||||
pauseMessage.SetFont(font);
|
||||
pauseMessage.SetCharacterSize(40);
|
||||
pauseMessage.SetPosition(170.f, 150.f);
|
||||
pauseMessage.SetColor(sf::Color::White);
|
||||
pauseMessage.SetString("Welcome to SFML pong!\nPress space to start the game");
|
||||
pauseMessage.setFont(font);
|
||||
pauseMessage.setCharacterSize(40);
|
||||
pauseMessage.setPosition(170.f, 150.f);
|
||||
pauseMessage.setColor(sf::Color::White);
|
||||
pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
|
||||
|
||||
// Define the paddles properties
|
||||
sf::Clock AITimer;
|
||||
const sf::Time AITime = sf::Seconds(0.1f);
|
||||
const sf::Time AITime = sf::seconds(0.1f);
|
||||
const float paddleSpeed = 400.f;
|
||||
float rightPaddleSpeed = 0.f;
|
||||
const float ballSpeed = 400.f;
|
||||
|
@ -83,22 +83,22 @@ int main()
|
|||
|
||||
sf::Clock clock;
|
||||
bool isPlaying = false;
|
||||
while (window.IsOpen())
|
||||
while (window.isOpen())
|
||||
{
|
||||
// Handle events
|
||||
sf::Event event;
|
||||
while (window.PollEvent(event))
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
// Window closed or escape key pressed: exit
|
||||
if ((event.Type == sf::Event::Closed) ||
|
||||
((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Keyboard::Escape)))
|
||||
if ((event.type == sf::Event::Closed) ||
|
||||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
|
||||
{
|
||||
window.Close();
|
||||
window.close();
|
||||
break;
|
||||
}
|
||||
|
||||
// Space key pressed: play
|
||||
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Keyboard::Space))
|
||||
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
|
||||
{
|
||||
if (!isPlaying)
|
||||
{
|
||||
|
@ -106,9 +106,9 @@ int main()
|
|||
isPlaying = true;
|
||||
|
||||
// Reset the position of the paddles and ball
|
||||
leftPaddle.SetPosition(10 + paddleSize.x / 2, gameHeight / 2);
|
||||
rightPaddle.SetPosition(gameWidth - 10 - paddleSize.x / 2, gameHeight / 2);
|
||||
ball.SetPosition(gameWidth / 2, gameHeight / 2);
|
||||
leftPaddle.setPosition(10 + paddleSize.x / 2, gameHeight / 2);
|
||||
rightPaddle.setPosition(gameWidth - 10 - paddleSize.x / 2, gameHeight / 2);
|
||||
ball.setPosition(gameWidth / 2, gameHeight / 2);
|
||||
|
||||
// Reset the ball angle
|
||||
do
|
||||
|
@ -123,34 +123,34 @@ int main()
|
|||
|
||||
if (isPlaying)
|
||||
{
|
||||
float deltaTime = clock.Restart().AsSeconds();
|
||||
float deltaTime = clock.restart().asSeconds();
|
||||
|
||||
// Move the player's paddle
|
||||
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) &&
|
||||
(leftPaddle.GetPosition().y - paddleSize.y / 2 > 5.f))
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
|
||||
(leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
|
||||
{
|
||||
leftPaddle.Move(0.f, -paddleSpeed * deltaTime);
|
||||
leftPaddle.move(0.f, -paddleSpeed * deltaTime);
|
||||
}
|
||||
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Down) &&
|
||||
(leftPaddle.GetPosition().y + paddleSize.y / 2 < gameHeight - 5.f))
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) &&
|
||||
(leftPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))
|
||||
{
|
||||
leftPaddle.Move(0.f, paddleSpeed * deltaTime);
|
||||
leftPaddle.move(0.f, paddleSpeed * deltaTime);
|
||||
}
|
||||
|
||||
// Move the computer's paddle
|
||||
if (((rightPaddleSpeed < 0.f) && (rightPaddle.GetPosition().y - paddleSize.y / 2 > 5.f)) ||
|
||||
((rightPaddleSpeed > 0.f) && (rightPaddle.GetPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))
|
||||
if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) ||
|
||||
((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))
|
||||
{
|
||||
rightPaddle.Move(0.f, rightPaddleSpeed * deltaTime);
|
||||
rightPaddle.move(0.f, rightPaddleSpeed * deltaTime);
|
||||
}
|
||||
|
||||
// Update the computer's paddle direction according to the ball position
|
||||
if (AITimer.GetElapsedTime() > AITime)
|
||||
if (AITimer.getElapsedTime() > AITime)
|
||||
{
|
||||
AITimer.Restart();
|
||||
if (ball.GetPosition().y + ballRadius > rightPaddle.GetPosition().y + paddleSize.y / 2)
|
||||
AITimer.restart();
|
||||
if (ball.getPosition().y + ballRadius > rightPaddle.getPosition().y + paddleSize.y / 2)
|
||||
rightPaddleSpeed = paddleSpeed;
|
||||
else if (ball.GetPosition().y - ballRadius < rightPaddle.GetPosition().y - paddleSize.y / 2)
|
||||
else if (ball.getPosition().y - ballRadius < rightPaddle.getPosition().y - paddleSize.y / 2)
|
||||
rightPaddleSpeed = -paddleSpeed;
|
||||
else
|
||||
rightPaddleSpeed = 0.f;
|
||||
|
@ -158,82 +158,82 @@ int main()
|
|||
|
||||
// Move the ball
|
||||
float factor = ballSpeed * deltaTime;
|
||||
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
|
||||
if (ball.GetPosition().x - ballRadius < 0.f)
|
||||
if (ball.getPosition().x - ballRadius < 0.f)
|
||||
{
|
||||
isPlaying = false;
|
||||
pauseMessage.SetString("You lost !\nPress space to restart or\nescape to exit");
|
||||
pauseMessage.setString("You lost !\nPress space to restart or\nescape to exit");
|
||||
}
|
||||
if (ball.GetPosition().x + ballRadius > 800)
|
||||
if (ball.getPosition().x + ballRadius > 800)
|
||||
{
|
||||
isPlaying = false;
|
||||
pauseMessage.SetString("You won !\nPress space to restart or\nescape to exit");
|
||||
pauseMessage.setString("You won !\nPress space to restart or\nescape to exit");
|
||||
}
|
||||
if (ball.GetPosition().y - ballRadius < 0.f)
|
||||
if (ball.getPosition().y - ballRadius < 0.f)
|
||||
{
|
||||
ballSound.Play();
|
||||
ballSound.play();
|
||||
ballAngle = -ballAngle;
|
||||
ball.SetPosition(ball.GetPosition().x, ballRadius + 0.1f);
|
||||
ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
|
||||
}
|
||||
if (ball.GetPosition().y + ballRadius > gameHeight)
|
||||
if (ball.getPosition().y + ballRadius > gameHeight)
|
||||
{
|
||||
ballSound.Play();
|
||||
ballSound.play();
|
||||
ballAngle = -ballAngle;
|
||||
ball.SetPosition(ball.GetPosition().x, gameHeight - ballRadius - 0.1f);
|
||||
ball.setPosition(ball.getPosition().x, gameHeight - ballRadius - 0.1f);
|
||||
}
|
||||
|
||||
// Check the collisions between the ball and the paddles
|
||||
// Left Paddle
|
||||
if (ball.GetPosition().x - ballRadius < leftPaddle.GetPosition().x + paddleSize.x / 2 &&
|
||||
ball.GetPosition().x - ballRadius > leftPaddle.GetPosition().x &&
|
||||
ball.GetPosition().y + ballRadius >= leftPaddle.GetPosition().y - paddleSize.y / 2 &&
|
||||
ball.GetPosition().y - ballRadius <= leftPaddle.GetPosition().y + paddleSize.y / 2)
|
||||
if (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 &&
|
||||
ball.getPosition().x - ballRadius > leftPaddle.getPosition().x &&
|
||||
ball.getPosition().y + ballRadius >= leftPaddle.getPosition().y - paddleSize.y / 2 &&
|
||||
ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2)
|
||||
{
|
||||
if (ball.GetPosition().y > leftPaddle.GetPosition().y)
|
||||
if (ball.getPosition().y > leftPaddle.getPosition().y)
|
||||
ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
|
||||
else
|
||||
ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
|
||||
|
||||
ballSound.Play();
|
||||
ball.SetPosition(leftPaddle.GetPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.GetPosition().y);
|
||||
ballSound.play();
|
||||
ball.setPosition(leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y);
|
||||
}
|
||||
|
||||
// Right Paddle
|
||||
if (ball.GetPosition().x + ballRadius > rightPaddle.GetPosition().x - paddleSize.x / 2 &&
|
||||
ball.GetPosition().x + ballRadius < rightPaddle.GetPosition().x &&
|
||||
ball.GetPosition().y + ballRadius >= rightPaddle.GetPosition().y - paddleSize.y / 2 &&
|
||||
ball.GetPosition().y - ballRadius <= rightPaddle.GetPosition().y + paddleSize.y / 2)
|
||||
if (ball.getPosition().x + ballRadius > rightPaddle.getPosition().x - paddleSize.x / 2 &&
|
||||
ball.getPosition().x + ballRadius < rightPaddle.getPosition().x &&
|
||||
ball.getPosition().y + ballRadius >= rightPaddle.getPosition().y - paddleSize.y / 2 &&
|
||||
ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2)
|
||||
{
|
||||
if (ball.GetPosition().y > rightPaddle.GetPosition().y)
|
||||
if (ball.getPosition().y > rightPaddle.getPosition().y)
|
||||
ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
|
||||
else
|
||||
ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
|
||||
|
||||
ballSound.Play();
|
||||
ball.SetPosition(rightPaddle.GetPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.GetPosition().y);
|
||||
ballSound.play();
|
||||
ball.setPosition(rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the window
|
||||
window.Clear(sf::Color(50, 200, 50));
|
||||
window.clear(sf::Color(50, 200, 50));
|
||||
|
||||
if (isPlaying)
|
||||
{
|
||||
// Draw the paddles and the ball
|
||||
window.Draw(leftPaddle);
|
||||
window.Draw(rightPaddle);
|
||||
window.Draw(ball);
|
||||
window.draw(leftPaddle);
|
||||
window.draw(rightPaddle);
|
||||
window.draw(ball);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Draw the pause message
|
||||
window.Draw(pauseMessage);
|
||||
window.draw(pauseMessage);
|
||||
}
|
||||
|
||||
// Display things on screen
|
||||
window.Display();
|
||||
window.display();
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue