#SFML key input and events?

4 messages · Page 1 of 1 (latest)

humble stag
#

Hi im a beginner at SFML, making my first project which is a pong clone. This is what ive made so far and i just noticed some unintended behavior with the player paddle. It moves how its supposed to with my up and down arrow key, but if i press on random keys while holding down an arrow key, it makes my paddle pause for half a sec. I read the SFML tutorial but still dont know why, any help would be appreciated.

Here's a vid to show u what i mean

https://gyazo.com/031923d604a715262fa27d2ea19ec13f

obsidian rapidsBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

humble stag
#
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using std::cout;

int main() {
    // Create Window
    RenderWindow window(VideoMode({ 800, 600 }), "Pong", Style::Titlebar | Style::Close);
    window.setVerticalSyncEnabled(true);
    // Create Paddles
    RectangleShape player_paddle({ 14, 100 }); 
    RectangleShape opponent_paddle({ 14, 100 });
    player_paddle.setFillColor(Color::Yellow);
    opponent_paddle.setFillColor(Color::White);
    // Center paddles on sides of screen
    player_paddle.setPosition({ 786, 250 });
    opponent_paddle.setPosition({ 0, 250 });
    // Create ball
    CircleShape ball({ 5 }); // radius 5
    // Centers the ball in the window
    ball.setPosition({ 395, 295 });

    bool paddle_moving_up = false;
    bool paddle_moving_down = false;

    while (window.isOpen()) {
        while (const std::optional<Event> event = window.pollEvent()) {
            if (Keyboard::isKeyPressed(Keyboard::Key::Up)) {
                paddle_moving_up = true;
            }
            if (Keyboard::isKeyPressed(Keyboard::Key::Down)) {
                paddle_moving_down = true;
            }
            if (event->is<Event::Closed>())
                window.close();
        } 
        if (paddle_moving_up) {
            // check top window collision
            if (player_paddle.getPosition().y - 10 >= 0) {
                player_paddle.move({ 0, -10 });
            }
        }
        if (paddle_moving_down) {
            // check bottom window collision
            if ((player_paddle.getPosition().y + 100) + 10 <= 600) {
                player_paddle.move({ 0, 10 });
            }
        }
        paddle_moving_up = false; paddle_moving_down = false;
        window.clear();
        window.draw(player_paddle); window.draw(opponent_paddle);
        window.draw(ball);
        window.display();
    }
}
#

I just realized that if I:

hold up or down arrow key and then hold a random key - my paddle pauses for half a second and then continues moving

hold up or down arrow key and then press and release a random key - my paddle stops moving completely, until i release the arrow key and then hold it again