1
\$\begingroup\$

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;

}

\$\endgroup\$
1
\$\begingroup\$

You are blocking on pollEvent. window.PollEvent is for handing events, not your draw loop. Since you are looking at the keypresses every frame, you can't do it in the event handling - it is only trigger on the actual keypress events (which you are now ignoring, which is fine). Move it into the draw loop.

Take a look at this sample:

http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php#the-drawing-window

Good luck!

\$\endgroup\$
4
  • \$\begingroup\$ Should I move all of my (isplaying) code into the draw loop as well? \$\endgroup\$ – DivideByZero Nov 18 '14 at 14:41
  • \$\begingroup\$ Yes I would think so. I am not that familiar with your framework you are using but normally games so not render in response to events. \$\endgroup\$ – Steven Nov 18 '14 at 15:50
  • \$\begingroup\$ If this solved your problem please mark this question answered. \$\endgroup\$ – Steven Nov 18 '14 at 19:33
  • \$\begingroup\$ I will as soon as I get a chance to try it (soon). \$\endgroup\$ – DivideByZero Nov 18 '14 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.