jeudi 30 juillet 2015

Make SFML Snake game more responsive to key presses?

I found a SFML C++ snake game and I've been messing around with it and changing a few things, but one of the things I can't figure out is how to make it more smooth/responsive with the arrow key presses. Right now it's using enum Direction {Up, Down, Left, Right}; with

while (window.isOpen())
{

    sf::Vector2f lastPosition(snakeBody[0].getPosition().x, snakeBody[0].getPosition().y);

    // Event
    sf::Event event;
    while (window.pollEvent(event))
    {
        //.....

        if (event.type == sf::Event::KeyPressed && event.key.code
                == sf::Keyboard::Return)
        {
            //clock.restart;
            if (!currentlyPlaying)
                currentlyPlaying = true;
            move = Down;

            New_Game(snakeBody, window_width, window_height, engine, apple, score, scoreText, lowBounds);
            mode = 1;
            moveClock.restart();
            //start game
        }
            if(inputClock.getElapsedTime().asSeconds() >= 0.07)
            {
                if(event.key.code == sf::Keyboard::Up && move != Down)
                    move = Up;
                inputClock.restart();
                if(event.key.code == sf::Keyboard::Down && move != Up)
                    move = Down;
                inputClock.restart();
                if(event.key.code == sf::Keyboard::Left && move != Right)
                    move = Left;
                inputClock.restart();
                if(event.key.code == sf::Keyboard::Right && move != Left)
                    move = Right;
                inputClock.restart();
            }
}

It's frustrating to play currently because I can't move as precisely as I would like. Is there a way to make the controls smoother or is it already responding to key presses as quickly as it's able to with my hardware?

Aucun commentaire:

Enregistrer un commentaire