简体   繁体   中英

SFML screen movement is slow

I have recently started to learn SFML and I wanted to make a Pong clone because it should be easy but I got into this problem while coding:

The bat movement is very laggy and when I press A or D it moves a bit then stops then moves again and continues.

#include <SFML/Graphics.hpp>
#include "bat.h"
int main()
{
int windowWidth=1024;
int windowHeight=728;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "SFML window");

bat Bat(windowWidth/2,windowHeight-20);



while (window.isOpen())
{

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

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))

                Bat.batMoveLeft();

            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))

            Bat.batMoveRight();

            else if (event.type == sf::Event::Closed)
            window.close();
    }


    window.clear();

Bat.batUpdate();

window.draw(Bat.getShape());

    window.display();
}


return 0;
}

bat.h

#ifndef BAT_H
#define BAT_H
#include <SFML/Graphics.hpp>

class bat
{
private:
           sf::Vector2f position;

    float batSpeed = .3f;

    sf::RectangleShape batShape;

public:
    bat(float startX, float startY);

    sf::FloatRect getPosition();

    sf::RectangleShape getShape();

    void batMoveLeft();

    void batMoveRight();

    void batUpdate();



 };

#endif // BAT_H

bat.cpp

    #include "bat.h"
using namespace sf;
bat::bat(float startX,float startY)
{
position.x=startX;

position.y=startY;

batShape.setSize(sf::Vector2f(50,5));
batShape.setPosition(position);

}
FloatRect bat::getPosition()
{
    return batShape.getGlobalBounds();
}

RectangleShape bat::getShape()
{
    return batShape;
}
void bat::batMoveLeft()
{
    position.x -= batSpeed;
}
void bat::batMoveRight()
{
    position.x += batSpeed;
}
void bat::batUpdate()
{
    batShape.setPosition(position);
}

Your problem is you're input handling strategies (polling events vs. checking current state).

In addition, the way you've implemented this right now, means that if there are – just assumption – 5 events in the queue, you'll move the bat 5 times between drawing. If there is only one event (eg "key down"), you'll move the bat once.

What you'll typically want to do is check the events while iterating over them:

while (window.pollEvent(event)) {
    switch (event.type) {
        case sf::Event::Closed:
            window.close();
            break;
        case sf::Event::KeyDown:
            switch (event.key.code) {
                case sf::Key::Left:
                    bat.moveLeft();
                    break;
                // other cases here
            }
            break;
    }
}

(Note this is from memory, so untested and might include typos.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM