Thank you very much,
I am new in SFML, I tried to do the ideas you suggested and I was not so successful. My program is based on the pong program that comes with the installation of SFML.
I would be very happy if you could direct me how to combine the ideas you have suggested in my program.
Thank you :)
The program:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <ctime>
#include <cstdlib>
int main()
{
std::srand(static_cast<unsigned int>(std::time(NULL)));
// Define some constants
const float pi = 3.14159f;
const int gameWidth = 800;
const int gameHeight = 600;
sf::Vector2f paddleSize(100, 25);
float ballRadius = 10.f;
// Create the window of the application
sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
sf::Style::Titlebar | sf::Style::Close);
window.setVerticalSyncEnabled(true);
// Create the left paddle
sf::RectangleShape paddle;
paddle.setSize(paddleSize);
paddle.setOutlineThickness(3);
paddle.setOutlineColor(sf::Color::Black);
paddle.setFillColor(sf::Color(100, 100, 200));
paddle.setOrigin(paddleSize / 2.f);
// Create the ball
sf::CircleShape ball;
ball.setRadius(ballRadius);
ball.setOutlineThickness(3);
ball.setOutlineColor(sf::Color::Black);
ball.setFillColor(sf::Color::White);
ball.setOrigin(ballRadius / 2, ballRadius / 2);
// Define the paddles properties
sf::Clock AITimer;
const sf::Time AITime = sf::seconds(0.1f);
const float paddleSpeed = 400.f;
const float ballSpeed = 400.f;
float ballAngle = 0.f; // to be changed later
sf::Clock clock;
bool isPlaying = false;
while (window.isOpen())
{
// Handle events
sf::Event 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)))
{
window.close();
break;
}
// Space key pressed: play
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
{
if (!isPlaying)
{
// (re)start the game
isPlaying = true;
clock.restart();
// Reset the position of the paddles and ball
paddle.setPosition(gameWidth / 2, gameHeight - paddleSize.y / 2 - 10);
ball.setPosition(gameWidth / 2, gameHeight - paddleSize.y / 2 - 40);
// Reset the ball angle
do
{
// Make sure the ball initial angle is not too much vertical
ballAngle = (std::rand() % 360) * 2 * pi / 360;
} while (std::abs(std::cos(ballAngle)) < 0.7f);
}
}
}
if (isPlaying)
{
float deltaTime = clock.restart().asSeconds();
// Move the player's paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) &&
(paddle.getPosition().x - paddleSize.x / 2 > 15.f))
{
paddle.move(-paddleSpeed * deltaTime, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) &&
(paddle.getPosition().x + paddleSize.x / 2 < gameWidth - 15.f))
{
paddle.move(paddleSpeed * deltaTime, 0.f);
}
// Move the ball
float factor = ballSpeed * deltaTime;
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)
{
ballAngle = -ballAngle;
ball.setPosition(ballRadius + 0.1f, ball.getPosition().y);
}
else if (ball.getPosition().x + ballRadius > gameWidth)
{
ballAngle = -ballAngle;
ball.setPosition(gameWidth - ballRadius - 0.1f, ball.getPosition().y);
}
// up
if (ball.getPosition().y - ballRadius < 0.f)
{
ballAngle = -ballAngle;
ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
}
// down
else if (ball.getPosition().y + ballRadius > gameHeight)
{
ballAngle = -ballAngle;
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 < paddle.getPosition().x + paddleSize.x / 2 &&
ball.getPosition().x - ballRadius > paddle.getPosition().x &&
ball.getPosition().y + ballRadius >= paddle.getPosition().y - paddleSize.y / 2 &&
ball.getPosition().y - ballRadius <= paddle.getPosition().y + paddleSize.y / 2)
{
if (ball.getPosition().y > paddle.getPosition().y)
ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
else
ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
ball.setPosition(paddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y);
}
}
// Clear the window
window.clear(sf::Color(50, 200, 50));
if (isPlaying)
{
// Draw the paddles and the ball
window.draw(paddle);
window.draw(ball);
}
// Display things on screen
window.display();
}
return EXIT_SUCCESS;
}