I have been having trouble with my pong paddle - It responds to user input as if it was typing.
Sometimes, people try to react to KeyPressed events directly to implement smooth movement. Doing so will not produce the expected effect, because when you hold a key you only get a few events (remember, the repeat delay). To achieve smooth movement with events, you must use a boolean that you set on KeyPressed and clear on KeyReleased; you can then move (independently of events) as long as the boolean is set. The other (easier) solution to produce smooth movement is to use real-time keyboard input with sf::Keyboard (see the dedicated tutorial). - http://sfml-dev.org/tutorials/2.1/window-events.php
I would like to figure out the 2nd option to keep my code neat. What should I be doing differently to achieve this? I already visited the tutorial, dug around in the docs for a while, and Googled it, but such things seem to be futile (am I missing something obvious?).
My Ball Move code (in the event loop):
float deltaTime = deltaTimer.restart().asMilliseconds();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
playerPaddle.move(0 , -paddleSpeed * deltaTime);
}
My full code:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 400), "Pong");
bool isPlaying = true; //set to true only for debugging
int spacing = 3; // the distance from edge of board to paddle.
int paddleSize = 90;
float paddleSpeed = 0.1;
sf::Clock deltaTimer;
//load the background sprite
sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("assets/images/Background.png");
sf::Sprite background;
background.setTexture(backgroundTexture);
sf::RectangleShape paddle(sf::Vector2f(10,paddleSize)); //10*90 rectangle
paddle.setFillColor(sf::Color(128,128,128));
paddle.setPosition(spacing,0);
sf::RectangleShape playerPaddle(sf::Vector2f(10,paddleSize)); //10*90 rectangle
playerPaddle.setFillColor(sf::Color(128,128,128));
playerPaddle.setPosition(window.getSize().x - playerPaddle.getSize().x - spacing , 0 );
sf::RectangleShape pongBall(sf::Vector2f(10,10));
pongBall.setFillColor(sf::Color(200,200,200));
pongBall.setPosition(20,20);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (isPlaying)
{
float deltaTime = deltaTimer.restart().asMilliseconds();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) //sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
{
playerPaddle.move(0 , -paddleSpeed * deltaTime);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
playerPaddle.move(0 , paddleSpeed * deltaTime);
}
if (playerPaddle.getPosition().y < 0 )
{
playerPaddle.setPosition(playerPaddle.getPosition().x , 0);
}
else if (playerPaddle.getPosition().y > window.getSize().y - paddle.getSize().y )
{
playerPaddle.setPosition(playerPaddle.getPosition().x , window.getSize().y - paddle.getSize().y );
}
}
else
{
}
}
window.clear(sf::Color::Black);
//window.draw();
window.draw(background);
window.draw(paddle);
window.draw(playerPaddle);
window.draw(pongBall);
window.display();
}
return 0;
}